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 from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject from Service.CommonService import CommonService ''' http://192.168.136.45:8077/userbrandinfo?operation=query&token=test&page=1&line=10 http://192.168.136.39:8000/userbrandinfo?operation=query&token=test&page=1&line=5 http://192.168.136.39:8000/userbrandinfo?operation=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' self.clientIP = CommonService.get_ip_address(request) return self.validation(request_dict=request.GET) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' self.clientIP = CommonService.get_ip_address(request) return self.validation(request_dict=request.POST) def validation(self, request_dict, *args, **kwargs): 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 operation = request_dict.get('operation', None) if userID is not None: if operation == 'query': return self.query_info(request_dict, userID,response) elif operation == 'add': return self.add_info(request_dict, userID,response) elif operation == 'queryAll': return self.query_all_info(request_dict, userID,response) else: return response.json(444,'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=12341234&deviceModel=deviceModel&osType=osType&osVersion=osVersion 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) print(self.clientIP) 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()) ) 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}) def query_info(self, request_dict, userID,response): page = int(request_dict.get('page', None)) line = int(request_dict.get('line', None)) param_flag = CommonService.get_param_flag(data=[page, line]) if param_flag is True: user_brand_queryset = User_Brand.objects.filter(userID=userID).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) send_json['count'] = count return response.json(0, send_json) return response.json(0, {'datas': [], 'count': 0}) 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)) order = request_dict.get('order','-id') if order == '': order ='-id' 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: user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct() print (user_brand_queryset) addtime=[] send_jsons=[] counts=0 for i in user_brand_queryset: counts=counts+1 user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by(order)[:1] user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast) addtime.append(user_brand_querysetlast["datas"][0]['fields']['addTime']) 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]) print(addtime) print(1111111111) user_brand_queryset = User_Brand.objects.filter(userID__in = user_brand_queryset, addTime__in=addtime).order_by(order) 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, {'datas': send_jsons, 'count': counts}) return response.json(0, {'datas': [], 'count': 0}) else: return response.json(404) else: return response.json(444)