|
@@ -5,6 +5,10 @@
|
5
|
5
|
|
6
|
6
|
import copy
|
7
|
7
|
|
|
8
|
+# imports used in classtypes <-> actual model checks
|
|
9
|
+import difflib
|
|
10
|
+import warnings
|
|
11
|
+
|
8
|
12
|
import EditorialModel
|
9
|
13
|
from EditorialModel.components import EmComponent
|
10
|
14
|
from EditorialModel.classtypes import EmClassType
|
|
@@ -31,10 +35,42 @@ class EmClass(EmComponent):
|
31
|
35
|
self.sortcolumn = sortcolumn # 'rank'
|
32
|
36
|
super(EmClass, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
|
33
|
37
|
|
34
|
|
- ## Check if the EmComponent is valid
|
35
|
|
- # @note this function add default and common fields to the EmClass if they are not yet created
|
|
38
|
+ ## @brief Check if the EmComponent is valid
|
|
39
|
+ #
|
|
40
|
+ # This function add default and common fields to the EmClass if they are not yet created (and raises warning if existing common fields are outdated given the one describe in EditorialModel.classtypes
|
36
|
41
|
# @throw EmComponentCheckError if fails
|
|
42
|
+ #
|
|
43
|
+ # @warning If default parameters of EmField constructor changes this method can be broken
|
37
|
44
|
def check(self):
|
|
45
|
+ # Checks that this class is up to date given the common_fields described in EditorialModel.classtypes
|
|
46
|
+ # if not just print a warning, don't raise an Exception
|
|
47
|
+ for field in self.fields():
|
|
48
|
+ if field.name in EditorialModel.classtypes.common_fields:
|
|
49
|
+ # Building fieltypes options to match the ones stored in EditorialModel.classtypes
|
|
50
|
+ ftype_opts = field.fieldtype_options()
|
|
51
|
+ ftype_opts['fieldtype'] = field.fieldtype
|
|
52
|
+
|
|
53
|
+ ctype_opts = EditorialModel.classtypes.common_fields[field.name]
|
|
54
|
+ #Adding default value for options nullable, uniq and internal to fieldtypes options stored in classtypes
|
|
55
|
+ defaults = { 'nullable': False, 'uniq': False, 'internal': False}
|
|
56
|
+ for opt_name, opt_val in defaults.items():
|
|
57
|
+ if opt_name not in ctype_opts:
|
|
58
|
+ ctype_opts[opt_name] = opt_val
|
|
59
|
+
|
|
60
|
+ if ftype_opts != ctype_opts:
|
|
61
|
+ # If options mismatch produce a diff and display a warning
|
|
62
|
+ ctype_opts = [ "%s: %s\n"%(repr(k), repr(ctype_opts[k])) for k in sorted(ctype_opts.keys())]
|
|
63
|
+ ftype_opts = [ "%s: %s\n"%(repr(k), repr(ftype_opts[k])) for k in sorted(ftype_opts.keys())]
|
|
64
|
+
|
|
65
|
+ diff_list = difflib.unified_diff(
|
|
66
|
+ ctype_opts,
|
|
67
|
+ ftype_opts,
|
|
68
|
+ fromfile="Classtypes.%s" % field.name,
|
|
69
|
+ tofile="CurrentModel_%s.%s" % (self.name, field.name)
|
|
70
|
+ )
|
|
71
|
+ diff = ''.join(diff_list)
|
|
72
|
+ warnings.warn("LOADED MODEL IS OUTDATED !!! The common_fields defined in classtypes differs from the fields in this model.\nHere is a diff : \n%s" % diff)
|
|
73
|
+
|
38
|
74
|
for fname in self.default_fields_list().keys():
|
39
|
75
|
if fname not in [f.name for f in self.fields()]:
|
40
|
76
|
self.model.add_default_class_fields(self.uid)
|