blob: e0def4b356803a6fc4a823903eada54001422447 [file] [log] [blame]
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -05001#!/usr/bin/python
2
3import os
4import pdb
5import sys
6import json
7import re
8from django.template import Context, Template
9
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050010blacklist = ['SingletonModel','PlCoreBase']
11
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050012# Django set up
13
14sys.path.append('.')
15os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050016from django.db.models.fields.related import ForeignKey, ManyToManyField
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050017from core.models import *
18
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050019def singular(foo, keys):
20 for k in keys:
21 if (foo==k+'es'):
22 return k
23 elif (foo==k+'s'):
24 return k
25 raise Exception('Plural to singular error for %s'%foo)
26
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050027g = globals()
28
29def enum_classes():
30 model_classes = []
31 for c in g.values():
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050032 if type(c)==type(PlCoreBase) and c.__name__ not in blacklist:
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050033 model_classes.append(c)
34 return model_classes
35
36
37class GenObj(object):
38 def __str__(self):
39 return str(self.model.__name__.lower())
40
41 def __init__(self, m):
42 self.model = m
43 self.props = []
44 self.refs = []
45
46 def plural(self):
47 name = str(self)
48 if (name.endswith('s')):
49 return name+'es'
50 else:
51 return name+'s'
52
53 def camel(self):
54 name = str(self.model.__name__)
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050055 return name
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050056
57class Generator(dict):
58 def all(self):
59 return self.values()
60
61 def regex(self, r):
62 filtered = filter(lambda o:re.match(r,str(o)), self.values())
63 return filtered
64
65 def add_object(self, o):
66 obj = GenObj(o)
67 fields = o._meta.fields
68 self[str(obj).lower()]=obj
69
70 def compute_links(self):
71 for obj in self.values():
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050072 if (str(obj).lower()=='user'):
73 pdb.set_trace()
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050074 fields = obj.model._meta.fields
75 for f in fields:
76 if type(f)==ForeignKey and f.name in self.keys():
77 refobj = self[f.name]
78 obj.refs.append(refobj)
79 else:
80 obj.props.append(f.name)
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050081 m2m = obj.model._meta.many_to_many
82 for f in m2m:
83 try:
84 related_model_name = f.m2m_reverse_field_name()
85 except:
86 related_model_name = f.m2m_db_table().rsplit('_',1)[-1]
87
88 if related_model_name in self.keys():
89 refobj = self[related_model_name]
90 obj.multi=True
91 refobj.refs.append(obj)
92
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050093
94def main():
95 try:
96 output = sys.argv[1]
97 except:
98 print 'Usage: modelgen <output template>'
99 exit(1)
100
101 generator = Generator()
102
103 models = enum_classes()
104
105 for m in models:
106 generator.add_object(m)
107
108 generator.compute_links()
109 template_contents = open(output).read()
110 template = Template(template_contents)
111 context = Context({'generator':generator})
112 print template.render(context)
113
114
115if (__name__=='__main__'):
116 main()