Browse Source

Adding default date for date_create and date_update columns

The dates are set in EmComponent::create() for date_create and in EmComponent::save() for date_update
The values are sets to datetime.datetime.utcnow() so we now that EVERY date in the db are in UTC, no matter django, database or even server configurations.
We will have to take care to convert dates from database to django timezone in dates fieldtypes.
Yann Weber 9 years ago
parent
commit
c100dd43f3

+ 4
- 4
Database/sqlsetup.py View File

@@ -20,8 +20,8 @@ class SQLSetup(object):
20 20
             {"name":"string",       "type":"TEXT"},
21 21
             {"name":"help",         "type":"TEXT"},
22 22
             {"name":"rank",         "type":"INTEGER"},
23
-            {"name":"date_update",  "type":"DATE"},
24
-            {"name":"date_create",  "type":"DATE"}
23
+            {"name":"date_create",  "type":"DATETIME"},
24
+            {"name":"date_update",  "type":"DATETIME"},
25 25
         ]
26 26
 
27 27
         # Table listing all objects created by lodel, giving them an unique id
@@ -99,8 +99,8 @@ class SQLSetup(object):
99 99
                 {"name":"string",      "type":"VARCHAR(50)"},
100 100
                 {"name":"class_id",    "type":"INTEGER", "extra":{"foreignkey":"em_class.uid"}},
101 101
                 {"name":"type_id",     "type":"INTEGER", "extra":{"foreignkey":"em_type.uid"}},
102
-                {"name":"date_update", "type":"DATE"},
103
-                {"name":"date_create", "type":"DATE"},
102
+                {"name":"date_create", "type":"DATETIME"},
103
+                {"name":"date_update", "type":"DATETIME"},
104 104
                 {"name":"history",     "type":"TEXT"}
105 105
             ]
106 106
         }

+ 2
- 4
Database/sqlwrapper.py View File

@@ -104,10 +104,8 @@ class SqlWrapper(object):
104 104
 
105 105
     def renewMetaData(self):
106 106
         """ (Re)load the database schema """
107
-        if self.metadata == None:
108
-            self.metadata = sqla.MetaData(bind=self.r_engine, reflect=True)
109
-        else:
110
-            self.metadata = sqla.MetaData(bind=self.r_engine, reflect=True)
107
+        self.metadata = sqla.MetaData(bind=self.r_engine, reflect=True)
108
+        self.metadata.reflect()
111 109
 
112 110
     @property
113 111
     def rconn(self):

+ 6
- 6
EditorialModel/classes.py View File

@@ -40,14 +40,14 @@ class EmClass(EmComponent):
40 40
     @classmethod
41 41
     def _createDb(c, name, class_type):
42 42
         """ Do the db querys for EmClass::create() """
43
-        uid = c.newUid()
43
+
44
+        #Create a new entry in the em_class table
45
+        values = { 'name':name, 'classtype':class_type['name'] }
46
+        resclass = super(EmClass,c).create(values)
47
+
44 48
 
45 49
         dbe = c.getDbE()
46 50
         conn = dbe.connect()
47
-        #Create a new entry in the em_class table
48
-        dbclass = sql.Table(c.table, sqlutils.meta(dbe))
49
-        req = dbclass.insert().values(uid = uid, name=name, classtype=class_type['name'])
50
-        res = conn.execute(req)
51 51
 
52 52
         #Create a new table storing LodelObjects of this EmClass
53 53
         meta = sql.MetaData()
@@ -57,7 +57,7 @@ class EmClass(EmComponent):
57 57
 
58 58
         conn.close()
59 59
 
60
-        return EmClass(name)
60
+        return resclass
61 61
 
62 62
 
63 63
     def populate(self):

+ 29
- 2
EditorialModel/components.py View File

@@ -5,6 +5,8 @@
5 5
     @see EmClass, EmType, EmFieldGroup, EmField
6 6
 """
7 7
 
8
+import datetime
9
+
8 10
 from Lodel.utils.mlstring import MlString
9 11
 import logging
10 12
 import sqlalchemy as sql
@@ -79,18 +81,43 @@ class EmComponent(object):
79 81
             raise EmComponentNotExistError("No component found with "+('name '+self.name if self.id == None else 'id '+self.id ))
80 82
 
81 83
         return res
84
+    
85
+    ## Insert a new component in the database
86
+    # This function create and assign a new UID and handle the date_create value
87
+    # @param values The values of the new component
88
+    # @return An instance of the created component
89
+    #
90
+    # @todo Check that the query didn't failed
91
+    @classmethod
92
+    def create(c, values):
93
+        values['uid'] = c.newUid()
94
+        values['date_update'] = values['date_create'] = datetime.datetime.utcnow()
95
+
96
+        dbe = c.getDbE()
97
+        conn = dbe.connect()
98
+        table = sql.Table(c.table, sqlutils.meta(dbe))
99
+        req = table.insert(values)
100
+        res = conn.execute(req) #Check res?
101
+        conn.close()
102
+        return c(values['name']) #Maybe no need to check res because this would fail if the query failed
103
+        
82 104
 
83 105
     """ write the representation of the component in the database
84 106
         @return bool
