Commits

Alexander Lyabah committed b3e9939

big commit before kyivpy

Comments (0)

Files changed (8)

djucsvlog/DjLogger.py

                     self.iget_loglev(),
                     self.iget_func_fields(),
                     self.iget_buffering(),
-                    self.iget_related_folder()
+                    self.iget_related_folder(),
+                    close_row = self.iget_close_row()
                 )
             
     
         return my_settings.LOG_DEF
     def iget_func_fields(self):
         return my_settings.LOG_BASE
+    def iget_close_row(self):
+        return my_settings.LOG_CLOSE_ROW
     
     
     

djucsvlog/components/change_model.py

+from djucsvlog import glog
+from djucsvlog import settings as S
+
+from django.utils.importlib import import_module
+from django.db.models import signals
+
+assert S.CHANGE_MODEL, 'UCSVLOG_CHANGE_MODEL is not defined'
+
+def log_prop_instance(instance,prop):
+    if callable(prop):
+        return prop(instance)
+    val = instance
+    for prop_chank in prop.split('.'):
+        val = getattr(val,prop_chank,None)
+    return val
+
+
+def gen_model_post_save(log_func,item, model):
+    def model_post_save(instance,created,raw,log_func=log_func,item=item, model=model,**kwargs):
+        if raw:
+            return
+        log_func([S.CHANGE_MODEL_LOG_START,\
+                      S.CHANGE_MODEL_LOG_CREATED if created else S.CHANGE_MODEL_LOG_UPDATED,\
+                      item['model']
+                      ] + [log_prop_instance(instance,prop) for prop in item['props']])
+        
+    return model_post_save
+
+def gen_model_post_delete(log_func,item, model):
+    def model_pre_delete(instance,log_func=log_func,item=item, model=model,**kwargs):
+        log_func([S.CHANGE_MODEL_LOG_START, S.CHANGE_MODEL_LOG_DELETED,\
+                      item['model']
+                      ] + [log_prop_instance(instance,prop) for prop in item.get('props_delete',S.CHANGE_MODEL_PROPS_DELETE)])
+        
+    return model_pre_delete
+
+
+for item_num, item in enumerate(S.CHANGE_MODEL):
+    assert 'model' in item, 'model key needs in %s item of UCSVLOG_CHANGE_MODEL' % (item_num, )
+    assert 'props' in item, 'props key needs in %s item of UCSVLOG_CHANGE_MODEL' % (item_num, )
+    app_name, model_name  = item['model'].split('.')
+    
+    model = getattr(import_module(app_name+'.models'),model_name)
+    log_func = getattr(glog,item.get('log',S.CHANGE_MODEL_LOG))
+    
+    
+    l_log = gen_model_post_save(log_func=log_func,item=item, model=model)
+    item['_post_save'] = l_log
+    signals.post_save.connect(l_log,sender=model)
+    
+    
+    l_log = gen_model_post_delete(log_func=log_func,item=item, model=model)
+    item['_post_delete'] = l_log
+    signals.pre_delete.connect(l_log,sender=model)
+    

djucsvlog/fields/exception_info.py

-from ucsvlog.utils import unicoder 
+from ucsvlog.utils import unicoder
+from djucsvlog.fields.tools import json_dump_line
 
 def estr(exception):
     return unicoder(exception)
     vars = frame['vars']
     ret = ''
     for v in vars:
-        ret += ' - %s:%s\n' %(v[0],unicoder_truncate(v[1],my_settings.EXCEPTION_VARS_MAX_LENGTH))
-    return ret
+        ret += '\n'+json_dump_line(v[0]) + ':'+ json_dump_line(unicoder_truncate(v[1],my_settings.EXCEPTION_VARS_MAX_LENGTH))+','
+    return '{'+ ret[:-1]+'}'
 
 def emeta(exception,exR):
     return 'meta'

djucsvlog/fields/request.py

-import json
 import djucsvlog.settings as my_settings
-def readable_dict(dd):
-    if not dd:
-        return '{}'
-    ret = ''
-    for kd,vd in dd.items():
-        ret += '\n"%s":%s,' %(unicode(kd),json.dumps(vd))
-    return '{'+ret[:-1]+'\n}'
+from djucsvlog.fields.tools import json_dump_line, readable_dict, readable_list
+
 
 def path(request):
     return request.path
         return '{}'
     ret = ''
     for field,file in request.FILES.items():
-        ret += '\n"%s":%s,' %(field,json.dumps([file.name,file.content_type,file.size]))
+        ret += '\n"%s":%s,' %(field,readable_list([file.name,file.content_type,file.size]))
     return '{'+ret[:-1]+'\n}'
 
 def cookies(request):
     ret = ''
     for field,file in request.FILES.items():
         store_filename = find_place_to_store(file.name)
-        ret += '\n"%s":%s,' %(field,json.dumps([file.name,store_filename,file.content_type,file.size]))
+        ret += '\n"%s":%s,' %(field,readable_list([file.name,store_filename,file.content_type,file.size]))
         
         fh = open(store_filename,'wb')
         for piece in read_in_chunks(file.file):

djucsvlog/fields/tools.py

+def json_dump_line(line):
+    return '"'+unicode(line).replace('\\','\\\\').replace('\n','\\n').replace('"','\\"')+'"'
+
+
+def readable_dict(dd):
+    if not dd:
+        return '{}'
+    ret = ''
+    for kd,vd in dd.items():
+        ret += '\n'+json_dump_line(kd) + ':' + json_dump_line(vd)+','
+    return '{'+ret[:-1]+'\n}'
+
+def readable_list(ll):
+    return '[' + ','.join(map(json_dump_line,ll)) + ']'

djucsvlog/glog.py

 __all__ = ['glog']
-import settings as my_settings
-from ucsvlog.utils import import_name
+
 from djucsvlog.DjLogger import DjLogger
 glog = DjLogger()
 
-for mod in my_settings.COMPONENTS:
-    try:
-        __import__(mod, {}, {}, [''])
-    except ImportError,e:
-        raise e

djucsvlog/models.py

+import settings as my_settings
+from ucsvlog.utils import import_name
+
+for mod in my_settings.COMPONENTS:
+    try:
+        __import__(mod, {}, {}, [''])
+    except ImportError,e:
+        raise e

djucsvlog/settings.py

 get('THREAD_LOCALS',False)
 get('RELATED_FOLDER',None)
 get('FILE',None)
+get('LOG_CLOSE_ROW',None) # replace it by close mark
 
 
 ##
 get('CLEAR_OLD_FOLDER',None)
 get('CLEAR_OLD_PERIOD',7) # period in days of logs keeping
 get('CLEAR_OLD_ARCHIVE',None) 
+
+
+###        ###
+# COMPONENTS #
+###        ###
+
+##
+# ChangeModel
+
+get('CHANGE_MODEL',None)
+get('CHANGE_MODEL_LOG','log')
+
+get('CHANGE_MODEL_LOG_START','CM')
+get('CHANGE_MODEL_LOG_CREATED','cr')
+get('CHANGE_MODEL_LOG_UPDATED','up')
+get('CHANGE_MODEL_LOG_DELETED','dl')
+
+get('CHANGE_MODEL_PROPS_DELETE',['pk'])