Browse Source

Updated the MH

Now it has a EmField 2 django method
Yann Weber 9 years ago
parent
commit
4096a19d77
1 changed files with 58 additions and 4 deletions
  1. 58
    4
      EditorialModel/migrationhandler/django.py

+ 58
- 4
EditorialModel/migrationhandler/django.py View File

4
 #settings.configure(DEBUG=True)
4
 #settings.configure(DEBUG=True)
5
 import os
5
 import os
6
 import sys
6
 import sys
7
-from django.db import models
8
-import django
9
 
7
 
8
+import django
9
+from django.db import models
10
 from django.db.models.loading import cache as django_cache
10
 from django.db.models.loading import cache as django_cache
11
+from django.core.exceptions import ValidationError
12
+
11
 from EditorialModel.exceptions import *
13
 from EditorialModel.exceptions import *
12
 
14
 
13
 #django.conf.settings.configure(DEBUG=True)
15
 #django.conf.settings.configure(DEBUG=True)
70
     ##
72
     ##
71
     # @param app_name str : The django application name for models generation
73
     # @param app_name str : The django application name for models generation
72
     # @param debug bool : Set to True to be in debug mode
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
     def __init__(self, app_name, debug=False, dryrun=False):
76
     def __init__(self, app_name, debug=False, dryrun=False):
74
         self.models = {}
77
         self.models = {}
75
         self.debug = debug
78
         self.debug = debug
247
                 if not emfield.optional:
250
                 if not emfield.optional:
248
                     # !!! Replace with fieldtype 2 django converter
251
                     # !!! Replace with fieldtype 2 django converter
249
                     #emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
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
             #print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
254
             #print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
252
             print("Model for class %s created"%emclass.uniq_name)
255
             print("Model for class %s created"%emclass.uniq_name)
253
             django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
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
                 #Adding selected optionnal fields
263
                 #Adding selected optionnal fields
261
                 for emfield in emtype.selected_fields():
264
                 for emfield in emtype.selected_fields():
262
                     #emtype_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
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
                 #Adding superiors foreign key
267
                 #Adding superiors foreign key
265
                 for nature, superior in emtype.superiors().items():
268
                 for nature, superior in emtype.superiors().items():
266
                     emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
269
                     emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
272
         self.models=django_models
275
         self.models=django_models
273
         pass
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
 

Loading…
Cancel
Save