|
@@ -7,7 +7,9 @@ import django
|
7
|
7
|
from django.db import models
|
8
|
8
|
from django.db.models.loading import cache as django_cache
|
9
|
9
|
from django.core.exceptions import ValidationError
|
|
10
|
+from django.contrib import admin
|
10
|
11
|
from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
|
|
12
|
+from EditorialModel.classtypes import EmNature
|
11
|
13
|
|
12
|
14
|
from EditorialModel.exceptions import *
|
13
|
15
|
|
|
@@ -183,19 +185,24 @@ class DjangoMigrationHandler(DummyMigrationHandler):
|
183
|
185
|
# @param level str: Wich type of model are we doing. Possible values are 'class' and 'type'
|
184
|
186
|
# @param datas list : List of property => value to set in the save function
|
185
|
187
|
# @return The wanted save function
|
186
|
|
- def get_save_fun(self, classname, level, datas):
|
|
188
|
+ @staticmethod
|
|
189
|
+ def set_save_fun(class_obj, level, datas):
|
187
|
190
|
|
188
|
191
|
if level == 'class':
|
189
|
192
|
def save(self, *args, **kwargs):
|
|
193
|
+ #Sets the classtype and class name
|
190
|
194
|
self.classtype = datas['classtype']
|
191
|
195
|
self.class_name = datas['class_name']
|
192
|
|
- super(classname, self).save(*args, **kwargs)
|
|
196
|
+ super(class_obj, self).save(*args, **kwargs)
|
193
|
197
|
elif level == 'type':
|
194
|
198
|
def save(self, *args, **kwargs):
|
|
199
|
+ #Set the type name
|
195
|
200
|
self.type_name = datas['type_name']
|
196
|
|
- super(classname, self).save(*args, **kwargs)
|
|
201
|
+ super(class_obj, self).save(*args, **kwargs)
|
197
|
202
|
|
198
|
|
- return save
|
|
203
|
+ class_obj.save = save
|
|
204
|
+
|
|
205
|
+ return class_obj
|
199
|
206
|
|
200
|
207
|
## @brief Create django models from an EditorialModel.model object
|
201
|
208
|
# @param edMod EditorialModel.model.Model : The editorial model instance
|
|
@@ -218,8 +225,8 @@ class DjangoMigrationHandler(DummyMigrationHandler):
|
218
|
225
|
document_attrs = {
|
219
|
226
|
'lodel_id': models.AutoField(primary_key=True),
|
220
|
227
|
'classtype': models.CharField(max_length=16, editable=False),
|
221
|
|
- 'class_name': models.CharField(max_length=16, editable=False),
|
222
|
|
- 'type_name': models.CharField(max_length=16, editable=False),
|
|
228
|
+ 'class_name': models.CharField(max_length=76, editable=False),
|
|
229
|
+ 'type_name': models.CharField(max_length=76, editable=False),
|
223
|
230
|
'string': models.CharField(max_length=255),
|
224
|
231
|
'date_update': models.DateTimeField(auto_now=True, auto_now_add=True),
|
225
|
232
|
'date_create': models.DateTimeField(auto_now_add=True),
|
|
@@ -234,10 +241,14 @@ class DjangoMigrationHandler(DummyMigrationHandler):
|
234
|
241
|
|
235
|
242
|
classes = edMod.classes()
|
236
|
243
|
|
|
244
|
+ def object_repr(self):
|
|
245
|
+ return self.string
|
|
246
|
+
|
237
|
247
|
#Creating the EmClasses models with document inheritance
|
238
|
248
|
for emclass in classes:
|
239
|
249
|
emclass_fields = {
|
240
|
|
- 'save': self.get_save_fun(emclass.uniq_name, 'class', {'classtype': emclass.classtype, 'class_name': emclass.uniq_name}),
|
|
250
|
+ 'save': None, #will be set later using self.set_save_fun
|
|
251
|
+ '__str__': object_repr,
|
241
|
252
|
}
|
242
|
253
|
|
243
|
254
|
#Addding non optionnal fields
|
|
@@ -251,10 +262,14 @@ class DjangoMigrationHandler(DummyMigrationHandler):
|
251
|
262
|
print("Model for class %s created" % emclass.uniq_name)
|
252
|
263
|
django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
|
253
|
264
|
|
|
265
|
+ self.set_save_fun(django_models['classes'][emclass.uniq_name], 'class', {'classtype': emclass.classtype, 'class_name': emclass.uniq_name})
|
|
266
|
+
|
|
267
|
+
|
254
|
268
|
#Creating the EmTypes models with EmClass inherithance
|
255
|
269
|
for emtype in emclass.types():
|
256
|
270
|
emtype_fields = {
|
257
|
|
- 'save': self.get_save_fun(emtype.uniq_name, 'type', {'type_name': emtype.uniq_name}),
|
|
271
|
+ 'save':None,
|
|
272
|
+ '__str__': object_repr,
|
258
|
273
|
}
|
259
|
274
|
#Adding selected optionnal fields
|
260
|
275
|
for emfield in emtype.selected_fields():
|
|
@@ -263,13 +278,29 @@ class DjangoMigrationHandler(DummyMigrationHandler):
|
263
|
278
|
#Adding superiors foreign key
|
264
|
279
|
for nature, superiors_list in emtype.superiors().items():
|
265
|
280
|
for superior in superiors_list:
|
266
|
|
- emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
|
|
281
|
+ emtype_fields[nature+'_'+superior.uniq_name] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, blank=True, default=None, null=True)
|
|
282
|
+
|
|
283
|
+ # Method to get the parent that is not None
|
|
284
|
+ emtype_fields['sup_field_list'] = [ nature + '_' + superior.uniq_name for superior in superiors_list ]
|
|
285
|
+ def get_sup(self):
|
|
286
|
+ for field in [ getattr(self, fname) for fname in self.sup_field_list ]:
|
|
287
|
+ if not (field is None):
|
|
288
|
+ return field
|
|
289
|
+ return None
|
|
290
|
+ #Adding a method to get the superior by nature
|
|
291
|
+ emtype_fields[nature] = get_sup
|
|
292
|
+
|
|
293
|
+ # Adding a dummy function to non present nature
|
|
294
|
+ def dummyNone(self): return None
|
|
295
|
+ for nat in [ nat for nat in EmNature.getall() if nat not in emtype_fields]:
|
|
296
|
+ emtype_fields[nat] = dummyNone
|
|
297
|
+
|
267
|
298
|
|
268
|
299
|
if self.debug:
|
269
|
300
|
print("Model for type %s created" % emtype.uniq_name)
|
270
|
|
- django_models['types'][emtype.uniq_name] = create_model(emtype.uniq_name, emtype_fields, self.app_name, module_name, parent_class=django_models['classes'][emclass.uniq_name])
|
271
|
|
-
|
272
|
|
- pass
|
|
301
|
+ django_models['types'][emtype.uniq_name] = create_model(emtype.uniq_name, emtype_fields, self.app_name, module_name, parent_class=django_models['classes'][emclass.uniq_name], admin_opts=dict())
|
|
302
|
+ self.set_save_fun(django_models['types'][emtype.uniq_name], 'type', {'type_name': emtype.uniq_name})
|
|
303
|
+ return django_models
|
273
|
304
|
|
274
|
305
|
## @brief Return a good django field type given a field
|
275
|
306
|
# @param f EmField : an EmField object
|