批量删除
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        req_dict = request.session.get("req_dict")
        error=forum.deletes(forum,
            forum,
             req_dict.get("ids")
        )
        if error!=None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def forum_vote(request,id_):
    '''
    浏览点击次数（表属性[browseClick:是/否]，点击字段（clicknum），调用info/detail接口的时候后端自动+1）、投票功能（表属性[vote:是/否]，投票字段（votenum）,调用vote接口后端votenum+1）
统计商品或新闻的点击次数；提供新闻的投票功能
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code}
        data= forum.getbyid(forum, forum, int(id_))
        for i in data:
            votenum=i.get('votenum')
            if votenum!=None:
                params={"id":int(id_),"votenum":votenum+1}
                error=forum.updatebyparams(forum,forum,params)
                if error!=None:
                    msg['code'] = crud_error_code
                    msg['msg'] = error
        return JsonResponse(msg)
def forum_flist(request):
    '''
    查看所有开放的帖列表(无需登录)
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code,  "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
        req_dict = request.session.get("req_dict")
        msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
        msg['data']['pageSize'] = forum.page(forum, forum, req_dict)
        return JsonResponse(msg)
def forum_list_id(request,id_):
    '''
    查看主贴和所有回帖内容(无需登录)
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {"currPage":1,"totalPage":1,"total":1,"pageSize":10,"childs":[]},"id":int(id_)}
        params = {"id":int(id_)}
        datas = forum.retrieve(forum, forum)
        parent_id=0#当前id，也就是parent_id,找到了下一级，把下一级id赋值给current_id，当做下下一级id的paretn_id
        current_id=0
        start_index=0
        #获取parentid
        for index,i in enumerate(datas):
            if i.get('id')==params.get('id'):
                current_id=parent_id=i.get('id')
                start_index=index
                msg['data'].update(i)
                break
        #把疑似回帖的id放进ids数组
        id_data_dict={}#id和详情的键值对
        for i in datas:
            id_data_dict[i.get('id')]=i
        dict1={}#部分层级，只获取上下级关系
        for v in id_data_dict.values():
            parentid_=copy.deepcopy(v.get("parentid"))
            id_=copy.deepcopy(v.get("id"))
            if dict1.get(parentid_)==None:
                dict1[parentid_]=[]
            dict1[parentid_].append(id_)
        childs=[]#msg.data.childs
        #填充第一层回帖
        for i  in dict1.get(parent_id,[]):
            child1=copy.deepcopy(id_data_dict.get(i))
            #填充第二层回帖
            if dict1.get(i)!=None:#判断第二次是否还有回帖
                child2=[]
                for j in  dict1.get(i):
                    child3=copy.deepcopy(id_data_dict.get(j))
                    id_=copy.deepcopy(child3.get("id"))
                    if dict1.get(id_)!=None:#判断第三次是否还有回帖
                        child3["childs"]=[]
                        for k in dict1.get(id_):
                            child4=copy.deepcopy(id_data_dict.get(k))
                            id__=copy.deepcopy(child4.get("id"))
                            if dict1.get(id__)!=None:#判断第四次是否还有回帖
                                child4['childs']=[]
                                for l in dict1.get(id__):
                                    child5=copy.deepcopy(id_data_dict.get(l))
                                    id___=copy.deepcopy(child5.get("id"))
                                    if dict1.get(id___)!=None:#判断第五次是否还有回帖
                                        child5['childs']=[]
                                        for m in dict1.get(id___):
                                            child6=copy.deepcopy(id_data_dict.get(m))
                                            id____=copy.deepcopy(child6.get("id"))
                                            if dict1.get(id____)!=None:#判断第六次是否还有回帖
                                                child6['childs']=[]
                                                child7=copy.deepcopy(id_data_dict.get(m))
                                                child7['childs']=[]
                                                for n in dict1.get(id____):
                                                    child7['childs'].append(id_data_dict.get(n))
                                                child6['childs'].append(child7)
                                        child5['childs'].append(child6)
                                child4["childs"].append(child5)
                        child3["childs"].append(child4)
                    child2.append(child3)
                child1['childs']=child2
            else:
                child1['childs']=None
            childs.append(child1)
        print(childs)
        msg['data']['childs']=childs
        return JsonResponse(msg)
#coding:utf-8
__author__ = "ila"
from django.http import JsonResponse
from django.apps import apps
from django.db.models.aggregates import Count,Sum
from .models import discussxuexiziliao
from util.codes import *
from util.auth import Auth
def discussxuexiziliao_register(request):
    if request.method in ["POST", "GET"]:
        msg = {'code': normal_code, "msg": mes.normal_code}
        req_dict = request.session.get("req_dict")
        error = discussxuexiziliao.createbyreq(discussxuexiziliao, discussxuexiziliao, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = "用户已存在,请勿重复注册!"
        return JsonResponse(msg)
def discussxuexiziliao_login(request):
    if request.method in ["POST", "GET"]:
        msg = {'code': normal_code, "msg": mes.normal_code}
        req_dict = request.session.get("req_dict")
        datas = discussxuexiziliao.getbyparams(discussxuexiziliao, discussxuexiziliao, req_dict)
        if not datas:
            msg['code'] = password_error_code
            msg['msg'] = mes.password_error_code
            return JsonResponse(msg)
        req_dict['id'] = datas[0].get('id')
        return Auth.authenticate(Auth, discussxuexiziliao, req_dict)
def discussxuexiziliao_logout(request):
    if request.method in ["POST", "GET"]:
        msg = {
            "msg": "登出成功",
            "code": 0
        }
        return JsonResponse(msg)
def discussxuexiziliao_resetPass(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code}
        req_dict = request.session.get("req_dict")
        error = discussxuexiziliao.updatebyparams(discussxuexiziliao,discussxuexiziliao, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def discussxuexiziliao_session(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code,"msg": mes.normal_code, "data": {}}
        req_dict={"id":request.session.get('params').get("id")}
        msg['data']  = discussxuexiziliao.getbyparams(discussxuexiziliao, discussxuexiziliao, req_dict)[0]
        return JsonResponse(msg)
def discussxuexiziliao_page(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code,  "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
        req_dict = request.session.get("req_dict")
        #获取全部列名
        columns=  discussxuexiziliao.getallcolumn( discussxuexiziliao, discussxuexiziliao)
        #当前登录用户所在表
        tablename = request.session.get("tablename")
        #当列属性authTable有值(某个用户表)[该列的列名必须和该用户表的登陆字段名一致]，则对应的表有个隐藏属性authTable为”是”，那么该用户查看该表信息时，只能查看自己的
        try:
            __authTables__=discussxuexiziliao.__authTables__
        except:
            __authTables__=None
        if __authTables__!=None and  __authTables__!={}:
            for authColumn,authTable in __authTables__.items():
                if authTable==tablename:
                    params = request.session.get("params")
                    req_dict[authColumn]=params.get(authColumn)
                    break
            #authColumn=list(__authTables__.keys())[0]
            #authTable=__authTables__.get(authColumn)
            # if authTable==tablename:
                #params = request.session.get("params")
                #req_dict[authColumn]=params.get(authColumn)
        '''__authSeparate__此属性为真，params添加userid，后台只查询个人数据'''
        try:
            __authSeparate__=discussxuexiziliao.__authSeparate__
        except:
            __authSeparate__=None
        if __authSeparate__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users" and 'userid' in columns:
                try:
                    req_dict['userid']=request.session.get("params").get("id")
                except:
                    pass
        #当项目属性hasMessage为”是”，生成系统自动生成留言板的表messages，同时该表的表属性hasMessage也被设置为”是”,字段包括userid（用户id），username(用户名)，content（留言内容），reply（回复）
        #接口page需要区分权限，普通用户查看自己的留言和回复记录，管理员查看所有的留言和回复记录
        try:
            __hasMessage__=discussxuexiziliao.__hasMessage__
        except:
            __hasMessage__=None
        if  __hasMessage__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users":
                req_dict["userid"]=request.session.get("params").get("id")
        # 判断当前表的表属性isAdmin,为真则是管理员表
        # 当表属性isAdmin=”是”,刷出来的用户表也是管理员，即page和list可以查看所有人的考试记录(同时应用于其他表)
        error = xuesheng.updatebyparams(xuesheng,xuesheng, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def xuesheng_session(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code,"msg": mes.normal_code, "data": {}}
        req_dict={"id":request.session.get('params').get("id")}
        msg['data']  = xuesheng.getbyparams(xuesheng, xuesheng, req_dict)[0]
        return JsonResponse(msg)
def xuesheng_page(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code,  "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
        req_dict = request.session.get("req_dict")
        #获取全部列名
        columns=  xuesheng.getallcolumn( xuesheng, xuesheng)
        #当前登录用户所在表
        tablename = request.session.get("tablename")
        #当列属性authTable有值(某个用户表)[该列的列名必须和该用户表的登陆字段名一致]，则对应的表有个隐藏属性authTable为”是”，那么该用户查看该表信息时，只能查看自己的
        try:
            __authTables__=xuesheng.__authTables__
        except:
            __authTables__=None
        if __authTables__!=None and  __authTables__!={}:
            for authColumn,authTable in __authTables__.items():
                if authTable==tablename:
                    params = request.session.get("params")
                    req_dict[authColumn]=params.get(authColumn)
                    break
            #authColumn=list(__authTables__.keys())[0]
            #authTable=__authTables__.get(authColumn)
            # if authTable==tablename:
                #params = request.session.get("params")
                #req_dict[authColumn]=params.get(authColumn)
        '''__authSeparate__此属性为真，params添加userid，后台只查询个人数据'''
        try:
            __authSeparate__=xuesheng.__authSeparate__
        except:
            __authSeparate__=None
        if __authSeparate__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users" and 'userid' in columns:
                try:
                    req_dict['userid']=request.session.get("params").get("id")
                except:
                    pass
        #当项目属性hasMessage为”是”，生成系统自动生成留言板的表messages，同时该表的表属性hasMessage也被设置为”是”,字段包括userid（用户id），username(用户名)，content（留言内容），reply（回复）
        #接口page需要区分权限，普通用户查看自己的留言和回复记录，管理员查看所有的留言和回复记录
        try:
            __hasMessage__=xuesheng.__hasMessage__
        except:
            __hasMessage__=None
        if  __hasMessage__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users":
                req_dict["userid"]=request.session.get("params").get("id")
        # 判断当前表的表属性isAdmin,为真则是管理员表
        # 当表属性isAdmin=”是”,刷出来的用户表也是管理员，即page和list可以查看所有人的考试记录(同时应用于其他表)
        __isAdmin__ = None
        allModels = apps.get_app_config('main').get_models()
        for m in allModels:
            if m.__tablename__==tablename:
                try:
                    __isAdmin__ = m.__isAdmin__
                except:
                    __isAdmin__ = None
                break
        # 当前表也是有管理员权限的表
        if  __isAdmin__ == "是" and req_dict.get("userid"):
            del req_dict["userid"]
        else:
            #非管理员权限的表,判断当前表字段名是否有userid
            if tablename!="users" and "userid" in xuesheng.getallcolumn(xuesheng,xuesheng):
                req_dict["userid"] = request.session.get("params").get("id")
        msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
        msg['data']['pageSize']  =xuesheng.page(xuesheng, xuesheng, req_dict)
        return JsonResponse(msg)
def xuesheng_autoSort(request):
    '''
    ．智能推荐功能(表属性：[intelRecom（是/否）],新增clicktime[前端不显示该字段]字段（调用info/detail接口的时候更新），按clicktime排序查询)
