def cancel(self, request, pk): """取消订单""" instance = self.get_object() if instance.status == 'CANCELED': raise exceptions.ValidationError('订单已被取消') elif instance.status != 'TO_PAY': raise exceptions.ValidationError('无法取消该订单') instance.status = 'CANCELED' instance.save() order_coupons = OrderCouponRelation.objects.filter( order=instance).values_list('coupon__coupon', flat=True) if order_coupons: # 如果使用了优惠券,将优惠券置为可用状态 StudentCoupon.objects.filter( coupon_id__in=list(order_coupons)).update(status='TO_USE') OperationLog.objects.create(operator=request.user, operation=2, target=request.user.id, ip_address=get_ip_address(request), description='取消了订单') if request.user.student_status == 'SUPPLY_ORDER': request.user.student_status = 'PERSONAL_FILE' request.user.save() return Response(self.serializer_class(instance).data)
def basic_info_update(self, request): params = request.data user_id = params.get('id') if not user_id: raise serializers.ValidationError('user_id为空') is_leader = None if 'is_leader' in params.keys(): is_leader = params.pop('is_leader') if 'qr_code' in params.keys(): qr_code = params.pop('qr_code') image_base64 = str(qr_code)[str(qr_code).find(',') + 1:] image_data = base64.b64decode(image_base64) file_name = str( uuid.uuid4()) + '.' + str(qr_code)[str(qr_code).find('/') + 1:str(qr_code).find(';')] abs_path = '%s/images/user_qrcode/%s' % (settings.MEDIA_ROOT, file_name) with open(abs_path, 'wb') as pic: pic.write(image_data) params['qr_code'] = 'images/user_qrcode/' + file_name UserInfo.objects.filter(id=user_id).update(**params) data = UserInfo.objects.filter(id=user_id).first() if is_leader: Department.objects.filter(id=data.department_id.id).update( leader_id=data.id, leader_name=data.name) log_to_db(operator=user_id, operation=UserOperationType.UPDATE_INFO.value, ip_address=get_ip_address(request), description='修改用户信息', operator_type=1) return Response()
def create(self, request, *args, **kwargs): is_leader = False if 'is_leader' in request.data.keys(): leader = request.data.pop('is_leader') is_leader = True if leader == 'true' else False request.data['password'] = md5('123456') qr_code = request.data.get('qr_code') if qr_code: image_base64 = str(qr_code)[str(qr_code).find(',') + 1:] image_data = base64.b64decode(image_base64) file_name = str( uuid.uuid4()) + '.' + str(qr_code)[str(qr_code).find('/') + 1:str(qr_code).find(';')] abs_path = '%s/images/user_qrcode/%s' % (settings.MEDIA_ROOT, file_name) with open(abs_path, 'wb') as pic: pic.write(image_data) request.data['qr_code'] = 'images/user_qrcode/%s' % file_name department = Department.objects.filter( id=request.data['department_id']).first() request.data['department_name'] = department.name role = Role.objects.filter(id=request.data['role_id']).first() request.data['role_name'] = role.name result = super().create(request, *args, **kwargs) if is_leader: department.leader_id = result.data['id'] department.leader_name = result.data['name'] department.save() log_to_db(operator=result.data['id'], operation=UserOperationType.UPDATE_INFO.value, ip_address=get_ip_address(request)) return result
def get_menu_tree(self, request): log_to_db(operator='1', operation=1, ip_address=get_ip_address(request), description='fwef', operator_type=1) result = MenuSerializer(Menu.objects.all().order_by('id'), many=True).data json_tree = generate_menu_tree(result[1:], result[0]) return Response(json_tree['children'])
def login(self, request): params = request.data username = params.get('username') password = params.get('password') if not all((username, password)): raise serializers.ValidationError('用户名和密码不能为空') result = UserInfo.objects.filter( username=username, password=password).values('id').first() if result: token_data = generate_token(result['id'], 0) response = Response() response.setdefault('token', token_data) response['Access-Control-Expose-Headers'] = 'token' log_to_db(operator=result['id'], operation=UserOperationType.LOGIN.value, ip_address=get_ip_address(request), description='登录系统', operator_type=1) return response else: raise serializers.ValidationError('用户名或密码验证失败')
def additional_order_update(self, order, coupon_list, chart_ids, user): """订单创建后,更新与订单相关信息""" if coupon_list: # 更新优惠券状态 for coupon_id in coupon_list: if StudentCoupon.objects.filter(user=user, coupon_id=coupon_id, status='TO_USE').exists(): StudentCoupon.objects.filter( user=user, coupon_id=coupon_id, status='TO_USE').update(status='LOCKED') OrderCouponRelation.objects.create(**{ 'coupon_id': coupon_id, 'order': order, 'remark': '创建了订单' }) # 创建订单与商品关系 order_chart = [] for chart_id in chart_ids: order_chart.append( OrderChartRelation(order=order, chart_id=chart_id)) OrderChartRelation.objects.bulk_create(order_chart) # 更新购物车 ShoppingChart.objects.filter(id__in=chart_ids).update(status='ORDERED') # 操作记录 OperationLog.objects.create(operator=user, operation=1, target=user.id, ip_address=get_ip_address( self.context['request']), description='创建了订单') # order_auto_notice_message(order=order, user=user) if user.student_status in ['NEW', 'PERSONAL_FILE']: change_student_status(user.id, 'SUPPLY_ORDER') return