|
@@ -4,10 +4,12 @@
|
4
|
4
|
#settings.configure(DEBUG=True)
|
5
|
5
|
import os
|
6
|
6
|
import sys
|
7
|
|
-from django.db import models
|
8
|
|
-import django
|
9
|
7
|
|
|
8
|
+import django
|
|
9
|
+from django.db import models
|
10
|
10
|
from django.db.models.loading import cache as django_cache
|
|
11
|
+from django.core.exceptions import ValidationError
|
|
12
|
+
|
11
|
13
|
from EditorialModel.exceptions import *
|
12
|
14
|
|
13
|
15
|
#django.conf.settings.configure(DEBUG=True)
|
|
@@ -70,6 +72,7 @@ class DjangoMigrationHandler(object):
|
70
|
72
|
##
|
71
|
73
|
# @param app_name str : The django application name for models generation
|
72
|
74
|
# @param debug bool : Set to True to be in debug mode
|
|
75
|
+ # @warning DONT use self.models it does not contains all the models (none of the through models for rel2type)
|
73
|
76
|
def __init__(self, app_name, debug=False, dryrun=False):
|
74
|
77
|
self.models = {}
|
75
|
78
|
self.debug = debug
|
|
@@ -247,7 +250,7 @@ class DjangoMigrationHandler(object):
|
247
|
250
|
if not emfield.optional:
|
248
|
251
|
# !!! Replace with fieldtype 2 django converter
|
249
|
252
|
#emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
|
250
|
|
- emclass_fields[emfield.uniq_name] = emfield.to_django()
|
|
253
|
+ emclass_fields[emfield.uniq_name] = self.field_to_django(emfield, emclass)
|
251
|
254
|
#print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
|
252
|
255
|
print("Model for class %s created"%emclass.uniq_name)
|
253
|
256
|
django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
|
|
@@ -260,7 +263,7 @@ class DjangoMigrationHandler(object):
|
260
|
263
|
#Adding selected optionnal fields
|
261
|
264
|
for emfield in emtype.selected_fields():
|
262
|
265
|
#emtype_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
|
263
|
|
- emtype_fields[emfield.uniq_name] = emfield.to_django()
|
|
266
|
+ emtype_fields[emfield.uniq_name] = self.field_to_django(emfield, emtype)
|
264
|
267
|
#Adding superiors foreign key
|
265
|
268
|
for nature, superior in emtype.superiors().items():
|
266
|
269
|
emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
|
|
@@ -272,4 +275,55 @@ class DjangoMigrationHandler(object):
|
272
|
275
|
self.models=django_models
|
273
|
276
|
pass
|
274
|
277
|
|
|
278
|
+ ## @brief Return a good django field type given a field
|
|
279
|
+ # @param f EmField : an EmField object
|
|
280
|
+ # @param assoc_comp EmComponent : The associated component (type or class)
|
|
281
|
+ def field_to_django(self, f, assoc_comp):
|
|
282
|
+
|
|
283
|
+ args = dict()
|
|
284
|
+ args['null'] = f.nullable
|
|
285
|
+ if not (f.default is None):
|
|
286
|
+ args['default'] = f.default
|
|
287
|
+ v_fun = f.validation_function(raise_e = ValidationError)
|
|
288
|
+ if v_fun:
|
|
289
|
+ args['validators'] = [v_fun]
|
|
290
|
+
|
|
291
|
+ if f.ftype == 'char':
|
|
292
|
+ args['max_length'] = f.max_length
|
|
293
|
+ return models.CharField(**args)
|
|
294
|
+ elif f.ftype == 'int':
|
|
295
|
+ return models.IntegerField(**args)
|
|
296
|
+ elif f.ftype == 'rel2type':
|
|
297
|
+
|
|
298
|
+ if assoc_comp == None:
|
|
299
|
+ raise RuntimeError("Rel2type field in a rel2type table is not allowed")
|
|
300
|
+ #create first a throught model if there is data field associated with the relation
|
|
301
|
+ kwargs = dict()
|
|
302
|
+
|
|
303
|
+ relf_l = f.get_related_fields()
|
|
304
|
+ if len(relf_l) > 0:
|
|
305
|
+ through_fields = {}
|
|
306
|
+
|
|
307
|
+ #The two FK of the through model
|
|
308
|
+ through_fields[assoc_comp.name] = models.ForeignKey(assoc_comp.uniq_name)
|
|
309
|
+ rtype = f.get_related_type()
|
|
310
|
+ through_fields[rtype.name] = models.ForeignKey(rtype.uniq_name)
|
|
311
|
+
|
|
312
|
+ for relf in relf_l:
|
|
313
|
+ through_fields[relf.name] = self.field_to_django(relf, None)
|
|
314
|
+
|
|
315
|
+ #through_model_name = f.uniq_name+assoc_comp.uniq_name+'to'+rtype.uniq_name
|
|
316
|
+ through_model_name = f.name+assoc_comp.name+'to'+rtype.name
|
|
317
|
+ module_name = self.app_name+'models'
|
|
318
|
+ #model created
|
|
319
|
+ through_model = create_model(through_model_name, through_fields, self.app_name, module_name)
|
|
320
|
+ kwargs['through'] = through_model_name
|
|
321
|
+
|
|
322
|
+ print('WOW !')
|
|
323
|
+ return models.ManyToManyField(f.get_related_type().uniq_name, **kwargs)
|
|
324
|
+ else:
|
|
325
|
+ raise NotImplemented("The conversion to django fields is not yet implemented for %s field type"%f.ftype)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
275
|
329
|
|