主要信息列表（如商品列表，新闻列表）中使用，显示最近点击的或最新添加的5条记录就行
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code,  "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
        req_dict = request.session.get("req_dict")
        if "clicknum"  in xuesheng.getallcolumn(xuesheng,xuesheng):
            req_dict['sort']='clicknum'
        else:
            req_dict['sort']='clicktime'
        req_dict['order']='desc'
        msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
        msg['data']['pageSize']  = xuesheng.page(xuesheng,xuesheng, req_dict)
        return JsonResponse(msg)
def xuesheng_list(request):
    '''
    前台分页
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code,  "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
        req_dict = request.session.get("req_dict")
        #获取全部列名
        columns=  xuesheng.getallcolumn( xuesheng, xuesheng)
        #表属性[foreEndList]前台list:和后台默认的list列表页相似,只是摆在前台,否:指没有此页,是:表示有此页(不需要登陆即可查看),前要登:表示有此页且需要登陆后才能查看
        try:
            __foreEndList__=xuesheng.__foreEndList__
        except:
            __foreEndList__=None
        if __foreEndList__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users" and 'userid' in columns:
                try:
                    req_dict['userid']=request.session.get("params").get("id")
                except:
                    pass
        #forrEndListAuth
        try:
            __foreEndListAuth__=xuesheng.__foreEndListAuth__
        except:
            __foreEndListAuth__=None
        #authSeparate
        try:
            __authSeparate__=xuesheng.__authSeparate__
        except:
            __authSeparate__=None
        if __foreEndListAuth__ =="是" and __authSeparate__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users":
                req_dict['userid']=request.session.get("params",{"id":0}).get("id")
        tablename = request.session.get("tablename")
        if tablename == "users" and req_dict.get("userid") != None:#判断是否存在userid列名
            del req_dict["userid"]
        else:
            __isAdmin__ = None
            allModels = apps.get_app_config('main').get_models()
            for m in allModels:
                if m.__tablename__==tablename:
                    try:
                        __isAdmin__ = m.__isAdmin__
                    except:
                        __isAdmin__ = None
                    break
            if __isAdmin__ == "是" and req_dict.get("userid"):
                del req_dict["userid"]
            else:
                #非管理员权限的表,判断当前表字段名是否有userid
                if "userid" in columns:
                    try:
                        # 本接口可以匿名访问,所以try判断是否为匿名
                        req_dict['userid']=request.session.get("params").get("id")
                    except:
                            pass
        msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
        msg['data']['pageSize']  = xuesheng.page(xuesheng, xuesheng, req_dict)
        return JsonResponse(msg)
def xuesheng_save(request):
    '''
    后台新增
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        req_dict = request.session.get("req_dict")
        tablename=request.session.get("tablename")
        #获取全部列名
        columns=  xuesheng.getallcolumn( xuesheng, xuesheng)
        if tablename!='users' and req_dict.get("userid")!=None and 'userid' in columns:
            params=request.session.get("params")
            req_dict['userid']=params.get('id')
        error= xuesheng.createbyreq(xuesheng,xuesheng, req_dict)
        if error!=None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def xuesheng_add(request):
    '''
    前台新增
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        req_dict = request.session.get("req_dict")
        #获取全部列名
        columns=  xuesheng.getallcolumn( xuesheng, xuesheng)
        try:
            __authSeparate__=xuesheng.__authSeparate__
        except:
            __authSeparate__=None
        if __authSeparate__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users" and 'userid' in columns:
                try:
                    req_dict['userid']=request.session.get("params").get("id")
                except:
                    pass
        try:
            __foreEndListAuth__=xuesheng.__foreEndListAuth__
        except:
            __foreEndListAuth__=None
        if __foreEndListAuth__ and __foreEndListAuth__!="否":
            tablename=request.session.get("tablename")
            if tablename!="users":
                req_dict['userid']=request.session.get("params").get("id")
        error= xuesheng.createbyreq(xuesheng,xuesheng, req_dict)
        if error!=None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def xuesheng_thumbsup(request,id_):
    '''
     点赞：表属性thumbsUp[是/否]，刷表新增thumbsupnum赞和crazilynum踩字段，
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        req_dict = request.session.get("req_dict")
        id_=int(id_)
        type_=int(req_dict.get("type",0))
        rets=xuesheng.getbyid(xuesheng,xuesheng,id_)
        update_dict={
        "id":id_,
        }
        if type_==1:#赞
            update_dict["thumbsupnum"]=int(rets[0].get('thumbsupnum'))+1
        elif type_==2:#踩
            update_dict["crazilynum"]=int(rets[0].get('crazilynum'))+1
        error = xuesheng.updatebyparams(xuesheng,xuesheng, update_dict)
        if error!=None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def xuesheng_info(request,id_):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        data = xuesheng.getbyid(xuesheng,xuesheng, int(id_))
        if len(data)>0:
            msg['data']=data[0]
        #浏览点击次数
        try:
            __browseClick__= xuesheng.__browseClick__
        except:
            __browseClick__=None
        if __browseClick__=="是"  and  "clicknum"  in xuesheng.getallcolumn(xuesheng,xuesheng):
            try:
                clicknum=int(data[0].get("clicknum",0))+1
            except:
                clicknum=0+1
            click_dict={"id":int(id_),"clicknum":clicknum}
            ret=xuesheng.updatebyparams(xuesheng,xuesheng,click_dict)
            if ret!=None:
                msg['code'] = crud_error_code
                msg['msg'] = ret
        return JsonResponse(msg)
