Browse Source

Modification to be less dependent to django + logging in db wrapper

Yann Weber 9 years ago
parent
commit
ebdb512500

+ 11
- 4
Database/sqlwrapper.py View File

5
 from django.conf import settings
5
 from django.conf import settings
6
 import re
6
 import re
7
 
7
 
8
+import logging as logger #TODO hack to replace the standarts use of logging in django
9
+logger.getLogger().setLevel('DEBUG')
10
+
8
 class SqlWrapper(object):
11
 class SqlWrapper(object):
9
 
12
 
10
     def __init__(self):
13
     def __init__(self):
155
         try:
158
         try:
156
             table.create(self.get_write_engine())
159
             table.create(self.get_write_engine())
157
             return True
160
             return True
158
-        except:
161
+        except Exception as e:
159
             # TODO Ajuster le code d'erreur à retourner
162
             # TODO Ajuster le code d'erreur à retourner
163
+            logger.warning("Error creating table : "+str(e))
160
             return False
164
             return False
161
 
165
 
162
 
166
 
187
             table.drop(db, checkfirst=True)
191
             table.drop(db, checkfirst=True)
188
             db.close()
192
             db.close()
189
             return True
193
             return True
190
-        except:
194
+        except Exception as e:
191
             # TODO ajuster le code d'erreur
195
             # TODO ajuster le code d'erreur
196
+            logger.warning("Error droping table : "+str(e))
192
             return False
197
             return False
193
 
198
 
194
 
199
 
277
             sqlresult = self.execute(update_object, sqlsettings.ACTION_TYPE_WRITE)
282
             sqlresult = self.execute(update_object, sqlsettings.ACTION_TYPE_WRITE)
278
             updated_lines_count = sqlresult.rowcount
283
             updated_lines_count = sqlresult.rowcount
279
 
284
 
280
-        except DataError:
285
+        except DataError as e:
281
             # TODO Voir si on garde "-1" ou si on place un "None" ou un "False"
286
             # TODO Voir si on garde "-1" ou si on place un "None" ou un "False"
287
+            logger.warning("Error updating database : "+str(e))
282
             updated_lines_count = -1
288
             updated_lines_count = -1
283
 
289
 
284
         return updated_lines_count
290
         return updated_lines_count
369
             records = sqlresult.fetchall()
375
             records = sqlresult.fetchall()
370
             for record in records:
376
             for record in records:
371
                 selected_lines.append(dict(zip(record.keys(), record)))
377
                 selected_lines.append(dict(zip(record.keys(), record)))
372
-        except:
378
+        except Exception as e:
379
+            logger.debug("Error selecting in database : "+str(e))
373
             selected_lines = None
380
             selected_lines = None
374
 
381
 
375
         return selected_lines
382
         return selected_lines

+ 4
- 0
EditorialModel/lib/component.py View File

6
 """
6
 """
7
 
7
 
8
 from Lodel.utils.mlstring import MlString
8
 from Lodel.utils.mlstring import MlString
9
+import logging
10
+
11
+logger = logging.getLogger('Lodel2.EditorialModel')
9
 
12
 
10
 class EmComponent(object):
13
 class EmComponent(object):
11
 
14
 
14
         @exception TypeError
17
         @exception TypeError
15
     """
18
     """
16
     def __init__(self, id_or_name):
19
     def __init__(self, id_or_name):
20
+        logger.debug('Instanciation : '+str(id_or_name))
17
         if self is EmComponent:
21
         if self is EmComponent:
18
             raise EnvironmentError('Abstract class')
22
             raise EnvironmentError('Abstract class')
19
         if type(id_or_name) is int:
23
         if type(id_or_name) is int:

+ 3
- 1
EditorialModel/test/test_component.py View File

1
-from django.test import TestCase
1
+#from django.test import TestCase
2
+from unittest import TestCase
2
 from EditorialModel.lib.component import EmComponent
3
 from EditorialModel.lib.component import EmComponent
3
 
4
 
4
 class ComponentTestCase(TestCase):
5
 class ComponentTestCase(TestCase):
6
     def test_component_instanciate_with_numeric_id(self):
7
     def test_component_instanciate_with_numeric_id(self):
7
         testComp = EmComponent(2)
8
         testComp = EmComponent(2)
8
         self.assertEqual(testComp.id, 2)
9
         self.assertEqual(testComp.id, 2)
10
+

+ 22
- 1
Lodel/settings/defaults.py View File

26
 
26
 
27
 ALLOWED_HOSTS = []
27
 ALLOWED_HOSTS = []
28
 
28
 
29
+LOGGING = {
30
+    'version': 1,
31
+    'disable_existing_loggers': False,
32
+    'handlers': {
33
+        'console': {
34
+            'class': 'logging.StreamHandler',
35
+            'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
36
+        },
37
+    },
38
+    'loggers': {
39
+        'django': {
40
+            'handlers': ['console'],
41
+            'propagate': True,
42
+            'level': 'DEBUG',
43
+        },
44
+        'Lodel2.database': {
45
+            'handlers': ['console'],
46
+            'level': 'DEBUG',
47
+            'propagate': True,
48
+        },
49
+    },
50
+}
29
 
51
 
30
 # Application definition
52
 # Application definition
31
 
53
 
55
 
77
 
56
 # Database
78
 # Database
57
 # https://docs.djangoproject.com/en/1.7/ref/settings/#databases
79
 # https://docs.djangoproject.com/en/1.7/ref/settings/#databases
58
-
59
 DATABASES = {
80
 DATABASES = {
60
     'default': {
81
     'default': {
61
         'ENGINE': 'django.db.backends.sqlite3',
82
         'ENGINE': 'django.db.backends.sqlite3',

+ 3
- 0
runtest View File

1
+#!/bin/bash
2
+
3
+python -m unittest

+ 6
- 0
test.py View File

1
+from django.conf import settings
2
+import logging
3
+
4
+logger = logging.getLogger('Lodel2.Database')
5
+
6
+logger.debug('Foobar')

Loading…
Cancel
Save