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,12 +52,6 @@ em_object.new_field(    'date_update',
52 52
                         data_handler = 'datetime',
53 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 57
 # Lodel old classtypes #
@@ -304,7 +298,7 @@ index_abstract = em.new_class(
304 298
     help_text = {'eng': 'Abstract class common to each Index classes'},
305 299
     abstract = True,
306 300
     group = index_group,
307
-    datasource = 'dummy2',
301
+    datasources = 'dummy2',
308 302
     parents = em_object)
309 303
 
310 304
 index_name = index_abstract.new_field(
@@ -343,7 +337,7 @@ index_theme = em.new_class(
343 337
         'eng': 'Thematic index',
344 338
         'fre': 'Index thématique'},
345 339
     group = index_group,
346
-    datasource = 'dummy2',
340
+    datasources = 'dummy2',
347 341
     parents = index_abstract)
348 342
 
349 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,6 +8,7 @@ import hashlib
8 8
 from lodel.utils.mlstring import MlString
9 9
 
10 10
 from lodel.editorial_model.exceptions import *
11
+from lodel.leapi.leobject import CLASS_ID_FIELDNAME
11 12
 
12 13
 ##@brief Abstract class to represent editorial model components
13 14
 # @see EmClass EmField
@@ -82,6 +83,19 @@ class EmClass(EmComponent):
82 83
         self.parents = parents
83 84
         ##@brief Stores EmFields instances indexed by field uid
84 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 100
     ##@brief Property that represent a dict of all fields (the EmField defined in this class and all its parents)
87 101
     # @todo use Settings.editorialmodel.groups to determine wich fields should be returned
@@ -118,6 +132,8 @@ class EmClass(EmComponent):
118 132
     # @todo use Settings.editorialmodel.groups to determine wich fields should be returned
119 133
     def fields(self, uid = None, no_parents = False):
120 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 137
         try:
122 138
             return list(fields.values()) if uid is None else fields[uid]
123 139
         except KeyError:

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

@@ -71,3 +71,18 @@ class UniqID(Integer):
71 71
 
72 72
     def _check_data_value(self, value):
73 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,6 +7,10 @@ from lodel import logger
7 7
 from lodel.settings import Settings
8 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 14
 class LeApiErrors(Exception):
11 15
     ##@brief Instanciate a new exceptions handling multiple exceptions
12 16
     # @param msg str : Exception message

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

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

Loading…
Cancel
Save