Browse Source

Fixes #79 (and brokes tests of xml translator)

Adding a field to all non abstract classes to store in DB the name of corresponding LeObject subclass
Yann Weber 8 years ago
parent
commit
e9f17eab6e

+ 2
- 8
em_test.py View File

52
                         data_handler = 'datetime',
52
                         data_handler = 'datetime',
53
                         now_on_update = True,
53
                         now_on_update = True,
54
 )
54
 )
55
-em_object.new_field(    'classname',
56
-                        display_name = 'Class name',
57
-                        group = base_group,
58
-                        data_handler = 'varchar',
59
-                        immutable = True
60
-)
61
 
55
 
62
 ########################
56
 ########################
63
 # Lodel old classtypes #
57
 # Lodel old classtypes #
304
     help_text = {'eng': 'Abstract class common to each Index classes'},
298
     help_text = {'eng': 'Abstract class common to each Index classes'},
305
     abstract = True,
299
     abstract = True,
306
     group = index_group,
300
     group = index_group,
307
-    datasource = 'dummy2',
301
+    datasources = 'dummy2',
308
     parents = em_object)
302
     parents = em_object)
309
 
303
 
310
 index_name = index_abstract.new_field(
304
 index_name = index_abstract.new_field(
343
         'eng': 'Thematic index',
337
         'eng': 'Thematic index',
344
         'fre': 'Index thématique'},
338
         'fre': 'Index thématique'},
345
     group = index_group,
339
     group = index_group,
346
-    datasource = 'dummy2',
340
+    datasources = 'dummy2',
347
     parents = index_abstract)
341
     parents = index_abstract)
348
 
342
 
349
 index_theme_theme = index_abstract.new_field(
343
 index_theme_theme = index_abstract.new_field(

BIN
examples/em_test.pickle View File


+ 16
- 0
lodel/editorial_model/components.py View File

8
 from lodel.utils.mlstring import MlString
8
 from lodel.utils.mlstring import MlString
9
 
9
 
10
 from lodel.editorial_model.exceptions import *
10
 from lodel.editorial_model.exceptions import *
11
+from lodel.leapi.leobject import CLASS_ID_FIELDNAME
11
 
12
 
12
 ##@brief Abstract class to represent editorial model components
13
 ##@brief Abstract class to represent editorial model components
13
 # @see EmClass EmField
14
 # @see EmClass EmField
82
         self.parents = parents
83
         self.parents = parents
83
         ##@brief Stores EmFields instances indexed by field uid
84
         ##@brief Stores EmFields instances indexed by field uid
84
         self.__fields = dict() 
85
         self.__fields = dict() 
86
+        
87
+        #Adding common field
88
+        if not self.abstract:
89
+            self.new_field(
90
+                CLASS_ID_FIELDNAME,
91
+                display_name = {
92
+                    'eng': "LeObject subclass identifier",
93
+                    'fre': "Identifiant de la class fille de LeObject"},
94
+                help_text = {
95
+                    'eng': "Allow to create instance of the good class when\
96
+ fetching arbitrary datas from DB"},
97
+                data_handler = 'LeobjectSubclassIdentifier',
98
+                internal = True)
85
     
99
     
86
     ##@brief Property that represent a dict of all fields (the EmField defined in this class and all its parents)
100
     ##@brief Property that represent a dict of all fields (the EmField defined in this class and all its parents)
87
     # @todo use Settings.editorialmodel.groups to determine wich fields should be returned
101
     # @todo use Settings.editorialmodel.groups to determine wich fields should be returned
118
     # @todo use Settings.editorialmodel.groups to determine wich fields should be returned
132
     # @todo use Settings.editorialmodel.groups to determine wich fields should be returned
119
     def fields(self, uid = None, no_parents = False):
133
     def fields(self, uid = None, no_parents = False):
120
         fields = self.__fields if no_parents else self.__all_fields
134
         fields = self.__fields if no_parents else self.__all_fields
135
+        if CLASS_ID_FIELDNAME in fields:
136
+            del(fields[CLASS_ID_FIELDNAME])
121
         try:
137
         try:
122
             return list(fields.values()) if uid is None else fields[uid]
138
             return list(fields.values()) if uid is None else fields[uid]
123
         except KeyError:
139
         except KeyError:

+ 15
- 0
lodel/leapi/datahandlers/datas.py View File

71
 
71
 
72
     def _check_data_value(self, value):
72
     def _check_data_value(self, value):
73
         return value, None
73
         return value, None
74
+
75
+class LeobjectSubclassIdentifier(Varchar):
76
+    
77
+    help = 'Datahandler designed to handle LeObject subclass identifier in DB'
78
+    base_type = 'varchar'
79
+
80
+    def __init__(self, **kwargs):
81
+        if 'internal' in kwargs and not kwargs['internal']:
82
+            raise RuntimeError(self.__class__.__name__+" datahandler can only \
83
+be internal")
84
+        kwargs['internal'] = True
85
+        super().__init__(**kwargs)
86
+
87
+    def _check_data_consistency(self, emcomponent, fname, datas):
88
+        datas[fname] = emcomponent.__class__.__name__

+ 4
- 0
lodel/leapi/leobject.py View File

7
 from lodel.settings import Settings
7
 from lodel.settings import Settings
8
 from lodel.settings.utils import SettingsError
8
 from lodel.settings.utils import SettingsError
9
 
9
 
10
+##@brief Stores the name of the field present in each LeObject that indicates
11
+#the name of LeObject subclass represented by this object
12
+CLASS_ID_FIELDNAME = "classname"
13
+
10
 class LeApiErrors(Exception):
14
 class LeApiErrors(Exception):
11
     ##@brief Instanciate a new exceptions handling multiple exceptions
15
     ##@brief Instanciate a new exceptions handling multiple exceptions
12
     # @param msg str : Exception message
16
     # @param msg str : Exception message

+ 1
- 1
tests/editorial_model/test_model.py View File

24
         grp2 = model.new_group('testgroup2')
24
         grp2 = model.new_group('testgroup2')
25
         grp2.add_components((cls2, c1f2, c2f1, c2f2))
25
         grp2.add_components((cls2, c1f2, c2f1, c2f2))
26
         grp2.add_dependencie(grp1)
26
         grp2.add_dependencie(grp1)
27
-        e_hash = 0x476f5917265b6fbd06d94b81f4e9b402
27
+        e_hash = 0x250eab75e782e51bbf212f47c6159571
28
         self.assertEqual(model.d_hash(), e_hash)
28
         self.assertEqual(model.d_hash(), e_hash)
29
 
29
 
30
         c2f1.uid = 'foobar'
30
         c2f1.uid = 'foobar'

Loading…
Cancel
Save