def create(self, request, *args, **kwargs): try: yonghu_obj = self.get_person(request) except KeyError: return Response(ReturnCode(1, msg='not login.')) title = request.data.get('title') content = request.data.get('content') status = 0 # 图片,前端这里用json序列化image数组 imagePaths = self.get_image_path(request) try: feedback_obj = Feedback() feedback_obj.title = title feedback_obj.content = content feedback_obj.status = status feedback_obj.yonghu = yonghu_obj feedback_obj.save() for imagepath in imagePaths: imgPath_obj = ImagePath() imgPath_obj.imgPath = imagepath imgPath_obj.content_type = feedback_obj imgPath_obj.object_id = feedback_obj.pk imgPath_obj.save() serializer = FeedbackSerializer(feedback_obj) return Response(ReturnCode(0, data=serializer.data)) except Exception: return Response(ReturnCode(1, msg='field error.'))
def update(self, request, *args, **kwargs): ''' 用户身份认证 :param request: :param args: :param kwargs: :return: ''' user = self.get_object() serializer = YonghuSerializer(user) # 未验证 if not user.is_auth: # user = request.user.pk UserName = request.data.get('UserName') Password = request.data.get('Password') university = UniversityLogin() if university.UscLogin(UserName, Password): uscinfo = UscInfo() uscinfo.UserName = UserName uscinfo.Password = Password uscinfo.user = user uscinfo.save() user.is_auth = True user.save() return Response(ReturnCode(0, data=serializer.data), status=200) else: return Response(ReturnCode(1, msg='登录失败'), status=400) else: return Response(ReturnCode(1, msg='请勿重复验证'), status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): ''' 用户身份认证 :param request: :param args: :param kwargs: :return: ''' user = self.get_object() serializer = YonghuSerializer(user) # 未验证 if not user.is_auth_new: # user = request.user.pk UserName = request.data.get('UserName') Password = request.data.get('Password') usclogin = UscLogin(UserName, Password) if usclogin.UscLoginNew(): newuscinfo = NewUSCINFO() newuscinfo.UserName = UserName newuscinfo.Password = Password newuscinfo.user = user newuscinfo.save() user.is_auth_new = True user.save() return Response(ReturnCode(0, data=serializer.data), status=200) else: return Response(ReturnCode(1, msg='登录失败'), status=400) else: return Response(ReturnCode(1, msg='请勿重复验证'), status=status.HTTP_400_BAD_REQUEST)
def list(self, request, *args, **kwargs): try: yonghu_obj = self.get_person(request) except KeyError: return Response(ReturnCode(1, msg='not login.')) feedbacks = yonghu_obj.feedbacks.all() serializers = FeedbackSerializer(feedbacks, many=True) return Response(ReturnCode(0, data=serializers.data))
def getTimetable(request): openid = request.data.get('openid') assert openid is not None, 'openid is None' print(openid) user = Yonghu.objects.get(pk=openid) if user.is_auth_new: uscNew = user.uscNew result = NewTimetable(uscNew.UserName, uscNew.Password).run() return Response(ReturnCode(0, data=result)) else: return Response(ReturnCode(1, msg='身份未认证'))
def timeTable(request): timename = request.query_params.get('TimeName') openid = request.query_params.get('openid') print(timename) user = Yonghu.objects.get(pk=openid) print(user) if user.is_auth: usc = user.usc result = Timetable.USC_Timetable(usc.UserName, usc.Password, timename) return Response(ReturnCode(0, data=result)) else: return Response(ReturnCode(1, msg='账号身份未认证'))
def create(self, request, *args, **kwargs): obj_id = self.request.query_params.get('id') try: commody_obj = Commody.objects.get(pk=int(obj_id)) except exceptions.ObjectDoesNotExist: return Response(ReturnCode(1, msg='commody object do not exists.')) return self.create_reply_message_and_add_reply_num(commody_obj)
def list(self, request, *args, **kwargs): ''' 列出指定用户发布的商品信息 :param request: :param args: :param kwargs: :return: ''' try: pk = request.session['pk'] except KeyError: return Response(ReturnCode(1, msg='must login.'), status=400) yonghu_obj = Yonghu.objects.get(pk=pk) commody_obj = yonghu_obj.commody.all() serializer = CommodySerializer(commody_obj, many=True) return Response(ReturnCode(0, data=serializer.data))
def logout_view(request): ''' 用户注销登录 :param request: :return: ''' request.session.clear() return Response(ReturnCode(0), status=status.HTTP_200_OK)
def qq_login(request): ''' QQ登录 :param request: :return: ''' code = request.data.get('code') # print(type(code)) userInfo = request.data.get('userInfo') if type(code) != type('str'): # print('运行') return Response(ReturnCode(1, msg="code出错"), status=status.HTTP_400_BAD_REQUEST) else: res = code2Session.c2s(settings.QQ_APPID, code) # print(res) if res.get('errcode') == 0: openid = res.get('openid') # print('openid: ', openid) userInfo['openid'] = openid user = Yonghu.objects.filter(openid=openid) # print(userInfo) ### 不存在,重新创建 if len(user) == 0: serializer = YonghuSerializer(data=userInfo) print(serializer.is_valid()) if serializer.is_valid(): serializer.save() else: return Response(ReturnCode(1, msg=serializer.errors), status=400) ### 存在,修改 else: serializer = YonghuSerializer(user[0], data=userInfo) if serializer.is_valid(): serializer.save() else: return Response(ReturnCode(1, msg=f'{serializer.errors}'), status=400) request.session['pk'] = openid return Response(ReturnCode(0, data=serializer.data), status=200) return Response(ReturnCode(1, msg='登录失败,请重新登录'), status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): ''' 创建商品信息 :param request: :param args: :param kwargs: :return: ''' data = request.data.copy() try: type_id = data.get('type_id') type_obj = Type.objects.get(pk=int(type_id)) yonghu_obj = self.get_person(request) except KeyError: return Response(ReturnCode(1, msg='object do not exists.')) try: commody = Commody() commody.name = data.get('name') commody.description = data.get('description') commody.price = data.get('price') commody.type = type_obj commody.qq = data.get('qq') commody.wx = data.get('wx') commody.phone_number = data.get('phone_number') commody.yonghu = yonghu_obj commody.save() ct = ContentType.objects.get_for_model(commody) imagePaths = self.get_image_path(request) for imagePath in imagePaths: img_path_obj = ImagePath() img_path_obj.content_type = ct img_path_obj.object_id = commody.pk img_path_obj.imgPath = imagePath img_path_obj.save() serializer = CommodySerializer(commody) return Response(ReturnCode(0, data=serializer.data)) except exceptions.FieldDoesNotExist: return Response( ReturnCode( 1, msg='qq wx and phone_number must have at least one.')) except exceptions.FieldError: return Response(ReturnCode(1, msg='field error.'))
def searchCommodyByNameOrDescription(request): ''' 根据商品名称、描述搜索商品 :param request: :return: ''' query_set = request.query_params.get('q') commody_obj = Commody.objects.filter( Q(name__icontains=query_set) | Q(description__icontains=query_set)) serializer = CommodySerializer(commody_obj, many=True) return Response(ReturnCode(0, data=serializer.data))
def create_reply_message_and_add_reply_num(self, obj): ''' 提交留言增加reply_num事务,乐观锁,循环三次 ''' ct = ContentType.objects.get_for_model(obj) try: yonghu_obj = self._get_create_yonghu() except KeyError: return Response(ReturnCode(1, msg='not login.')) except exceptions.ObjectDoesNotExist: return Response(ReturnCode(1, msg='user not find, cookie error.')) read_and_reply_num_obj = self._get_or_create_read_and_reply_num_model( ct, obj) msg = self.request.data.get('msg') reply_yonghu_pk = self.request.data.get('reply_yonghu_pk') reply_yonghu_obj = Yonghu.objects.get(pk=reply_yonghu_pk) floor = int(self.request.query_params.get('floor')) sid = transaction.savepoint() num = 0 while num < 3: reply_num = obj.reply_num reply_message = ReplyMessage() reply_message.msg = msg reply_message.yonghu = yonghu_obj reply_message.content_type = ct reply_message.object_id = obj.pk reply_message.floor = floor reply_message.reply_yonghu = reply_yonghu_obj reply_message.save() read_and_reply_num_obj.reply_num += 1 read_and_reply_num_obj.save() new_reply_num = obj.reply_num if reply_num == new_reply_num - 1: return Response(ReturnCode(0)) else: num += 1 transaction.savepoint_rollback(sid) return Response(ReturnCode(1, msg='create message error.'))
def create_main_message_and_add_main_reply_num(self, obj): ''' 创建主楼评论并且增加回复数 ''' ct = ContentType.objects.get_for_model(obj) try: yonghu_obj = self._get_create_yonghu() except KeyError: return Response(ReturnCode(1, msg='not login.')) except exceptions.ObjectDoesNotExist: return Response(ReturnCode(1, msg='user not find, cookie error.')) read_and_reply_num_obj = self._get_or_create_read_and_reply_num_model( ct, obj) msg = self.request.data.get('msg') sid = transaction.savepoint() num = 0 while num < 3: reply_num = obj.reply_num main_floor_num = obj.main_floor_num main_message = MainMessage() main_message.msg = msg main_message.yonghu = yonghu_obj main_message.content_type = ct main_message.object_id = obj.pk main_message.floor = main_floor_num + 1 main_message.save() read_and_reply_num_obj.reply_num += 1 read_and_reply_num_obj.main_floor_num += 1 read_and_reply_num_obj.save() new_reply_num = obj.reply_num new_main_floor_num = obj.main_floor_num if reply_num == new_reply_num - 1 and main_floor_num == new_main_floor_num - 1: return Response(ReturnCode(0)) else: num += 1 transaction.savepoint_rollback(sid) return Response(ReturnCode(1, msg='create message error.'))
def update(self, request, *args, **kwargs): ''' 更新商品信息 :param request: :param args: :param kwargs: :return: ''' commody_obj = self.get_object() data = request.data.copy() type_id = data.get('type_id') # 删除参数中指定的字段,防止框架根据参数自动修改用户不能修改的字段 if data.get('is_delete'): data.pop('is_delete') if data.get('create_time'): data.pop('create_time') serializer = CommodySerializer(commody_obj, data=data) if serializer.is_valid(): try: if type_id: try: type_new = Type.objects.get(pk=int(type_id)) except exceptions.ObjectDoesNotExist: return Response( ReturnCode(1, msg='type objects do not exist.')) commody_obj.type = type_new commody_obj.save() serializer.save() return Response( ReturnCode(0, msg='success.', data=serializer.data)) except exceptions.FieldDoesNotExist: return Response( ReturnCode( 1, msg='qq wx and phone_number must have at least one.')) else: return Response(ReturnCode(1, msg='data invalid.'))
def create(self, request, *args, **kwargs): ''' 上传图片 :param request: :param args: :param kwargs: :return: ''' imgPaths = request.data.get('imagePath') ct = ContentType.objects.get_for_model(self.get_object()) img_path_obj = ImagePath() img_path_obj.content_type = ct img_path_obj.object_id = self.get_object().pk img_path_obj.imgPath = imgPaths img_path_obj.save() serializer = CommodySerializer(self.get_object()) return Response(ReturnCode(0, data=serializer.data))
def retrieve(self, request, *args, **kwargs): if self.add_read_num(): return super().retrieve(self, request, *args, **kwargs) else: return Response(ReturnCode(1, msg='request error.'), status=404)