| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | #!/usr/bin/env python3# -*- coding: utf-8 -*-"""@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.@AUTHOR: ASJRD019@NAME: AnsjerFormal@software: PyCharm@DATE: 2019/5/27 15:50@Version: python3.6@MODIFY DECORD:ansjer dev@file: AliPayObject.py@Contact: pzb3076@163.com"""import timeimport tracebackimport simplejson as jsonfrom django.utils.decorators import method_decoratorfrom django.views.decorators.csrf import csrf_exemptfrom django.views.generic.base import Viewfrom Model.models import UidSetModel, Device_User, Device_Info,UidPushModelfrom Object.ResponseObject import ResponseObjectfrom Object.TokenObject import TokenObjectfrom Service.CommonService import CommonServicefrom Service.ModelService import ModelService'''# 管理员获取信息http://192.168.136.39:8000/uidset/adminQuery?token=local&page=1&line=10  管理员删除信息http://192.168.136.39:8000/uidset/adminDelete?token=local&id=2  管理员添加信息http://192.168.136.39:8000/uidset/adminAdd?token=local&uid=JW3684H8BSHG9TTM111A  管理员编辑信息http://192.168.136.39:8000/uidset/adminUpdate?token=local&id=6&content={"uid":"9999"}'''# 设备信息添加class UidSetView(View):    @method_decorator(csrf_exempt)    def dispatch(self, *args, **kwargs):        return super(UidSetView, self).dispatch(*args, **kwargs)    def get(self, request, *args, **kwargs):        request.encoding = 'utf-8'        operation = kwargs.get('operation')        return self.validation(request.GET, request, operation)    def post(self, request, *args, **kwargs):        request.encoding = 'utf-8'        operation = kwargs.get('operation')        return self.validation(request.POST, request, operation)    def validation(self, request_dict, request, operation):        response = ResponseObject()        if operation is None:            return response.json(444, 'error path')        token = request_dict.get('token', None)        # 设备主键uid        tko = TokenObject(token)        response.lang = tko.lang        if tko.code != 0:            return response.json(tko.code)        userID = tko.userID        if operation == 'queryAll':            return self.do_queryAll(request_dict, userID, response)        elif operation == 'adminQueryAll':            return self.do_admin_queryAll(request_dict, userID, response)        elif operation == 'adminDelete':            return self.do_admin_delete(request_dict, userID, response)        elif operation == 'adminQuery':            return self.do_admin_query(request_dict, userID, response)        elif operation == 'adminAdd':            return self.do_admin_add(request_dict, userID, response)        elif operation == 'adminUpdate':            return self.do_admin_update(request_dict, userID, response)        else:            return response.json(444, 'error path')    def do_queryAll(self, request_dict, userID, response):        uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)        uid_set_qs = UidSetModel.objects.filter(uid__in=uid_list).values('uid', 'detect_status', 'detect_interval',                                                                         'version', 'ucode', 'p2p_region', 'tz',                                                                         'video_code', 'channel', 'cloud_vod')                if uid_set_qs.exists():            return response.json(0, list(uid_set_qs))        else:            return response.json(0)    def do_admin_queryAll(self, request_dict, userID, response):            uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)            print (uid_list)            id = request_dict.get('id', None)            uid_set_qs = UidPushModel.objects.filter(uid_set__id=id).values('addTime',                                                                            'appBundleId','app_type','lang','m_code','push_type','token_val','tz','uid_set',                                                                            'updTime','userID','userID__username'                                                                            , 'userID__userEmail', 'userID__NickName'                                                                            , 'userID__is_active', 'userID__phone'                                                                            , 'uid_set__channel', 'uid_set__uid'                                                                            , 'uid_set__detect_status'                                                                            ,'uid_set__detect_interval', 'uid_set__ucode'                                                                            , 'uid_set__version'                                                                            , 'uid_set__p2p_region'                                                                            ,'uid_set__cloud_vod', 'uid_set__tz'                                                                            , 'uid_set__video_code'                                                                            ,'uid_set__nickname'                                                                            ,'uid_set__ip'                                                                            )            return response.json(0, list(uid_set_qs))        # 管理员删除    def do_admin_delete(self, request_dict, userID, response):        own_perm = ModelService.check_perm(userID, 20)        if own_perm is True:            id = request_dict.get('id')            try:                list = UidSetModel.objects.filter(id=id)                if list.exists():                    list.delete()                    return response.json(0)                else:                    return response.json(173)            except Exception as e:                print(repr(e))                return response.json(424, repr(e))        else:            return response.json(404)    # 管理员查询接口    def do_admin_query(self, request_dict, userID, response):        own_perm = ModelService.check_perm(userID, 20)        if own_perm is True:            page = request_dict.get('page', None)            line = request_dict.get('line', None)            page = int(page)            line = int(line)            deviceContent = request_dict.get('content', None)            if deviceContent:                try:                    searchCondition = json.loads(deviceContent)                except Exception as e:                    return response.json(10, repr(e))                else:                    kwargs = CommonService.get_kwargs(data=searchCondition)                    omqs = UidSetModel.objects.filter(**kwargs)                    if not omqs.exists():                        return response.json(0, [])                    count = omqs.count()                    order_ql = omqs[(page - 1) * line:page * line]                    uidset_json = CommonService.qs_to_dict(order_ql)                    uidset_json['count'] = count                    return response.json(0, uidset_json)            else:                omqs = UidSetModel.objects.all().order_by('-id')                if not omqs.exists():                    return response.json(0, [])                count = omqs.count()                order_ql = omqs[(page - 1) * line:page * line]                uidset_json = CommonService.qs_to_dict(order_ql)                uidset_json['count'] = count                return response.json(0, uidset_json)        else:            return response.json(404)    # 管理员的添加    def do_admin_add(self, request_dict, userID, response):        own_perm = ModelService.check_perm(userID=userID, permID=40)        if own_perm is not True:            return response.json(404)        uid = request_dict.get('uid', None)        timestamp = int(time.time())        param_flag = CommonService.get_param_flag(            data=[uid])        if param_flag is not True:            return response.json(444)        try:            uidset = UidSetModel(                uid=uid,                addTime=timestamp,                updTime=timestamp)            uidset.save()            return response.json(0)        except Exception:            errorInfo = traceback.format_exc()            print(errorInfo)            return response.json(500, {'details': errorInfo})    # 管理员的编辑    def do_admin_update(self, request_dict, userID, response):        own_perm = ModelService.check_perm(userID=userID, permID=50)        if own_perm is not True:            return response.json(404)        deviceContent = request_dict.get('content', None)        id = request_dict.get('id', None)        if not deviceContent or not id:            return response.json(444, 'content,id')        try:            timestamp = int(time.time())            deviceData = json.loads(deviceContent)            uid_set = UidSetModel.objects.filter(id=id)            if uid_set.exists():                uid_set.update(updTime=timestamp, **deviceData)                return response.json(0)            else:                return response.json(173)        except Exception:            errorInfo = traceback.format_exc()            print(errorInfo)            return response.json(500, {'details': errorInfo})
 |