from django.shortcuts import render

# Create your views here.


from system.models import Log
from django.contrib.contenttypes.models import ContentType

# Create your views here.
def sys_log(user, obj, action='edit'):
    '''
    用于将系统内object对像修改内容保存到系统日志
    user: 操作人    (对像实例)
    obj: 修改对像   (对像实例)
    action:'edit'修改; delete' 删除
    '''

    e=False    
    #2;"新建";"新建"
    #3;"修改";"修改"
    #4;"查看";"查看"
    #5;"列表";"列表"
    #6;"删除";"删除"
    if action=='edit':
        # 保存修改前调用，记录修改前后的内容
        d=obj.changed_data
        action_id=3
    elif what=='delete':
        # 执行删除前调用，记录当前数据内容
        d=obj.current_data
        action_id=6
    else:
        e=True
    if not e:
        # 写入系统日志
        l=Log(ancestor_id=user.ancestor_id,company_id=user.company_id,user_id=user.id,            
            contenttype=ContentType.objects.get_for_model(obj),
            object_id=obj.id, action_id=action_id,changed_data=d
        )
        l.save()        
    return