def xuesheng_detail(request,id_):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        data =xuesheng.getbyid(xuesheng,xuesheng, int(id_))
        if len(data)>0:
            msg['data']=data[0]
        #浏览点击次数
        try:
            __browseClick__= xuesheng.__browseClick__
        except:
            __browseClick__=None
        if __browseClick__=="是"   and  "clicknum"  in xuesheng.getallcolumn(xuesheng,xuesheng):
            try:
                clicknum=int(data[0].get("clicknum",0))+1
            except:
                clicknum=0+1
            click_dict={"id":int(id_),"clicknum":clicknum}
            ret=xuesheng.updatebyparams(xuesheng,xuesheng,click_dict)
            if ret!=None:
                msg['code'] = crud_error_code
                msg['msg'] = retfo
        return JsonResponse(msg)
def xuesheng_update(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        req_dict = request.session.get("req_dict")
        if req_dict.get("mima") and req_dict.get("password"):
            if "mima" not  in xuesheng.getallcolumn(xuesheng,xuesheng) :
                del req_dict["mima"]
            if  "password" not  in xuesheng.getallcolumn(xuesheng,xuesheng) :
                del req_dict["password"]
        try:
            del req_dict["clicknum"]
        except:
            pass
        error = xuesheng.updatebyparams(xuesheng, xuesheng, req_dict)
        if error!=None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def xuesheng_delete(request):
    '''
    批量删除
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
        req_dict = request.session.get("req_dict")
        error=xuesheng.deletes(xuesheng,
            xuesheng,
             req_dict.get("ids")
        )
        if error!=None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def xuesheng_vote(request,id_):
    '''
    浏览点击次数（表属性[browseClick:是/否]，点击字段（clicknum），调用info/detail接口的时候后端自动+1）、投票功能（表属性[vote:是/否]，投票字段（votenum）,调用vote接口后端votenum+1）
统计商品或新闻的点击次数；提供新闻的投票功能
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code}
        data= xuesheng.getbyid(xuesheng, xuesheng, int(id_))
        for i in data:
            votenum=i.get('votenum')
            if votenum!=None:
                params={"id":int(id_),"votenum":votenum+1}
                error=xuesheng.updatebyparams(xuesheng,xuesheng,params)
                if error!=None:
                    msg['code'] = crud_error_code
                    msg['msg'] = error
        return JsonResponse(msg)
