def check_data(self, data, uuid): checkUser = User.objects.exclude(uuid=uuid).filter( name=data["name"], address=data["address"], status=1).first() if checkUser: raise ParamError(USER_AREADY_EXIST) checkTel = UserAuth.objects.exclude(userUuid__uuid=uuid).filter( tel=data["tel"]).first() if checkTel: raise ParamError(TEL_AREADY_EXIST) return data
def validate(self, data): checkUser = User.objects.filter(name=data["name"], address=data["address"], status=1).first() if checkUser: raise ParamError(USER_AREADY_EXIST) checkTel = UserAuth.objects.filter(tel=data["tel"], status=1).first() if checkTel: raise ParamError(TEL_AREADY_EXIST) return data
def update(self, request, *args, **kwargs): try: instance = self.get_object() except Exception as e: raise ParamError(USER_WORK_NOT_EXIST) serializer_data = UserWorkPostserializer(data=request.data) result = serializer_data.is_valid(raise_exception=False) if not result: raise ParamError(serializer_data.errors) serializer_data.update_user_work(instance, serializer_data.validated_data) return Response(PUT_SUCCESS)
def update(self, request, *args, **kwargs): # 改 try: instance = self.get_object() except Exception as e: raise ParamError(USER_NOT_EXISTS) serializers = UserUpdateSerializer(data=request.data) result = serializers.is_valid(raise_exception=False) if not result: raise ParamError({ 'code': 2001, 'msg': list(serializers.errors.values())[0][0] }) data = serializers.update_user(instance, serializers.data) return Response(data)
def retrieve(self, request, *args, **kwargs): try: instance = self.get_object() except Exception as e: raise ParamError(USER_WORK_NOT_EXIST) serializer = UserWorkSerializer(instance) return Response(serializer.data)
def create(self, request, *args, **kwargs): serializer_data = UserWorkPostserializer(data=request.data) result = serializer_data.is_valid(raise_exception=False) if not result: raise ParamError(serializer_data.errors) serializer_data.create_user_work(serializer_data.validated_data) return Response(serializer_data.data, status=status.HTTP_201_CREATED)
def retrieve(self, request, *args, **kwargs): # 查 try: instance = self.get_object() except Exception as e: raise ParamError(PERMISSION_NOT_EXISTS) serializer = self.get_serializer(instance) return Response(serializer.data)
def destroy(self, request, *args, **kwargs): try: instance = self.get_object() except Exception as e: raise ParamError(USER_WORK_NOT_EXIST) instance.status = 3 instance.save() return Response(DEL_SUCCESS)
def update(self, request, *args, **kwargs): instance = self.get_object() serializer_data = UserUpdateserializer(data=request.data) result = serializer_data.is_valid(raise_exception=False) if not result: raise ParamError(serializer_data.errors) check_data = serializer_data.check_data(serializer_data.validated_data, instance.uuid) instance = serializer_data.update_user(instance, check_data) auth = serializer_data.update_auth(instance, check_data) return Response(PUT_SUCCESS)
def destroy(self, request, *args, **kwargs): # 删 try: instance = self.get_object() except Exception as e: raise ParamError(USER_NOT_EXISTS) instance.isDelete = True instance.save() serializers = UserBasicSerializer(instance) return Response(serializers.data)
def create(self, request, *args, **kwargs): # 增 serializers = RolePostSerializer(data=request.data) result = serializers.is_valid(raise_exception=False) if not result: raise ParamError({ 'code': 2001, 'msg': list(serializers.errors.values())[0][0] }) # raise ParamError({'code': 2001, 'msg': '用户字段校验有误', **serializers.errors}) data = serializers.create_role(serializers.data) return Response(data)
def destroy(self, request, *args, **kwargs): # 删 try: instance = self.get_object() except Exception as e: raise ParamError(USER_NOT_EXIST) instance.status = 3 instance.save() auth = instance.userAuthkUuid.first() auth.status = 3 auth.save() return Response(DEL_SUCCESS)
def destroy(self, request, *args, **kwargs): # 删 try: instance = self.get_object() except Exception as e: raise ParamError(PERMISSION_NOT_EXISTS) instance.isDelete = True roles = Role.objects.filter(isDelete=False).all() instance.role.remove(*roles) instance.save() serializers = PermissionsBasicSerializer(instance) return Response(serializers.data)
def destroy(self, request, *args, **kwargs): # 删 try: instance = self.get_object() except Exception as e: raise ParamError(ROLE_NOT_EXISTS) instance.isDelete = True users = User.objects.filter(isDelete=False).all() instance.user.remove(*users) # 删除中间表数据, 删除角色的时候,用户关联的角色的中间表也被删除 instance.save() serializers = RoleBasicSerializer(instance) return Response(serializers.data)
def buy(user_uid, goods_list): """ goods_list = [{"id": 111, "amount": 1}] 还应该判断货币是否足够,并扣除货币 """ if not isinstance(goods_list, (list, tuple)): raise ParamError( "goods_list should be type of list, but {} found".format( type(goods_list))) order = None with db.atomic() as transaction: try: user = User.get_with_uid(user_uid) if not user: raise DataNotFound( "user not found for uid={}".format(user_uid)) order = Order.create_data(user=user_uid) for goods in goods_list: goods_id, amount = goods["id"], goods["amount"] goods = Goods.check_amount(goods_id=goods_id, amount=amount) if not goods: raise GoodsNotEnough() if user.money < goods.price: raise MoneyNotEnough() OrderItem.create_data(order=order.uid, goods=goods.uid, price=goods.price, amount=amount) user.money -= goods.price goods.amount -= amount user.save() goods.save() except GoodsNotEnough: transaction.rollback() order = None except MoneyNotEnough: transaction.rollback() order = None except Exception as e: print(e) transaction.rollback() order = None return order
def validate(self, data): work = Work.objects.filter(uuid=data["workUuid"], status=1).first() if not work: raise ParamError(WORK_NOT_EXIST) return data
def validate(self, data): userUuid = data["userUuid"] user = User.objects.filter(uuid=userUuid, status=1).first() if not user: raise ParamError(USER_NOT_EXIST) return data