9 from django.template import Context, Template
11 blacklist = ['SingletonModel','PlCoreBase']
16 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
17 from django.db.models.fields.related import ForeignKey, ManyToManyField
18 from core.models import *
20 def singular(foo, keys):
26 raise Exception('Plural to singular error for %s'%foo)
33 if type(c)==type(PlCoreBase) and c.__name__ not in blacklist:
34 model_classes.append(c)
40 return str(self.model.__name__.lower())
42 def __init__(self, m):
49 if (name.endswith('s')):
55 name = str(self.model.__name__)
58 class Generator(dict):
63 filtered = filter(lambda o:re.match(r,str(o)), self.values())
66 def add_object(self, o):
68 fields = o._meta.fields
69 self[str(obj).lower()]=obj
71 def compute_links(self):
72 for obj in self.values():
73 #if (str(obj).lower()=='user'):
75 fields = obj.model._meta.fields
77 if type(f)==ForeignKey and f.name in self.keys():
79 obj.refs.append(refobj)
81 obj.props.append(f.name)
82 m2m = obj.model._meta.many_to_many
85 related_model_name = f.m2m_reverse_field_name()
87 related_model_name = f.m2m_db_table().rsplit('_',1)[-1]
89 if related_model_name in self.keys():
91 refobj = self[related_model_name]
92 cobj = copy.deepcopy(obj)
94 refobj.refs.append(cobj)
101 print 'Usage: modelgen <output template>'
104 generator = Generator()
106 models = enum_classes()
109 generator.add_object(m)
111 generator.compute_links()
112 template_contents = open(output).read()
113 template = Template(template_contents)
114 context = Context({'generator':generator})
115 print template.render(context)
118 if (__name__=='__main__'):