Browse Source

Start multivalueFieldType implementation (in migrationhandler)

Yann Weber 8 years ago
parent
commit
8211fd5c47

+ 37
- 1
DataSource/MySQL/migrationhandler.py View File

@@ -340,6 +340,17 @@ class MysqlMigrationHandler(DummyMigrationHandler):
340 340
     # @return True if the column was added else return False
341 341
     def _add_column(self, table_name, col_name, col_fieldtype, drop_if_exists=False):
342 342
 
343
+        instr, col_type, col_specs = fieldtypes_utils.fieldtype_db_init(col_fieldtype)
344
+
345
+        if instr == 'table':
346
+            # multivalue field. We are not going to add a column in this table
347
+            # but in corresponding multivalue table
348
+            self._add_column_multivalue(
349
+                                            table_name,
350
+                                            key_infos = col_type,
351
+                                            column_infos = (col_name, col_specs)
352
+                                        )
353
+
343 354
         col_name = utils.column_name(col_name)
344 355
 
345 356
         add_col = """ALTER TABLE {table_name}
@@ -348,7 +359,6 @@ ADD COLUMN {col_name} {col_type} {col_specs};"""
348 359
         etname = utils.escape_idname(table_name)
349 360
         ecname = utils.escape_idname(col_name)
350 361
 
351
-        instr, col_type, col_specs = fieldtypes_utils.fieldtype_db_init(col_fieldtype)
352 362
         if instr is None:
353 363
             return True
354 364
         if instr != "column":
@@ -397,6 +407,32 @@ ADD COLUMN {col_name} {col_type} {col_specs};"""
397 407
 
398 408
         return True
399 409
 
410
+    ## @brief Add a column to a multivalue table
411
+    #
412
+    # Add a column (and create a table if not existing) for storing multivalue
413
+    # datas. (typically i18n)
414
+    # @param ref_table_name str : Referenced table name
415
+    # @param key_infos tuple : tuple(key_name, key_fieldtype)
416
+    # @param column_infos tuple : tuple(col_name, col_fieldtype)
417
+    def _add_column_multivalue(ref_table_name, key_info, column_infos):
418
+        key_name, key_ftype = key_infos
419
+        col_name, col_ftype = column_infos
420
+        table_name = utils.multivalue_table_name(ref_table_name, key_name)
421
+        # table creation
422
+        self._create_table(
423
+                            table_name,
424
+                            key_name,
425
+                            key_ftype,
426
+                            self.db_engine,
427
+                            if_exists = 'nothing'
428
+        )
429
+        # with FK
430
+        self._del_fk(table_name, dst_table_name)
431
+        self._add_fk(table_name, dst_table_name, key_name, utils._object_pk[0])
432
+        # adding the column
433
+        self._add_column(table_name, col_name, col_ftype)
434
+
435
+
400 436
     ## @brief Add a foreign key
401 437
     # @param src_table_name str : The name of the table where we will add the FK
402 438
     # @param dst_table_name str : The name of the table the FK will point on

+ 2
- 0
DataSource/MySQL/utils.py View File

@@ -32,6 +32,8 @@ def object_table_name(class_name):
32 32
 def r2t_table_name(class_name, type_name):
33 33
     return ("%s%s_%s" % (table_preffix['relation'], class_name, type_name)).lower()
34 34
 
35
+def multivalue_table_name(referenced_table_name, key_name):
36
+    return ("%s%s" % (key_name, referenced_table_name))
35 37
 
36 38
 ## @brief Return a column name given a field name
37 39
 # @param field_name : The EmField or LeObject field name

+ 1
- 1
EditorialModel/fieldtypes/char.py View File

@@ -11,5 +11,5 @@ class EmFieldType(SingleValueFieldType):
11 11
     # @brief max_length int : The maximum length of this field
12 12
     def __init__(self, max_length=64, **kwargs):
13 13
         self.max_length = max_length
14
-        super(EmFieldType, self).__init__(**kwargs)
14
+        super().__init__(**kwargs)
15 15
 

+ 1
- 1
EditorialModel/fieldtypes/generic.py View File

@@ -143,7 +143,7 @@ class MultiValueFieldType(GenericFieldType):
143 143
     # @param keyname str : The identifier key name
144 144
     # @param key_fieldtype SingleValueFieldType : A SingleValueFieldType child class instance
145 145
     # @param value_fieldtype SingleValueFieldType : A SingleValueFieldType child class instance
146
-    def __init__(self, internal, keyname, key_fieldtype, value_fieldtype, **args):
146
+    def __init__(self, keyname, key_fieldtype, value_fieldtype, internal = False, **args):
147 147
         super().__init__(internal)
148 148
         ## stores the keyname
149 149
         self.keyname = keyname

+ 14
- 0
EditorialModel/fieldtypes/i18n.py View File

@@ -0,0 +1,14 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+from .generic import MultiValueFieldType
4
+from . import char
5
+
6
+class EmFieldType(MutliValueFieldType):
7
+    
8
+    help = 'Fieldtype designed to handle translations'
9
+    
10
+    def __init__(self, value_max_length = 64, **args):
11
+        args['keyname'] = 'lang'
12
+        args['key_fieldtype'] = char.EmFieldType(max_length = 4)
13
+        args['value_fieldtype'] = char.EmFieldType(value_max_length)
14
+        super().__init__(**args)

Loading…
Cancel
Save