85 107
     """
86 108
     def save(self, values):
109
+
87 110
         values['name'] = self.name
88 111
         values['rank'] = self.rank
89
-        values['date_update'] = self.date_update
90
-        values['date_create'] = self.date_create
112
+        values['date_update'] = datetime.datetime.utcnow()
91 113
         values['string'] = str(self.string)
92 114
         values['help']= str(self.help)
93 115
 
116
+        #Don't allow creation date overwritting
117
+        if 'date_create' in values:
118
+            del values['date_create']
119
+            logger.warning("date_create supplied for save, but overwritting of date_create not allowed, the date will not be changed")
120
+
94 121
         self._saveDb(values)
95 122
 
96 123
     def _saveDb(self, values):

+ 3
- 21
EditorialModel/fieldgroups.py View File

@@ -24,8 +24,8 @@ class EmFieldGroup(EmComponent):
24 24
         self.table = EmFieldGroup.table
25 25
         super(EmFieldGroup, self).__init__(id_or_name)
26 26
 
27
-    @staticmethod
28
-    def create(name, em_class):
27
+    @classmethod
28
+    def create(c, name, em_class):
29 29
         """ Create a new EmFieldGroup, save it and instanciate it
30 30
 
31 31
             @param name str: The name of the new fielgroup
@@ -34,28 +34,10 @@ class EmFieldGroup(EmComponent):
34 34
         try:
35 35
             exists = EmFieldGroup(name)
36 36
         except EmComponentNotExistError:
37
-            return EmFieldGroup._createDb(name, em_class)
37
+            return super(EmFieldGroup, c).create({'name': name, 'class_id':em_class.id}) #Check the return value ?
38 38
 
39 39
         return exists
40 40
 
41
-    @classmethod
42
-    def _createDb(c,name, em_class):
43
-        """ Make the Db insertion for fieldgroup creation """
44
-        uid = c.newUid()
45
-
46
-        dbe = c.getDbE()
47
-        conn = dbe.connect()
48
-
49
-        req = sql.Table(c.table, sqlutils.meta(dbe)).insert().values(uid=uid, name=name, class_id=em_class.id)
50
-        res = conn.execute(req)
51
-
52
-        conn.close()
53
-
54
-        return EmFieldGroup(name)
55
-        
56
-
57
-        
58
-
59 41
     """ Use dictionary (from database) to populate the object
60 42
     """
61 43
     def populate(self):

+ 3
- 16
EditorialModel/fields.py View File

@@ -21,8 +21,8 @@ class EmField(EmComponent):
21 21
         self.table = EmField.table
22 22
         super(EmField, self).__init__(id_or_name)
23 23
 
24
-    @staticmethod
25
-    def create(name, em_fieldgroup, em_fieldtype, optional=True, internal=False):
24
+    @classmethod
25
+    def create(c, name, em_fieldgroup, em_fieldtype, optional=True, internal=False):
26 26
         """ Create a new EmField and instanciate it
27 27
             @static
28 28
 
@@ -50,23 +50,10 @@ class EmField(EmComponent):
50 50
                 'optional' : 1 if optional else 0,
51 51
                 'internal' : 1 if internal else 0,
52 52
             }
53
-
54
-            return EmField._createDb(values)
53
+            return super(EmField,c).create(values)
55 54
 
56 55
         return exists
57
-    
58
-    @classmethod
59
-    def _createDb(c, values):
60
-        values['uid'] = c.newUid()
61
-
62
-        dbe = c.getDbE()
63
-        conn = dbe.connect()
64
-        req = sql.Table(c.table, sqlutils.meta(dbe)).insert(values=values)
65
-        res = conn.execute(req)
66
-
67
-        conn.close()
68 56
 
69
-        return EmField(values['name'])
70 57
 
71 58
     """ Use dictionary (from database) to populate the object
72 59
     """

+ 4
- 18
EditorialModel/types.py View File

@@ -25,8 +25,8 @@ class EmType(EmComponent):
25 25
         self.table = EmType.table
26 26
         super(EmType, self).__init__(id_or_name)
27 27
 
28
-    @staticmethod
29
-    def create(name, em_class):
28
+    @classmethod
29
+    def create(c, name, em_class):
30 30
         """ Create a new EmType and instanciate it
31 31
 
32 32
             @param name str: The name of the new type
@@ -35,29 +35,15 @@ class EmType(EmComponent):
35 35
             @see EmComponent::__init__()
36 36
 
37 37
             @todo Change the icon param type
38
-            @todo change staticmethod to classmethod
38
+            @todo check that em_class is an EmClass object
39 39
         """
40 40
         try:
41 41
             exists = EmType(name)
42 42
         except EmComponentNotExistError:
43
-            return EmType._createDb(name, em_class)
43
+            return super(EmType, c).create({'name':name, 'class_id': em_class.id})
44 44
 
45 45
         return exists
46 46
 
47
-    @classmethod
48
-    def _createDb(c, name, em_class):
49
-        uid = c.newUid()
50
-
51
-        dbe = c.getDbE()
52
-        conn = dbe.connect()
53
-
54
-        #Insert type in db
55
-        dbtype = sql.Table(c.table, sqlutils.meta(dbe))
56
-        req = dbtype.insert().values(uid=uid, name=name, class_id=em_class.id)
57
-        res = conn.execute(req)
58
-
59
-        return EmType(name)
60
-
61 47
     """ Use dictionary (from database) to populate the object
62 48
     """
63 49
     def populate(self):

Loading…
Cancel
Save