blob: db9ae8446f15e000c8c9fb435243f16c6cff0e45 [file] [log] [blame]
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -05001#!/usr/bin/python
2
3import os
4import pdb
Sapan Bhatiacdd90b72014-01-28 20:03:13 -05005import copy
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -05006import sys
7import json
8import re
9from django.template import Context, Template
10
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050011blacklist = ['SingletonModel','PlCoreBase']
12
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050013# Django set up
14
15sys.path.append('.')
16os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050017from django.db.models.fields.related import ForeignKey, ManyToManyField
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050018from core.models import *
19
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050020def singular(foo, keys):
21 for k in keys:
22 if (foo==k+'es'):
23 return k
24 elif (foo==k+'s'):
25 return k
26 raise Exception('Plural to singular error for %s'%foo)
27
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050028g = globals()
29
30def enum_classes():
31 model_classes = []
32 for c in g.values():
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050033 if type(c)==type(PlCoreBase) and c.__name__ not in blacklist:
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050034 model_classes.append(c)
35 return model_classes
36
37
38class GenObj(object):
39 def __str__(self):
40 return str(self.model.__name__.lower())
41
42 def __init__(self, m):
43 self.model = m
44 self.props = []
45 self.refs = []
46
47 def plural(self):
48 name = str(self)
49 if (name.endswith('s')):
50 return name+'es'
51 else:
52 return name+'s'
53
54 def camel(self):
55 name = str(self.model.__name__)
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050056 return name
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050057
58class Generator(dict):
59 def all(self):
60 return self.values()
61
62 def regex(self, r):
63 filtered = filter(lambda o:re.match(r,str(o)), self.values())
64 return filtered
65
66 def add_object(self, o):
67 obj = GenObj(o)
68 fields = o._meta.fields
69 self[str(obj).lower()]=obj
70
71 def compute_links(self):
72 for obj in self.values():
Sapan Bhatiacdd90b72014-01-28 20:03:13 -050073 #if (str(obj).lower()=='user'):
74 # pdb.set_trace()
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050075 fields = obj.model._meta.fields
76 for f in fields:
77 if type(f)==ForeignKey and f.name in self.keys():
78 refobj = self[f.name]
79 obj.refs.append(refobj)
80 else:
81 obj.props.append(f.name)
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050082 m2m = obj.model._meta.many_to_many
83 for f in m2m:
84 try:
85 related_model_name = f.m2m_reverse_field_name()
86 except:
87 related_model_name = f.m2m_db_table().rsplit('_',1)[-1]
88
89 if related_model_name in self.keys():
Sapan Bhatiacdd90b72014-01-28 20:03:13 -050090 # pdb.set_trace()
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050091 refobj = self[related_model_name]
Sapan Bhatiacdd90b72014-01-28 20:03:13 -050092 cobj = copy.deepcopy(obj)
93 cobj.multi=True
94 refobj.refs.append(cobj)
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050095
Sapan Bhatia3a45f8b2014-01-14 21:20:16 -050096
97def main():
98 try:
99 output = sys.argv[1]
100 except:
101 print 'Usage: modelgen <output template>'
102 exit(1)
103
104 generator = Generator()
105
106 models = enum_classes()
107
108 for m in models:
109 generator.add_object(m)
110
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)
116
117
118if (__name__=='__main__'):
119 main()