| 1 |
#!/usr/bin/env 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 |
from django.core.template import Context, Template |
|---|
| 22 |
|
|---|
| 23 |
p = OptionParser() |
|---|
| 24 |
|
|---|
| 25 |
p.add_option("-a", "--app", dest="app", help="The app which contains the model") |
|---|
| 26 |
p.add_option("-m", "--model", dest="model", help="The model to produce the form for") |
|---|
| 27 |
p.add_option("-n", "--name", dest="name", help="The name of the custom manipulator") |
|---|
| 28 |
|
|---|
| 29 |
options, args = p.parse_args() |
|---|
| 30 |
|
|---|
| 31 |
if not (options.model and options.app): |
|---|
| 32 |
p.print_help() |
|---|
| 33 |
sys.exit() |
|---|
| 34 |
|
|---|
| 35 |
if not options.name: |
|---|
| 36 |
options.name = "%s%s" % (options.app, options.model) |
|---|
| 37 |
|
|---|
| 38 |
m = __import__("django.models.%s" % (options.app,), '', '', [options.model]) |
|---|
| 39 |
|
|---|
| 40 |
a = getattr(m, options.model) |
|---|
| 41 |
|
|---|
| 42 |
def field_text(field): |
|---|
| 43 |
d = field.__dict__ |
|---|
| 44 |
args = dict([(f, isinstance(d[f], str) and "'%s'" % d[f] or d[f]) for f in d if f not in ['db_index', 'primary_key', 'rel', 'core', 'creation_counter', |
|---|
| 45 |
'editable', 'name', 'default', 'column', 'attname', 'auto_now_add', 'help_text', 'auto_now', 'unique', 'verbose_name']]) |
|---|
| 46 |
if args['blank'] is True or args['null'] is True: |
|---|
| 47 |
args['is_required'] = False |
|---|
| 48 |
else: |
|---|
| 49 |
args['is_required'] = True |
|---|
| 50 |
try: |
|---|
| 51 |
del(args['blank']) |
|---|
| 52 |
del(args['null']) |
|---|
| 53 |
except KeyError: |
|---|
| 54 |
pass |
|---|
| 55 |
try: |
|---|
| 56 |
args['validator_list'] = ['%s' % v.__name__ for v in args['validator_list']] |
|---|
| 57 |
except KeyError: |
|---|
| 58 |
pass |
|---|
| 59 |
if field.__class__.__name__ == 'CharField': |
|---|
| 60 |
fieldType = 'TextField' |
|---|
| 61 |
elif field.__class__.__name__ == 'ForeignKey': |
|---|
| 62 |
fieldType = 'SelectField' |
|---|
| 63 |
args['choices'] = "[('','-------')] + [(o.id, o) for o in %s.get_list()]" % (field.name.lower() + "s",) |
|---|
| 64 |
elif field.__class__.__name__ == 'BooleanField': |
|---|
| 65 |
fieldType = 'CheckboxField' |
|---|
| 66 |
elif field.__class__.__name__ == 'DateTimeField': |
|---|
| 67 |
fieldType = 'DatetimeField' |
|---|
| 68 |
elif field.__class__.__name__ == 'ManyToManyField': |
|---|
| 69 |
fieldType = 'SelectMultipleField' |
|---|
| 70 |
args['choices'] = "[(o.id, o) for o in %s.get_list()]" % (field.name.lower() + "s",) |
|---|
| 71 |
else: |
|---|
| 72 |
fieldType = field.__class__.__name__ |
|---|
| 73 |
extra_args = ["%s=%s" % (s, args[s]) for s in args if args[s] not in [None, '', False, []]] |
|---|
| 74 |
if extra_args: |
|---|
| 75 |
return '''\ |
|---|
| 76 |
formfields.%s(field_name='%s', %s),''' % (fieldType, field.name, ', '.join(extra_args)) |
|---|
| 77 |
else: |
|---|
| 78 |
return '''\ |
|---|
| 79 |
formfields.%s(field_name='%s'),''' % (fieldType, field.name) |
|---|
| 80 |
|
|---|
| 81 |
def value_text(field): |
|---|
| 82 |
return """\ |
|---|
| 83 |
%s=new_data['%s']""" % (field.name, field.name) |
|---|
| 84 |
|
|---|
| 85 |
def m2m_text(field): |
|---|
| 86 |
return """ temp.set_%s(newdata['%s'])""" % (field.name, field.name) |
|---|
| 87 |
|
|---|
| 88 |
template = Template( |
|---|
| 89 |
"""class {{ name }}Manipulator(formfields.Manipulator): |
|---|
| 90 |
def __init__(self, pk=None): |
|---|
| 91 |
self.fields = ( |
|---|
| 92 |
{{ fields }} |
|---|
| 93 |
) |
|---|
| 94 |
|
|---|
| 95 |
def save(self, new_data): |
|---|
| 96 |
temp = {{ model }}( |
|---|
| 97 |
{{ values }} |
|---|
| 98 |
) |
|---|
| 99 |
{{ m2m }} |
|---|
| 100 |
temp.save() |
|---|
| 101 |
return temp |
|---|
| 102 |
""") |
|---|
| 103 |
|
|---|
| 104 |
context = Context({'fields': '\n'.join([field_text(f) for f in a._meta.fields + a._meta.many_to_many if f.name !='id']), 'name': options.name, |
|---|
| 105 |
'model': options.model, 'values': ',\n'.join([value_text(f) for f in a._meta.fields if f.name != 'id']), |
|---|
| 106 |
'm2m': '\n'.join([m2m_text(f) for f in a._meta.many_to_many])}) |
|---|
| 107 |
print template.render(context) |
|---|