|
@@ -2,43 +2,49 @@
|
2
|
2
|
|
3
|
3
|
from EditorialModel.components import EmComponent, EmComponentNotExistError
|
4
|
4
|
from Database import sqlutils
|
|
5
|
+from Database.sqlwrapper import SqlWrapper
|
|
6
|
+from Database.sqlquerybuilder import SqlQueryBuilder
|
5
|
7
|
|
6
|
8
|
import sqlalchemy as sql
|
7
|
9
|
|
8
|
10
|
import EditorialModel
|
|
11
|
+import logging
|
|
12
|
+
|
|
13
|
+logger = logging.getLogger('Lodel2.EditorialModel')
|
9
|
14
|
|
10
|
15
|
"""Represent one data for a lodel2 document"""
|
11
|
16
|
class EmField(EmComponent):
|
12
|
17
|
|
13
|
18
|
table = 'em_field'
|
14
|
19
|
|
|
20
|
+ ## __init__ (Function)
|
|
21
|
+ #
|
|
22
|
+ # Instanciates an EmField object with data fetched from the database
|
|
23
|
+ #
|
|
24
|
+ # @param id_or_name str\int: Identifier of the EmField (global_id or name)
|
|
25
|
+ # @throw TypeError
|
|
26
|
+ # @see EmComponent::__init__()
|
15
|
27
|
def __init__(self, id_or_name):
|
16
|
|
- """ Instanciate an EmField with data fetched from db
|
17
|
|
- @param id_or_name str|int: Identify the EmType by name or by global_id
|
18
|
|
- @throw TypeError
|
19
|
|
- @see EmComponent::__init__()
|
20
|
|
- """
|
21
|
28
|
self.table = EmField.table
|
22
|
29
|
super(EmField, self).__init__(id_or_name)
|
23
|
30
|
|
|
31
|
+ ## Create (Function)
|
|
32
|
+ #
|
|
33
|
+ # Creates a new EmField and instanciates it
|
|
34
|
+ #
|
|
35
|
+ # @static
|
|
36
|
+ #
|
|
37
|
+ # @param name str: The name of the new Type
|
|
38
|
+ # @param em_fieldgroup EmFieldGroup: The new field will belong to this fieldgroup
|
|
39
|
+ # @param em_fieldtype EmFieldType: The new field will have this type
|
|
40
|
+ # @param optional bool: Is the field optional ?
|
|
41
|
+ # @param optional bool: Is the field internal ?
|
|
42
|
+ #
|
|
43
|
+ # @throw TypeError
|
|
44
|
+ # @see EmComponent::__init__()
|
|
45
|
+ # @staticmethod
|
24
|
46
|
@classmethod
|
25
|
47
|
def create(c, name, em_fieldgroup, em_fieldtype, optional=True, internal=False):
|
26
|
|
- """ Create a new EmField and instanciate it
|
27
|
|
- @static
|
28
|
|
-
|
29
|
|
- @param name str: The name of the new Type
|
30
|
|
- @param em_fieldgroup EmFieldGroup: The new field will belong to this fieldgroup
|
31
|
|
- @param ml_repr MlString|None: Multilingual representation of the type
|
32
|
|
- @param ml_help MlString|None: Multilingual help for the type
|
33
|
|
- @param The string|None: filename of the icon
|
34
|
|
-
|
35
|
|
- @param optional bool: Is the field optional ?
|
36
|
|
- @param internal bool: Is the field internal?
|
37
|
|
-
|
38
|
|
- @throw TypeError
|
39
|
|
- @see EmComponent::__init__()
|
40
|
|
- @staticmethod
|
41
|
|
- """
|
42
|
48
|
try:
|
43
|
49
|
exists = EmField(name)
|
44
|
50
|
except EmComponentNotExistError:
|
|
@@ -50,13 +56,73 @@ class EmField(EmComponent):
|
50
|
56
|
'optional' : 1 if optional else 0,
|
51
|
57
|
'internal' : 1 if internal else 0,
|
52
|
58
|
}
|
53
|
|
- return super(EmField,c).create(values)
|
|
59
|
+
|
|
60
|
+ createdField = super(EmField,c).create(values)
|
|
61
|
+ if createdField:
|
|
62
|
+ # The field was created, we then add its column in the corresponding class' table
|
|
63
|
+ is_field_column_added = EmField.addFieldColumnToClassTable(createdField)
|
|
64
|
+ if is_field_column_added:
|
|
65
|
+ return createdField
|
|
66
|
+
|
|
67
|
+ exists = createdField
|
54
|
68
|
|
55
|
69
|
return exists
|
56
|
70
|
|
57
|
71
|
|
58
|
|
- """ Use dictionary (from database) to populate the object
|
59
|
|
- """
|
|
72
|
+ ## addFieldColumnToClassTable (Function)
|
|
73
|
+ #
|
|
74
|
+ # Adds a column representing the field in its class' table
|
|
75
|
+ #
|
|
76
|
+ # @static
|
|
77
|
+ #
|
|
78
|
+ # @param emField EmField: the object representing the field
|
|
79
|
+ # @return True in case of success, False if not
|
|
80
|
+ @classmethod
|
|
81
|
+ def addFieldColumnToClassTable(cls, emField):
|
|
82
|
+ field_type = EditorialModel.fieldtypes.get_field_type(emField.em_fieldtype)
|
|
83
|
+ field_sqlalchemy_args = field_type.sqlalchemy_args()
|
|
84
|
+ field_sqlalchemy_args['name'] = emField.name
|
|
85
|
+ field_sqlalchemy_column_object = sqlwrapper.createColumn(**field_sqlalchemy_args)
|
|
86
|
+ field_uid = emField.uid
|
|
87
|
+ field_class_table = emField.get_class_table()
|
|
88
|
+ return sqlwrapper.addColumnObject(tname=field_class_table, column=field_sqlalchemy_column_object)
|
|
89
|
+
|
|
90
|
+ ## get_class_table (Function)
|
|
91
|
+ #
|
|
92
|
+ # Gets the name of the table of the class corresponding to the field
|
|
93
|
+ #
|
|
94
|
+ # @return Name of the table
|
|
95
|
+ def get_class_table(self):
|
|
96
|
+ return self._get_class_tableDb()
|
|
97
|
+
|
|
98
|
+ ## _get_class_tableDb (Function)
|
|
99
|
+ #
|
|
100
|
+ # Executes a request to the database to get the name of the table in which to add the field
|
|
101
|
+ #
|
|
102
|
+ # @return Name of the table
|
|
103
|
+ def _get_class_tableDb(self):
|
|
104
|
+ dbe = self.getDbE()
|
|
105
|
+ uidtable = sql.Table('uids', sqlutils.meta(dbe))
|
|
106
|
+ conn = dbe.connect()
|
|
107
|
+ sql_wrapper = SqlWrapper(read_db='default', write_db='default', alchemy_logs=False)
|
|
108
|
+ columns=('table')
|
|
109
|
+ query_builder = SqlQueryBuilder(sql_wrapper,'uids')
|
|
110
|
+ query_builder.Select(columns)
|
|
111
|
+ query_builder.From('uids')
|
|
112
|
+ query_builder.Where('uids.uid=%s' % self.uid)
|
|
113
|
+
|
|
114
|
+ records = query.Execute().fetchall()
|
|
115
|
+ table_records = []
|
|
116
|
+ for record in records:
|
|
117
|
+ table_records.append(dict(zip(record.keys(), record)))
|
|
118
|
+ table_record = table_records[0]
|
|
119
|
+ table_name = table_record['table']
|
|
120
|
+
|
|
121
|
+ return table_name
|
|
122
|
+
|
|
123
|
+ ## Populate (Function)
|
|
124
|
+ #
|
|
125
|
+ # Sets the object's properties using the values from the database
|
60
|
126
|
def populate(self):
|
61
|
127
|
row = super(EmField, self).populate()
|
62
|
128
|
self.em_fieldgroup = EditorialModel.fieldgroups.EmFieldGroup(int(row.fieldgroup_id))
|
|
@@ -67,6 +133,11 @@ class EmField(EmComponent):
|
67
|
133
|
self.rel_to_type_id = EditorialModel.fieldtypes.EmFieldType(int(row.rel_to_type_id)) if row.rel_to_type_id else None
|
68
|
134
|
self.rel_field_id = EmField(int(row.rel_field_id)) if row.rel_field_id else None
|
69
|
135
|
|
|
136
|
+ ## Save (Function)
|
|
137
|
+ #
|
|
138
|
+ # Saves the properties of the object as a record in the database
|
|
139
|
+ #
|
|
140
|
+ # @return True in case of success, False if not
|
70
|
141
|
def save(self):
|
71
|
142
|
# should not be here, but cannot see how to do this
|
72
|
143
|
if self.name is None:
|