#coding:utf-8
__author__ = "ila"
from django.utils.deprecation import MiddlewareMixin
from django.http import JsonResponse
class Xparam(MiddlewareMixin):
    def process_request(self,request):
        fullPath=request.get_full_path()
        print("fullPath===============>",fullPath)
        if "/js/" not in fullPath and "/css/" not in fullPath and "/img/" not in fullPath and "/fonts/" not in fullPath and "/front/" not in fullPath:
            req_dict={}
            #获取query的参数
            for k,v in request.GET.items():
                req_dict[k]=v
            if request.method=="POST":
                contentType=request.META.get('CONTENT_TYPE',"")
                # print("contentType==================>",contentType)
                if "json" in contentType:
                    #解析application/json的参数,如果解析失败,提前返回报错20200925
                    try:
                        data_=json.loads(request.body)
                    except Exception as e:
                        errStr="json loads params error :{} {}".format(Exception,e)
                        print(errStr)
                        msg={"code":500,"msg":errStr}
                        return JsonResponse(msg)
                    if type(data_)==type([1]):
                        req_dict['ids']=data_
                    else:
                        for k,v in data_.items():
                            req_dict[k] = v
                else:
                    # 获取formdata的参数
                    for k, v in request.POST.items():
                        req_dict[k] = v
            #处理参数，去掉无用的和错误的参数
            if req_dict.get("created")!=None:
                req_dict['addtime']=copy.deepcopy(req_dict.get("created"))
                del req_dict["created"]
            if req_dict.get("t") != None:
                del req_dict["t"]
            if req_dict.get("1")!=None:
                req_dict['type']=copy.deepcopy(req_dict.get("1"))
                del req_dict["1"]
            if req_dict.get(1)!=None:
                req_dict['type']=copy.deepcopy(req_dict.get(1))
                del req_dict[1]
            print("req_dict=============+>",req_dict)
            request.session["req_dict"] =req_dict
