def post(self, request, *args, **kwargs): response = UserResponse() code = request.data.get('code', None) gender = request.data.get('gender', None) country = request.data.get('country', None) # if not code: # return Response({'message': '缺少code'}, status=status.HTTP_400_BAD_REQUEST) # # url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code" \ # .format(settings.APP_ID, settings.APP_KEY, code) # r = requests.get(url) # res = json.loads(r.text) # openid = res['openid'] if 'openid' in res else None # session_key = res['session_key'] if 'session_key' in res else None # if not openid: # return Response({'message': '微信调用失败'}, status=status.HTTP_400_BAD_REQUEST) openid = '033xWHE718sLYL1VYID71LEME71xWHEI' user = UserInfo.objects.filter(username=openid).first() if user: UserInfo.objects.filter(username=openid).update(gender=gender, nationality=country) token = generate_token(user.id, openid) else: user_obj = UserInfo.objects.create(username=openid, gender=gender, nationality=country) token = generate_token(user_obj.id, openid) response.msg = "Login Ok!" response.token = token response.openid = openid return Response(response.get_data)
def get(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() response.msg = "我是榜单规则" return Response(response.get_data)
def post(self,request,*args,**kwargs): response = UserResponse() content = request.data.get("content",None) if content: Notice.objects.create(content=content) response.msg = "Add notice successfully" else: response.status = 401 response.msg = "Content is Null" return Response(response.get_data)
def post(self, request, *args, **kwargs): response = UserResponse() gender = request.data.get('gender', None) nationality = request.data.get('nationality', None) avatar = request.data.get('avatar', None) openid = request.data.get('openid', None) nickname = request.data.get('nickName', None) if not all([gender, nationality, avatar, openid]): return Response({"status": 401, "msg": "数据不完整"}) if openid: user_obj = UserInfo.objects.filter(openid=openid) if user_obj: try: UserInfo.objects.filter(openid=openid).update( nickname=nickname, gender=gender, nationality=nationality, avatar=avatar) response.msg = "传送信息成功" except Exception as e: response.status = 402 response.msg = str(e) else: response.status = 402 response.msg = "无效openid,没有对应的用户" else: response.status = 403 response.msg = "openid 不存在" return Response(response.get_data)
def get(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() phone = request.GET.get('phone', None) total = request.GET.get('total', None) crunchies = request.GET.get('crunchies', None) crunchies = self._get_crunchies(crunchies) result_phone = detect_phone(phone) if result_phone.get('status') == 200: user_obj = UserInfo.objects.filter(phone=phone).first() if user_obj: datalist = [] score_obj = ScoreRecord.objects.filter(user_id=user_obj.id, total=total, crunchies=crunchies).values( "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "crunchies") for item in score_obj: datalist.append(item) response.datalist = datalist response.msg = "Query successfully" else: response.status = 401 response.msg = "User doesn't exist" else: response.status = 402 response.msg = "Phone number is wrong" return Response(response.get_data)
def get(self,request,*args,**kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() notice_obj = Notice.objects.order_by("-created").distinct()[:3] datalist = [] for item in notice_obj: datalist.append(item.content) response.msg = "Query successfully" response.datalist = datalist return Response(response.get_data)
def post(self, request, *args, **kwargs): response = UserResponse() username = request.data.get('username', None) openid = request.data.get('openid', None) print(username, openid) if username and openid: user_obj = UserInfo.objects.filter(username=username).first() if user_obj: print("该名字已存在,请重新输入") response.status = 401 response.msg = "该名字已存在,请重新输入" else: UserInfo.objects.filter(openid=openid).update( username=username) response.msg = "更新成功" return Response(response.get_data)
def get(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() crunchies = request.GET.get('crunchies', None) category = request.GET.get('category', None) type = request.GET.get('type', "total") data_list = [] current_month = datetime.datetime.now().month if category == 'athletics' and crunchies == 'new': if type == "month": score_obj = ScoreRecord.objects.filter(category=0, crunchies=0, created__month=current_month).order_by("-total").values( "user__username", "total").distinct()[:50] elif type == "quarter": score_obj = ScoreRecord.objects.filter(category=0, crunchies=0).filter( created__month__in=[current_month, current_month + 1, current_month + 2]).order_by("-total").values( "user__username", "total").distinct()[:50] else: score_obj = ScoreRecord.objects.filter(category=0, crunchies=0).order_by("-total").values( "user__username", "total").distinct()[:50] data_list = self._getdata(score_obj, data_list, crunchies='new') elif category == 'athletics' and crunchies == 'brave': if type == "month": score_obj = ScoreRecord.objects.filter(category=0, crunchies=1, created__month=current_month).order_by( "-total").values("user__username", "total").distinct()[:50] elif type == 'quarter': score_obj = ScoreRecord.objects.filter(category=0, crunchies=1).filter( created__month__in=[current_month, current_month + 1, current_month + 2]).order_by("-total").values( "user__username", "total").distinct()[:50] else: score_obj = ScoreRecord.objects.filter(category=0, crunchies=1).order_by("-total").values( "user__username", "total").distinct()[:50] data_list = self._getdata(score_obj, data_list, crunchies='brave') elif category == "recreation": pass response.datalist = data_list response.msg = "Query successfully" return Response(response.get_data)
def post(self,request,*args,**kwargs): """ :param request: {“panda_file”:"FILE"} :param args: None :param kwargs: None :return: """ response = UserResponse() # print(request.FILES.get("file")) file = request.FILES.get('file', None) print(file) if file is None: response.status = 401 response.msg = "There is no file" return Response(response.get_data) file_name = file.name file_size = file.size suffix = file_name.split('.')[1] print(file_name,file_size,suffix) if not self._detect_suffix(suffix): response.status = 402 response.msg = "The file is not in the right format" return Response(response.get_data) else: # file_path = settings.MEDIA_ROOT + '\\' + file.name file_path = os.path.join(settings.MEDIA_ROOT,file_name) print(file_path) if os.path.exists(file_path): self._write_file(file_path, file) self._import_excel(file_path, response, file_name, file_size) else: self._write_file(file_path, file) self._import_excel(file_path, response, file_name, file_size) return Response(response.get_data)
def get(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() openid = request.GET.get('openid', None) crunchies = request.GET.get('crunchies', None) user_obj = UserInfo.objects.filter(openid=openid).first() if user_obj: datalist = [] if crunchies: score_obj = ScoreRecord.objects.filter( user_id=user_obj.id, crunchies=crunchies).values("first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "crunchies", "total", "rank") else: score_obj = ScoreRecord.objects.filter( user_id=user_obj.id).values("first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "crunchies", "total", "rank") for item in score_obj: data = self._data_process(item) if data.get("rank") != None: datalist.append(data) response.username = user_obj.username response.phone = user_obj.phone response.avatar = user_obj.avatar response.datalist = datalist response.msg = "Query successfully" else: response.status = 401 response.msg = "User doesn't exist" return Response(response.get_data)
def post(self,request,*args,**kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() username = request.data.get("username",None) password = request.data.get("password",None) if username and password: superuser_obj = UserInfo.objects.filter(username=username).first() if superuser_obj.password == password: response.msg = "login successfully" else: response.status = 401 response.msg = "wrong password" else: response.status = 402 response.msg = "The data is incomplete" return Response(response.get_data)
def post(self, request, *args, **kwargs): response = UserResponse() code = request.data.get('code', None) encryptedData = request.data.get('encryptedData', None) iv = request.data.get('iv', None) if not code: return Response({'message': 'lack code'}, status=status.HTTP_400_BAD_REQUEST) url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code" \ .format(settings.APP_ID, settings.APP_KEY, code) r = requests.get(url) res = json.loads(r.text.decode('utf-8')) openid = res['openid'] if 'openid' in res else None session_key = res['session_key'] if 'session_key' in res else None if not openid: return Response({'message': 'The call to WeChat failed'}, status=status.HTTP_400_BAD_REQUEST) pc = WXBizDataCrypt(settings.APP_ID, session_key) res = pc.decrypt(encryptedData, iv) phone = res.get('phoneNumber') print(phone) user = UserInfo.objects.filter(openid=openid).first() if user: UserInfo.objects.filter(openid=openid).update(phone=phone) token = generate_token(user.id, openid) response.msg = "登录成功" else: user_obj = UserInfo.objects.create( username=self._create_tmp_username(), openid=openid, phone=phone) token = generate_token(user_obj.id, openid) response.msg = "暂无排名,等待后台上传成绩" response.token = token response.phone = phone response.openid = openid return Response(response.get_data)
def get(self, request, *args, **kwargs): """ :param request: :param args: :param kwargs: :return: """ response = UserResponse() crunchies = request.GET.get('crunchies', None) category = request.GET.get('category', None) month = request.GET.get('month', None) types = request.GET.get('type', "total") data_list = [] current_month = datetime.datetime.now().month target_month = self._select_month(month, current_month) if category == 'athletics' and crunchies == "0": if types == "month": score_obj = ScoreRecord.objects.filter( category=0, crunchies=0, created__month=target_month).order_by("-total").values( "user__phone", "total").distinct()[:50] else: score_obj = ScoreRecord.objects.filter( category=0, crunchies=0).order_by("-total").values( "user__phone", "total").distinct()[:50] score_obj = self._distinct_score(score_obj) data_list = self._getdata(score_obj, data_list, crunchies=crunchies) elif category == 'athletics' and crunchies == "1": if types == "month": score_obj = ScoreRecord.objects.filter( category=0, crunchies=1, created__month=target_month).order_by("-total").values( "user__phone", "total").distinct()[:50] else: score_obj = ScoreRecord.objects.filter( category=0, crunchies=1).order_by("-total").values( "user__phone", "total").distinct()[:50] score_obj = self._distinct_score(score_obj) data_list = self._getdata(score_obj, data_list, crunchies=crunchies) elif category == 'athletics' and crunchies == "3": if types == "month": score_obj = ScoreRecord.objects.filter( category=0, crunchies=3, created__month=target_month).order_by("-total").values( "user__phone", "total").distinct()[:50] else: score_obj = ScoreRecord.objects.filter( category=0, crunchies=3).order_by("-total").values( "user__phone", "total").distinct()[:50] score_obj = self._distinct_score(score_obj) data_list = self._getdata(score_obj, data_list, crunchies=crunchies) elif category == "recreation": pass response.datalist = data_list response.msg = "Query successfully" return Response(response.get_data)