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):
46 self.plural_name = None
49 if (self.plural_name):
50 return self.plural_name
53 if (name.endswith('s')):
62 # These are things that either for historic reasons or due to incorrect naming,
63 # got called something different than the autogen thinks they should be
65 REST_FIXUP = {'controllernetworkses': 'controllernetworks',
66 'controllerimageses': 'controllerimages',
67 'controllersliceses': 'controllerslices',
68 'controlleruserses': 'controllerusers',
69 'sitedeploymentses': 'sitedeployments',
70 'siteroles': 'site_roles',
71 'sliceprivileges': 'slice_privileges',
72 'sliceroles': 'slice_roles',
74 return REST_FIXUP.get(self.plural(), self.plural())
77 name = str(self.model.__name__)
80 class Generator(dict):
85 filtered = filter(lambda o:re.match(r,str(o)), self.values())
88 def add_object(self, o):
90 fields = o._meta.fields
91 self[str(obj).lower()]=obj
93 def compute_links(self):
94 for obj in self.values():
95 #if (str(obj)=='network'):
97 fields = obj.model._meta.fields
100 to_name = str(f.rel.to)
104 if type(f)==ForeignKey and to_name and to_name in self.keys():
105 refobj = self[f.to_name]
107 if (str(obj)=='slice' and f.to_name=='networks'):
108 obj.refs.append(refobj)
109 related_name = f.related_query_name()
110 if (related_name!='+' and related_name.lower()!=str(obj).lower()):
111 cobj = copy.deepcopy(obj)
113 cobj.plural_name = related_name
114 refobj.refs.append(cobj)
116 obj.props.append(f.name)
118 m2m = obj.model._meta.many_to_many
121 related_model_name = f.m2m_reverse_field_name()
123 related_model_name = f.m2m_db_table().rsplit('_',1)[-1]
125 related_name = f.related_query_name()
126 if related_model_name in self.keys():
127 #print "XXX1", obj, f, related_name, related_model_name
128 refobj = self[related_model_name]
129 cobj = copy.deepcopy(obj)
131 refobj.refs.append(cobj)
133 # deal with upgradeFrom_rel_+
134 if (related_name.endswith("+")):
137 if (related_name!='+' and related_name.lower()!=str(obj).lower()):
138 #print "XXX2", obj, f, related_name, related_model_name, refobj.plural_name
139 refobj = self[related_model_name]
140 cobj = copy.deepcopy(refobj)
143 obj.refs.append(cobj)
152 print 'Usage: modelgen <output template>'
155 generator = Generator()
157 models = enum_classes()
160 generator.add_object(m)
162 generator.compute_links()
163 template_contents = open(output).read()
164 template = Template(template_contents)
165 context = Context({'generator':generator})
166 print template.render(context)
169 if (__name__=='__main__'):