default_app_config = 'main.apps.MainConfig'
#coding:utf-8
__author__ = "ila"
from django.http import JsonResponse
from django.apps import apps
from django.db.models.aggregates import Count,Sum
from .models import news
from util.codes import *
from util.auth import Auth
def news_register(request):
    if request.method in ["POST", "GET"]:
        msg = {'code': normal_code, "msg": mes.normal_code}
        req_dict = request.session.get("req_dict")
        error = news.createbyreq(news, news, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = "用户已存在,请勿重复注册!"
        return JsonResponse(msg)
def news_login(request):
    if request.method in ["POST", "GET"]:
        msg = {'code': normal_code, "msg": mes.normal_code}
        req_dict = request.session.get("req_dict")
        datas = news.getbyparams(news, news, req_dict)
        if not datas:
            msg['code'] = password_error_code
            msg['msg'] = mes.password_error_code
            return JsonResponse(msg)
        req_dict['id'] = datas[0].get('id')
        return Auth.authenticate(Auth, news, req_dict)
def news_logout(request):
    if request.method in ["POST", "GET"]:
        msg = {
            "msg": "登出成功",
            "code": 0
        }
        return JsonResponse(msg)
def news_resetPass(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code}
        req_dict = request.session.get("req_dict")
        error = news.updatebyparams(news,news, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return JsonResponse(msg)
def news_session(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code,"msg": mes.normal_code, "data": {}}
        req_dict={"id":request.session.get('params').get("id")}
        msg['data']  = news.getbyparams(news, news, req_dict)[0]
        return JsonResponse(msg)
def news_page(request):
    '''
    '''
    if request.method in ["POST", "GET"]:
        msg = {"code": normal_code, "msg": mes.normal_code,  "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
        req_dict = request.session.get("req_dict")
        #获取全部列名
        columns=  news.getallcolumn( news, news)
        #当前登录用户所在表
        tablename = request.session.get("tablename")
        #当列属性authTable有值(某个用户表)[该列的列名必须和该用户表的登陆字段名一致]，则对应的表有个隐藏属性authTable为”是”，那么该用户查看该表信息时，只能查看自己的
        try:
            __authTables__=news.__authTables__
        except:
            __authTables__=None
        if __authTables__!=None and  __authTables__!={}:
            for authColumn,authTable in __authTables__.items():
                if authTable==tablename:
                    params = request.session.get("params")
                    req_dict[authColumn]=params.get(authColumn)
                    break
            #authColumn=list(__authTables__.keys())[0]
            #authTable=__authTables__.get(authColumn)
            # if authTable==tablename:
                #params = request.session.get("params")
                #req_dict[authColumn]=params.get(authColumn)
        '''__authSeparate__此属性为真，params添加userid，后台只查询个人数据'''
        try:
            __authSeparate__=news.__authSeparate__
        except:
            __authSeparate__=None
        if __authSeparate__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users" and 'userid' in columns:
                try:
                    req_dict['userid']=request.session.get("params").get("id")
                except:
                    pass
        #当项目属性hasMessage为”是”，生成系统自动生成留言板的表messages，同时该表的表属性hasMessage也被设置为”是”,字段包括userid（用户id），username(用户名)，content（留言内容），reply（回复）
        #接口page需要区分权限，普通用户查看自己的留言和回复记录，管理员查看所有的留言和回复记录
        try:
            __hasMessage__=news.__hasMessage__
        except:
            __hasMessage__=None
        if  __hasMessage__=="是":
            tablename=request.session.get("tablename")
            if tablename!="users":
                req_dict["userid"]=request.session.get("params").get("id")