123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #!/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/9 11:50
- @Version: python3.6
- @MODIFY DECORD:ansjer dev
- @file: AliPayObject.py
- @Contact: pzb3076@163.com
- """
- from django.views.generic.base import View
- from django.utils.decorators import method_decorator
- from django.views.decorators.csrf import csrf_exempt
- from Service.ModelService import ModelService
- from Model.models import User_Brand,Device_User
- from django.utils import timezone
- import traceback,time,json,urllib.request
- from Object.ResponseObject import ResponseObject
- from Object.TokenObject import TokenObject
- from Service.CommonService import CommonService
- from collections import Counter
- '''
- http://192.168.136.39:8000/userbrandinfo/queryByAdmin?token=test&page=1&line=10 全部记录
- http://192.168.136.39:8000/userbrandinfo/queryArea?token=test 市的区域统计
- http://192.168.136.39:8000/userbrandinfo/queryDeviceSupplier?token=test 品牌统计
- http://192.168.136.39:8000/userbrandinfo/queryAllByAdmin?token=test&page=1&line=10 全部记录中过滤查询每个用户的最新一条数据
- http://192.168.136.39:8000/userbrandinfo/queryAll?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiY24iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1NTk4OTY4NTd9.nhK3VSghSGjyXKjel4woz7R_3bhjgqQDlX-ypYsklNU&page=1&line=5
- '''
- class UserBrandInfo(View):
- @method_decorator(csrf_exempt)
- def dispatch(self, *args, **kwargs):
- return super(UserBrandInfo, 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):
- if operation is None:
- return response.json(444, 'error path')
- self.clientIP = CommonService.get_ip_address(request)
- response = ResponseObject()
- token = request_dict.get('token', None)
- if token is not None:
- tko = TokenObject(token)
- response.lang = tko.lang
- if tko.code == 0:
- userID = tko.userID
- if userID is not None:
- if operation == 'queryByAdmin':
- return self.query_info(request_dict, userID,response)
- elif operation == 'add':
- return self.add_info(request_dict, userID,response)
- elif operation == 'queryAllByAdmin':
- return self.query_all_info(request_dict, userID,response)
- elif operation == 'deleteByAdmin':
- return self.delete_by_admin(request_dict, userID,response)
- elif operation == 'queryDeviceSupplier':
- return self.query_deviceSupplier_info(request_dict, userID,response)
- elif operation == 'queryArea':
- return self.query_area_info(request_dict, userID,response)
- else:
- return response.json(444)
- else:
- return response.json(309)
- else:
- return response.json(tko.code)
- else:
- return response.json(309)
- # 获取外网IP
- # http://192.168.136.39:8000/userbrandinfo?operation=add&token=test&deviceSupplier=小米&deviceModel=HM NOTE 1TD&osType=WEB&osVersion=4.0.0
- def add_info(self, request_dict, userID,response):
- deviceSupplier = request_dict.get('deviceSupplier', None)
- deviceModel = request_dict.get('deviceModel', None)
- osType = request_dict.get('osType', None)
- osVersion = request_dict.get('osVersion', None)
- country = request_dict.get('country', None)
- province = request_dict.get('province', None)
- city = request_dict.get('city', None)
- area = request_dict.get('area', None)
- street = request_dict.get('street', None)
- longitude = request_dict.get('longitude', None)
- latitude = request_dict.get('latitude', None)
- appId = request_dict.get('appId', None)
- if area is None:
- area=''
- if street is None:
- street=''
- if longitude is None:
- longitude=''
- if latitude is None:
- latitude=''
- param_area = CommonService.get_param_flag(data=[country,province,city])
- must_fill_in = CommonService.get_param_flag(data=[appId,deviceSupplier,deviceModel,osType,osVersion])
- if must_fill_in is False:
- return response.json(444,'appId,deviceSupplier,deviceModel,osType,osVersion')
- if param_area is True:
- if '省' in province:
- province = province.replace('省','')
- if '市' in city:
- city = city.replace('市','')
- status = 1
- country = country
- province = province
- city = city
- area = area
- street = street
- longitude = longitude
- latitude = latitude
- else:
- status = 0
- print (self.clientIP)
- jsonData = CommonService.getIpIpInfo(ip=self.clientIP,lang='CN')
- country = jsonData['country_name']
- province = jsonData['region_name']
- city = jsonData['city_name']
- area = ''
- street = ''
- longitude = jsonData['longitude']
- latitude = jsonData['latitude']
- param_flag = CommonService.get_param_flag(data=[deviceSupplier, deviceModel,osType,osVersion])
- if param_flag is True:
- try:
- user_brand = User_Brand(
- userID=Device_User.objects.get(userID=userID),
- deviceSupplier=deviceSupplier,
- deviceModel=deviceModel,
- osType=osType,
- osVersion=osVersion,
- ip=self.clientIP,
- addTime = int(time.time()),
- status = status,
- country = country,
- province = province,
- city = city,
- appId=appId,
- area = area,
- street = street,
- longitude = longitude,
- latitude = latitude
- )
- user_brand.save()
- except Exception:
- errorInfo = traceback.format_exc()
- print(errorInfo)
- return response.json(424, {'details': errorInfo})
- else:
- print(type(user_brand.addTime))
- return response.json(0,{'id':user_brand.id})
- else:
- # 参数错误
- return response.json(444)
- def query_info(self, request_dict, userID,response):
- page = int(request_dict.get('page', None))
- line = int(request_dict.get('line', None))
- username = request_dict.get('username',None)
- param_flag = CommonService.get_param_flag(data=[page, line])
- if param_flag is True:
- check_perm = ModelService.check_perm(userID=userID,permID=30)
- if check_perm is True:
- if username is None or username is '':
- user_brand_queryset = User_Brand.objects.all().order_by( '-id')
- else:
- user_brand_queryset = User_Brand.objects.filter(userID__username=username).order_by('-id')
- if user_brand_queryset.exists():
- count = user_brand_queryset.count()
- res = user_brand_queryset[(page - 1) * line:page * line]
- send_json = CommonService.qs_to_dict(res)
- for k, v in enumerate(send_json["datas"]):
- username = ModelService.get_user_name(userID=send_json["datas"][k]['fields']['userID'])
- send_json["datas"][k]['fields']['username']=username
- send_json['count'] = count
- return response.json(0, send_json)
- return response.json(0, {'datas': [], 'count': 0})
- else:
- return response.json(404)
- else:
- return response.json(444)
- def delete_by_admin(self, request_dict, userID,response):
- id_list = request_dict.getlist('id', None)
- param_flag = CommonService.get_param_flag(data=[id_list])
- if param_flag is True:
- check_perm = ModelService.check_perm(userID=userID, permID=10)
- if check_perm is True:
- is_delete = User_Brand.objects.filter(id__in=id_list).delete()
- return response.json(0, {'delete_count': is_delete[0]})
- else:
- return response.json(404)
- else:
- return response.json(444)
- # 查询每个用户最新的一条记录统计信息
- def query_all_info(self, request_dict, userID,response):
- page = int(request_dict.get('page', None))
- line = int(request_dict.get('line', None))
- username = request_dict.get('username', None)
- param_flag = CommonService.get_param_flag(data=[page, line])
- if param_flag is True:
- check_perm = ModelService.check_perm(userID=userID,permID=30)
- if check_perm is True:
- if username is None or username is '':
- # 按照用户去重复查询
- # 分页
- user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()[(page - 1) * line:page * line]
- else:
- # 分页
- user_brand_queryset = User_Brand.objects.filter(userID__username=username).order_by('userID').values_list( 'userID',flat=True).distinct()[(page - 1) * line:page * line]
- send_jsons=[]
- counts=0
- for i in user_brand_queryset:
- counts=counts+1
- user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
- user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
- username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
- user_brand_querysetlast["datas"][0]['fields']['username']=username
- send_jsons.append(user_brand_querysetlast["datas"][0])
- # 按照加入的日期排序
- send_jsons = sorted(send_jsons, key=lambda x:x['fields']['addTime'], reverse=True)
- return response.json(0, {'datas': send_jsons, 'count': counts})
- else:
- return response.json(404)
- else:
- return response.json(444)
- # 品牌统计的接口
- def query_deviceSupplier_info(self, request_dict, userID,response):
- check_perm = ModelService.check_perm(userID=userID,permID=30)
- if check_perm is True:
- # 按照用户去重复查询
- user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
- send_jsons=[]
- counts=0
- for i in user_brand_queryset:
- counts=counts+1
- user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
- user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
- username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
- user_brand_querysetlast["datas"][0]['fields']['username']=username
- send_jsons.append(user_brand_querysetlast["datas"][0])
- deviceSupplier=[]
- for k, v in enumerate(send_jsons):
- deviceSupplier.append(v['fields']['deviceSupplier'])
- deviceSupplier = Counter(deviceSupplier)
- return response.json(0, {'datas': deviceSupplier,'counts':counts})
- else:
- return response.json(404)
- # 区域统计的接口
- def query_area_info(self, request_dict, userID,response):
- check_perm = ModelService.check_perm(userID=userID,permID=30)
- district = request_dict.get('district', None)
- if check_perm is True:
- # 按照用户去重复查询
- user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
- send_jsons=[]
- counts=0
- for i in user_brand_queryset:
- counts=counts+1
- user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
- user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
- username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
- user_brand_querysetlast["datas"][0]['fields']['username']=username
- send_jsons.append(user_brand_querysetlast["datas"][0])
- deviceSupplier=[]
- for k, v in enumerate(send_jsons):
- deviceSupplier.append(v['fields'][district])
- deviceSupplier = Counter(deviceSupplier)
- return response.json(0, {'datas': deviceSupplier,'counts':counts})
- else:
- return response.json(404)
|