|
@@ -12,12 +12,16 @@ from EditorialModel.exceptions import *
|
12
|
12
|
|
13
|
13
|
#django.conf.settings.configure(DEBUG=True)
|
14
|
14
|
|
15
|
|
-## @package EditorialModel.migrationhandler.django
|
16
|
|
-# @brief A migration handler for django ORM
|
17
|
|
-#
|
18
|
|
-# Create django models according to the editorial model
|
19
|
15
|
|
20
|
|
-##
|
|
16
|
+## @brief Create a django model
|
|
17
|
+# @param name str : The django model name
|
|
18
|
+# @param fields dict : A dict that contains fields name and type ( str => DjangoField )
|
|
19
|
+# @param app_label str : The name of the applications that will have those models
|
|
20
|
+# @param module str : The module name this model will belong to
|
|
21
|
+# @param options dict : Dict of options (name => value)
|
|
22
|
+# @param admin_opts dict : Dict of options for admin part of this model
|
|
23
|
+# @param parent_class str : Parent class name
|
|
24
|
+# @return A dynamically created django model
|
21
|
25
|
# @source https://code.djangoproject.com/wiki/DynamicModels
|
22
|
26
|
#
|
23
|
27
|
def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None, parent_class=None):
|
|
@@ -56,11 +60,13 @@ def create_model(name, fields=None, app_label='', module='', options=None, admin
|
56
|
60
|
return model
|
57
|
61
|
|
58
|
62
|
|
|
63
|
+## @package EditorialModel.migrationhandler.django
|
|
64
|
+# @brief A migration handler for django ORM
|
|
65
|
+#
|
|
66
|
+# Create django models according to the editorial model
|
59
|
67
|
|
60
|
68
|
class DjangoMigrationHandler(object):
|
61
|
69
|
|
62
|
|
- app_label = 'lodel'
|
63
|
|
-
|
64
|
70
|
##
|
65
|
71
|
# @param app_name str : The django application name for models generation
|
66
|
72
|
# @param debug bool : Set to True to be in debug mode
|
|
@@ -68,6 +74,7 @@ class DjangoMigrationHandler(object):
|
68
|
74
|
self.models = {}
|
69
|
75
|
self.debug = debug
|
70
|
76
|
self.app_name = app_name
|
|
77
|
+
|
71
|
78
|
## @brief Record a change in the EditorialModel and indicate wether or not it is possible to make it
|
72
|
79
|
# @note The states ( initial_state and new_state ) contains only fields that changes
|
73
|
80
|
# @param em model : The EditorialModel.model object to provide the global context
|
|
@@ -76,14 +83,29 @@ class DjangoMigrationHandler(object):
|
76
|
83
|
# @param new_state dict | None : dict with field name as key and field value as value. Representing the new state. None mean component deletion
|
77
|
84
|
# @throw EditorialModel.exceptions.MigrationHandlerChangeError if the change was refused
|
78
|
85
|
def register_change(self, em, uid, initial_state, new_state):
|
79
|
|
-
|
|
86
|
+
|
|
87
|
+ #Starting django
|
|
88
|
+ os.environ['LODEL_MIGRATION_HANDLER_TESTS'] = 'YES'
|
80
|
89
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
|
81
|
90
|
django.setup()
|
82
|
91
|
from django.contrib import admin
|
83
|
92
|
from django.core.management import call_command as django_cmd
|
84
|
93
|
|
|
94
|
+ if self.debug:
|
|
95
|
+ self.dump_migration(uid, initial_state, new_state)
|
85
|
96
|
|
|
97
|
+ #Generation django models
|
|
98
|
+ self.em_to_models(em)
|
|
99
|
+ try:
|
|
100
|
+ #Calling makemigrations to see if the migration is valid
|
|
101
|
+ django_cmd('makemigrations', self.app_name, dry_run=True, intercative=True, merge=True, noinput=True)
|
|
102
|
+ except django.core.management.base.CommandError as e:
|
|
103
|
+ raise MigrationHandlerChangeError(str(e))
|
|
104
|
+
|
|
105
|
+ return True
|
86
|
106
|
|
|
107
|
+ ## @brief Print a debug message representing a migration
|
|
108
|
+ def dump_migration(self, uid, initial_state, new_state):
|
87
|
109
|
if self.debug:
|
88
|
110
|
print("\n##############")
|
89
|
111
|
print("DummyMigrationHandler debug. Changes for component with uid %d :" % uid)
|
|
@@ -106,21 +128,11 @@ class DjangoMigrationHandler(object):
|
106
|
128
|
str_chg += " deletion "
|
107
|
129
|
print(str_chg)
|
108
|
130
|
print("##############\n")
|
109
|
|
-
|
110
|
|
- self.em_to_models(em,self.app_name, self.app_name+'models')
|
111
|
|
- try:
|
112
|
|
- #ret = django_cmd('makemigrations', self.app_name, interactive=False, noinput=True, dryrun=True, traceback=True)
|
113
|
|
- #django_cmd('makemigrations', self.app_name, dry-run=True, intercative=True, noinput=True)
|
114
|
|
- django_cmd('makemigrations', self.app_name, dry_run=True, intercative=True, merge=True, noinput=True)
|
115
|
|
- except django.core.management.base.CommandError as e:
|
116
|
|
- raise MigrationHandlerChangeError(str(e))
|
117
|
|
-
|
118
|
|
- return True
|
119
|
131
|
pass
|
120
|
|
-
|
|
132
|
+
|
|
133
|
+ ## @brief Not usefull ?
|
121
|
134
|
def register_model_state(self, em, state_hash):
|
122
|
135
|
print('OHOHOH !!! i\'ve been called')
|
123
|
|
- #ret = django_cmd('makemigrations', '--noinput', '--traceback', self.app_name)
|
124
|
136
|
pass
|
125
|
137
|
|
126
|
138
|
## @brief Return the models save method
|
|
@@ -145,22 +157,24 @@ class DjangoMigrationHandler(object):
|
145
|
157
|
super(classname, self).save(*args, **kwargs)
|
146
|
158
|
|
147
|
159
|
return save
|
148
|
|
-
|
|
160
|
+
|
149
|
161
|
## @brief Create django models from an EditorialModel.model object
|
150
|
|
- # @param me EditorialModel.model.Model : The editorial model instance
|
|
162
|
+ # @param edMod EditorialModel.model.Model : The editorial model instance
|
151
|
163
|
# @return a dict with all the models
|
152
|
164
|
# @todo Handle fieldgroups
|
153
|
165
|
# @todo write and use a function to forge models name from EmClasses and EmTypes names
|
154
|
166
|
# @note There is a problem with the related_name for superiors fk : The related name cannot be subordinates, it has to be the subordinates em_type name
|
155
|
|
- def em_to_models(self, me, app_label, module_name):
|
|
167
|
+ def em_to_models(self, edMod):
|
156
|
168
|
|
|
169
|
+ module_name = self.app_name+'models'
|
|
170
|
+
|
157
|
171
|
#Purging django models cache
|
158
|
|
- if app_label in django_cache.all_models:
|
159
|
|
- for modname in django_cache.all_models[app_label]:
|
160
|
|
- del(django_cache.all_models[app_label][modname])
|
161
|
|
- #del(django_cache.all_models[app_label])
|
|
172
|
+ if self.app_name in django_cache.all_models:
|
|
173
|
+ for modname in django_cache.all_models[self.app_name]:
|
|
174
|
+ del(django_cache.all_models[self.app_name][modname])
|
|
175
|
+ #del(django_cache.all_models[self.app_name])
|
162
|
176
|
|
163
|
|
- #django_cache.clear_cache()
|
|
177
|
+ #This cache at instance level seems to be useless...
|
164
|
178
|
del(self.models)
|
165
|
179
|
self.models = {}
|
166
|
180
|
|
|
@@ -179,11 +193,11 @@ class DjangoMigrationHandler(object):
|
179
|
193
|
}
|
180
|
194
|
|
181
|
195
|
#Creating the base model document
|
182
|
|
- document_model = create_model('document', document_attrs, app_label, module_name)
|
|
196
|
+ document_model = create_model('document', document_attrs, self.app_name, module_name)
|
183
|
197
|
|
184
|
198
|
django_models = {'doc' : document_model, 'classes':{}, 'types':{} }
|
185
|
199
|
|
186
|
|
- classes = me.classes()
|
|
200
|
+ classes = edMod.classes()
|
187
|
201
|
|
188
|
202
|
#Creating the EmClasses models with document inheritance
|
189
|
203
|
for emclass in classes:
|
|
@@ -198,7 +212,7 @@ class DjangoMigrationHandler(object):
|
198
|
212
|
emclass_fields[emfield.uniq_name] = models.CharField(max_length=56, default=emfield.uniq_name)
|
199
|
213
|
#print("Model for class %s created with fields : "%emclass.uniq_name, emclass_fields)
|
200
|
214
|
print("Model for class %s created"%emclass.uniq_name)
|
201
|
|
- django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, app_label, module_name, parent_class=django_models['doc'])
|
|
215
|
+ django_models['classes'][emclass.uniq_name] = create_model(emclass.uniq_name, emclass_fields, self.app_name, module_name, parent_class=django_models['doc'])
|
202
|
216
|
|
203
|
217
|
#Creating the EmTypes models with EmClass inherithance
|
204
|
218
|
for emtype in emclass.types():
|
|
@@ -213,12 +227,11 @@ class DjangoMigrationHandler(object):
|
213
|
227
|
for nature, superior in emtype.superiors().items():
|
214
|
228
|
emtype_fields[nature] = models.ForeignKey(superior.uniq_name, related_name=emtype.uniq_name, null=True)
|
215
|
229
|
|
216
|
|
- #print("Model for type %s created with fields : "%emtype.uniq_name, emtype_fields)
|
217
|
|
- print("Model for type %s created"%emtype.uniq_name)
|
218
|
|
- django_models['types'][emtype.uniq_name] = create_model(emtype.uniq_name, emtype_fields, app_label, module_name, parent_class=django_models['classes'][emclass.uniq_name])
|
|
230
|
+ if self.debug:
|
|
231
|
+ print("Model for type %s created"%emtype.uniq_name)
|
|
232
|
+ 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])
|
219
|
233
|
|
220
|
234
|
self.models=django_models
|
221
|
235
|
pass
|
222
|
236
|
|
223
|
237
|
|
224
|
|
-
|