| 1 |
#!/usr/bin/python |
|---|
| 2 |
|
|---|
| 3 |
from optparse import OptionParser |
|---|
| 4 |
import sys |
|---|
| 5 |
import os |
|---|
| 6 |
|
|---|
| 7 |
try: |
|---|
| 8 |
import settings |
|---|
| 9 |
except ImportError: |
|---|
| 10 |
print "Settings file not found. Place this file in the same place as manage.py" |
|---|
| 11 |
sys.exit() |
|---|
| 12 |
|
|---|
| 13 |
project_directory = os.path.dirname(settings.__file__) |
|---|
| 14 |
project_name = os.path.basename(project_directory) |
|---|
| 15 |
sys.path.append(os.path.join(project_directory, '..')) |
|---|
| 16 |
project_module = __import__(project_name, '', '', ['']) |
|---|
| 17 |
sys.path.pop() |
|---|
| 18 |
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project_name |
|---|
| 19 |
|
|---|
| 20 |
from django.core import meta |
|---|
| 21 |
|
|---|
| 22 |
p = OptionParser() |
|---|
| 23 |
|
|---|
| 24 |
p.add_option("-a", "--app", dest="app", help="The app which contains the model") |
|---|
| 25 |
p.add_option("-m", "--model", dest="model", help="The model to produce the form for") |
|---|
| 26 |
|
|---|
| 27 |
options, args = p.parse_args() |
|---|
| 28 |
|
|---|
| 29 |
if not (options.model and options.app): |
|---|
| 30 |
p.print_help() |
|---|
| 31 |
sys.exit() |
|---|
| 32 |
|
|---|
| 33 |
m = __import__("django.models.%s" % (options.app,), '', '', [options.model]) |
|---|
| 34 |
|
|---|
| 35 |
a = getattr(m, options.model) |
|---|
| 36 |
|
|---|
| 37 |
def image_field_html(name): |
|---|
| 38 |
return '''\ |
|---|
| 39 |
{{ form.NAME_file }} \ |
|---|
| 40 |
{{ form.NAME }} |
|---|
| 41 |
{% if form.NAME.errors %} |
|---|
| 42 |
*** {{ form.NAME.errors|join:", " }} |
|---|
| 43 |
{% endif %}'''.replace('NAME', name) |
|---|
| 44 |
|
|---|
| 45 |
def field_html(name): |
|---|
| 46 |
return '''\ |
|---|
| 47 |
{{ form.NAME }} |
|---|
| 48 |
{% if form.NAME.errors %} |
|---|
| 49 |
*** {{ form.NAME.errors|join:", " }} |
|---|
| 50 |
{% endif %}'''.replace('NAME', name) |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
def labeled_field_html(field): |
|---|
| 54 |
if isinstance(field, meta.DateTimeField): |
|---|
| 55 |
rows = [field_html(field.name + '_' + suffix) |
|---|
| 56 |
for suffix in 'date', 'time'] |
|---|
| 57 |
elif isinstance(field, meta.ImageField): |
|---|
| 58 |
rows = [image_field_html(field.name)] |
|---|
| 59 |
else: |
|---|
| 60 |
rows = [field_html(field.name)] |
|---|
| 61 |
return '''\ |
|---|
| 62 |
<p> |
|---|
| 63 |
<label for="id_%s">%s:</label> |
|---|
| 64 |
%s |
|---|
| 65 |
</p>''' % (field.name, field.verbose_name or field.name, '\n'.join(rows)) |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
print '<form method="POST" action=".">' |
|---|
| 69 |
print '\n'.join(labeled_field_html(f) for f in a._meta.fields if f.name !='id') |
|---|
| 70 |
print ' <input type="submit" value="submit" />' |
|---|
| 71 |
print '</form>' |
|---|