Browse Source

Merge branch 't12'

Roland Haroutiounian 9 years ago
parent
commit
9140b1b5b1
3 changed files with 142 additions and 20 deletions
  1. 8
    19
      EditorialModel/fields.py
  2. 104
    0
      EditorialModel/fields_types.py
  3. 30
    1
      EditorialModel/test/test_field.py

+ 8
- 19
EditorialModel/fields.py View File

2
 
2
 
3
 from EditorialModel.components import EmComponent, EmComponentNotExistError
3
 from EditorialModel.components import EmComponent, EmComponentNotExistError
4
 from EditorialModel.fieldtypes import *
4
 from EditorialModel.fieldtypes import *
5
+from EditorialModel.fields_types import Em_Field_Type
5
 from Database import sqlutils
6
 from Database import sqlutils
6
 from Database.sqlwrapper import SqlWrapper
7
 from Database.sqlwrapper import SqlWrapper
7
 from Database.sqlquerybuilder import SqlQueryBuilder
8
 from Database.sqlquerybuilder import SqlQueryBuilder
20
 
21
 
21
     table = 'em_field'
22
     table = 'em_field'
22
     _fields = [
23
     _fields = [
23
-        ('fieldtype', EmField_char()),
24
-        ('fieldgroup_id', EmField_integer()),
25
-        ('rel_to_type_id', EmField_integer()),
26
-        ('rel_field_id', EmField_integer()),
27
-        ('optional', EmField_boolean()),
28
-        ('internal', EmField_boolean()),
29
-        ('icon', EmField_integer())
24
+        ('fieldtype', EmField_char),
25
+        ('fieldgroup_id', EmField_integer),
26
+        ('rel_to_type_id', EmField_integer),
27
+        ('rel_field_id', EmField_integer),
28
+        ('optional', EmField_boolean),
29
+        ('internal', EmField_boolean),
30
+        ('icon', EmField_integer)
30
     ]
31
     ]
31
 
32
 
32
-    ## __init__ (Function)
33
-    #
34
-    # Instanciates an EmField object with data fetched from the database
35
-    #
36
-    # @param id_or_name str\int: Identifier of the EmField (global_id or name)
37
-    # @throw TypeError
38
-    # @see EmComponent::__init__()
39
-    def __init__(self, id_or_name):
40
-        self.table = EmField.table
41
-        self._fields = self.__class__._fields
42
-        super(EmField, self).__init__(id_or_name)
43
-
44
     ## Create (Function)
33
     ## Create (Function)
45
     #
34
     #
46
     # Creates a new EmField and instanciates it
35
     # Creates a new EmField and instanciates it

