Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import pdb |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 5 | import copy |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 6 | import sys |
| 7 | import json |
| 8 | import re |
| 9 | from django.template import Context, Template |
| 10 | |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 11 | blacklist = ['SingletonModel','PlCoreBase'] |
| 12 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 13 | # Django set up |
| 14 | |
| 15 | sys.path.append('.') |
| 16 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 17 | from django.db.models.fields.related import ForeignKey, ManyToManyField |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 18 | from core.models import * |
| 19 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 20 | def 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 Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 28 | g = globals() |
| 29 | |
| 30 | def enum_classes(): |
| 31 | model_classes = [] |
| 32 | for c in g.values(): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 33 | if type(c)==type(PlCoreBase) and c.__name__ not in blacklist: |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 34 | model_classes.append(c) |
| 35 | return model_classes |
| 36 | |
| 37 | |
| 38 | class 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 = [] |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame^] | 46 | self.plural_name = None |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 47 | |
| 48 | def plural(self): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame^] | 49 | if (self.plural_name): |
| 50 | return self.plural_name |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 51 | else: |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame^] | 52 | name = str(self) |
| 53 | if (name.endswith('s')): |
| 54 | return name+'es' |
| 55 | else: |
| 56 | return name+'s' |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 57 | |
| 58 | def camel(self): |
| 59 | name = str(self.model.__name__) |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 60 | return name |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 61 | |
| 62 | class Generator(dict): |
| 63 | def all(self): |
| 64 | return self.values() |
| 65 | |
| 66 | def regex(self, r): |
| 67 | filtered = filter(lambda o:re.match(r,str(o)), self.values()) |
| 68 | return filtered |
| 69 | |
| 70 | def add_object(self, o): |
| 71 | obj = GenObj(o) |
| 72 | fields = o._meta.fields |
| 73 | self[str(obj).lower()]=obj |
| 74 | |
| 75 | def compute_links(self): |
| 76 | for obj in self.values(): |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame^] | 77 | #if (str(obj)=='siteprivilege'): |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 78 | # pdb.set_trace() |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 79 | fields = obj.model._meta.fields |
| 80 | for f in fields: |
| 81 | if type(f)==ForeignKey and f.name in self.keys(): |
| 82 | refobj = self[f.name] |
| 83 | obj.refs.append(refobj) |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame^] | 84 | related_name = f.related_query_name() |
| 85 | if (related_name.lower()!=str(obj).lower()): |
| 86 | cobj = copy.deepcopy(obj) |
| 87 | cobj.multi = True |
| 88 | cobj.plural_name = related_name |
| 89 | refobj.refs.append(cobj) |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 90 | else: |
| 91 | obj.props.append(f.name) |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 92 | m2m = obj.model._meta.many_to_many |
| 93 | for f in m2m: |
| 94 | try: |
| 95 | related_model_name = f.m2m_reverse_field_name() |
| 96 | except: |
| 97 | related_model_name = f.m2m_db_table().rsplit('_',1)[-1] |
| 98 | |
| 99 | if related_model_name in self.keys(): |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 100 | # pdb.set_trace() |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 101 | refobj = self[related_model_name] |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 102 | cobj = copy.deepcopy(obj) |
| 103 | cobj.multi=True |
| 104 | refobj.refs.append(cobj) |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame^] | 105 | |
| 106 | if (related_name.lower()!=str(obj).lower()): |
| 107 | cobj = copy.deepcopy(obj) |
| 108 | cobj.multi = True |
| 109 | cobj.plural_name = related_name |
| 110 | refobj.refs.append(obj) |
| 111 | |
| 112 | |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 113 | |
Sapan Bhatia | 3a45f8b | 2014-01-14 21:20:16 -0500 | [diff] [blame] | 114 | |
| 115 | def main(): |
| 116 | try: |
| 117 | output = sys.argv[1] |
| 118 | except: |
| 119 | print 'Usage: modelgen <output template>' |
| 120 | exit(1) |
| 121 | |
| 122 | generator = Generator() |
| 123 | |
| 124 | models = enum_classes() |
| 125 | |
| 126 | for m in models: |
| 127 | generator.add_object(m) |
| 128 | |
| 129 | generator.compute_links() |
| 130 | template_contents = open(output).read() |
| 131 | template = Template(template_contents) |
| 132 | context = Context({'generator':generator}) |
| 133 | print template.render(context) |
| 134 | |
| 135 | |
| 136 | if (__name__=='__main__'): |
| 137 | main() |