Django

Code

ScaffoldScript: scaffold.2.py

File scaffold.2.py, 1.2 kB (added by akaihola, 3 years ago)

bugfixes and datetime support

Line 
1 #!/usr/bin/python
2
3 from optparse import OptionParser
4 from django.core import meta
5 import sys
6
7 p = OptionParser()
8
9 p.add_option("-a", "--app", dest="app", help="The app which contains the model")
10 p.add_option("-m", "--model", dest="model", help="The model to produce the form for")
11
12 options, args = p.parse_args()
13
14 if not (options.model and options.app):
15     p.print_help()
16     sys.exit()
17
18 m = __import__("django.models.%s" % (options.app,), '', '', [options.model])
19
20 a = getattr(m, options.model)
21
22 def field_html(name):
23     return '''\
24     {{ form.NAME }}
25     {% if form.NAME.errors %}
26       *** {{ form.NAME.errors|join:", " }}
27     {% endif %}'''.replace('NAME', name)
28    
29
30 def labeled_field_html(field):
31     if isinstance(field, meta.DateTimeField):
32         rows = [field_html(field.name + '_' + suffix)
33                 for suffix in 'date', 'time']
34     else:
35         rows = [field_html(field.name)]
36     return '''\
37   <p>
38     <label for="id_%s">%s:</label>
39 %s
40   </p>''' % (field.name, field.verbose_name or field.name, '\n'.join(rows))
41
42
43 print '<form method="POST" action=".">'
44 print '\n'.join(labeled_field_html(f) for f in a._meta.fields if f.name !='id')
45 print '  <input type="submit" value="submit" />'
46 print '</form>'