+ 104
- 0
EditorialModel/fields_types.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from EditorialModel.fieldtypes import EmField_integer
4
+from EditorialModel.components import EmComponent
5
+
6
+from Database import sqlutils
7
+import sqlalchemy as sqla
8
+
9
+import logging
10
+
11
+logger = logging.getLogger('Lodel2.EditorialModel')
12
+
13
+## Em_Field_Type (Class)
14
+#
15
+# Represents an association between a field and a type
16
+class Em_Field_Type(object):
17
+
18
+    table = 'em_field_type'
19
+    _fields = [('type_id', EmField_integer),('field_id', EmField_integer)]
20
+
21
+    ## __init__ (Function)
22
+    #
23
+    # Instanciates an Em_Field_Type object with data fetched from the database
24
+    #
25
+    # @param type_id integer: Identifier of the type
26
+    # @param field_id integer: Identifier of the field
27
+    def __init__(self, type_id, field_id):
28
+        self.table = Em_Field_Type.table
29
+        self._fields = self.__class__._fields
30
+        self.type_id = type_id
31
+        self.field_id = field_id
32
+
33
+    ## Create (Function)
34
+    #
35
+    # Creates a relation between a field and a type
36
+    #
37
+    # @static
38
+    #
39
+    # @param emType EmType: Object representing the Type
40
+    # @param emField EmField: Object representing the Field
41
+    # @return Em_Field_Type object
42
+    @classmethod
43
+    def create(cls, emType, emField):
44
+        values = {
45
+                'type_id': emType.uid,
46
+                'field_id': emField.uid
47
+        }
48
+
49
+        createdRelation = Em_Field_Type._createDb(**values)
50
+        return createdRelation
51
+
52
+    @classmethod
53
+    def _createDb(cls, **kwargs):
54
+        dbe = EmComponent.getDbE()
55
+        conn = dbe.connect()
56
+        table = sqla.Table(cls.table, sqlutils.meta(dbe))
57
+        req = table.insert(kwargs)
58
+        res = conn.execute(req)
59
+        conn.close()
60
+        return Em_Field_Type(kwargs['type_id'], kwargs['field_id'])
61
+
62
+    ## Delete (Function)
63
+    #
64
+    # Deletes a relation between a field and a type
65
+    #
66
+    # @return Boolean
67
+    def delete(self):
68
+        return _deleteDb()
69
+
70
+    def _deleteDb(self):
71
+        dbe = EmComponent.getDbe()
72
+        table = sqla.Table(self.table, sqlutils.meta(dbe))
73
+        req = table.Delete().Where(table.c.type_id==self.type_id).Where(table.c.field_id==self.field_id)
74
+        conn = dbe.connect()
75
+        try:
76
+            conn.execute(req)
77
+            res = True
78
+        except:
79
+            res = False
80
+        conn.close()
81
+
82
+        return res
83
+
84
+    ## Exists (Function)
85
+    #
86
+    # Checks if a the relation exists in the database
87
+    #
88
+    # @return True if success, False if failure
89
+    def exists(self):
90
+        return _existsDb()
91
+
92
+    ## _ExistsDb (Function)
93
+    #
94
+    # Queries the database to see if a relation exists or not
95
+    #
96
+    # @return True if success, False if failure
97
+    def _existsDb(self):
98
+        dbe = EmComponent.getDbe()
99
+        table = sqla.Table(self.table, sqlutils.meta(dbe))
100
+        req = table.Select().Where(table.c.type_id==self.type_id).Where(table.c.field_id==self.field_id)
101
+        conn = dbe.connect()
102
+        res = conn.execute(req).fetchall()
103
+        conn.close()
104
+        return len(res)>0

+ 30
- 1
EditorialModel/test/test_field.py View File

12
 from EditorialModel.classtypes import EmClassType
12
 from EditorialModel.classtypes import EmClassType
13
 from EditorialModel.types import EmType
13
 from EditorialModel.types import EmType
14
 from EditorialModel.fieldgroups import EmFieldGroup
14
 from EditorialModel.fieldgroups import EmFieldGroup
15
+from EditorialModel.fields_types import Em_Field_Type
15
 from EditorialModel.fieldtypes import *
16
 from EditorialModel.fieldtypes import *
16
 
17
 
17
 from Database.sqlsetup import SQLSetup
18
 from Database.sqlsetup import SQLSetup
63
 
64
 
64
         pass
65
         pass
65
 
66
 
67
+
68
+    ## Get_Field_Type_Record (Function)
69
+    #
70
+    # Returns associations between field and type from the em_field_type table
71
+    #
72
+    # @param field EmField: Field object
73
+    # @param type EmType: Type object
74
+    # @return list of found associations
75
+    def get_field_type_record(self, field, type):
76
+        return self._get_field_type_record_Db(field, type)
77
+
78
+    ## _Get_Field_Type_Record_Db (Function)
79
+    #
80
+    # Queries the database to get the record from the em_field_type table corresponding to a given field and type
81
+    # @param field EmField: Field object
82
+    # @param type EmType: Type object
83
+    # @return found associations
84
+    def _get_field_type_record_Db(self, field, type):
85
+        sqlwrapper = SqlWrapper(read_db='default', write_db='default', alchemy_logs=False)
86
+        sql_builder = SqlQueryBuilder(sql_wrapper, 'em_field_type')
87
+        sql_builder.Select().From('em_field_type').Where('em_field_type.field_id=%s' % field.uid).Where('em_field_type.type_id=%s' % type.uid)
88
+        records = sql_builder.Execute().fetchall()
89
+        field_type_records = []
90
+        for record in records:
91
+            field_type_records.append(dict(zip(record.keys(),record)))
92
+
93
+        return field_type_records
94
+
95
+
66
     ## Get_Field_Records (Function)
96
     ## Get_Field_Records (Function)
67
     #
97
     #
68
     # Returns the list of fields corresponding to a given uid
98
     # Returns the list of fields corresponding to a given uid
142
         field_column = sqla.Column(**field_column_args)
172
         field_column = sqla.Column(**field_column_args)
143
         self.assertIn(field_column,field_table_columns)
173
         self.assertIn(field_column,field_table_columns)
144
         pass
174
         pass
145
-

Loading…
Cancel
Save