def create(self, validated_data): user_course = [] for course_id in validated_data['course_ids']: user_course.append( UserCourse(user=self.context['request'].user, order=validated_data['order'], project=validated_data['project'], course_id=course_id)) user_courses = UserCourse.objects.bulk_create(user_course) user_all_course = UserCourse.objects.filter( user=self.context['request'].user).count() user_max_course = Order.objects.filter( user=self.context['request'].user, status='CONFIRMED').values_list( 'orderchartrelation__chart__course_num') user_info = UserInfo.objects.filter(user=self.context['request'].user, student_status__in=[ 'NEW', 'PERSONAL_FILE', 'ADDED_CC', 'SUPPLY_ORDER', 'PAYMENT_CONFIRM', 'TO_CHOOSE_COURSE' ]).exists() if user_info and user_all_course == sum( [number[0] for number in user_max_course]): change_student_status(self.context['request'].user.id, 'PICKUP_COURSE') return user_courses[0]
def additional_order_update(self, order, coupon_list, chart_ids, user): """订单创建后,更新与订单相关信息""" if coupon_list: # 更新优惠券状态 for coupon_id in coupon_list: if UserCoupon.objects.filter(user=user, coupon_id=coupon_id, status='TO_USE').exists(): UserCoupon.objects.filter( user=user, coupon_id=coupon_id, status='TO_USE').update(status='LOCKED') # 创建订单与商品关系 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') # 操作记录 HistoryFactory.create_record(operator=self.context['request'].user, source=order, key='CREATE', remark='创建了订单', source_type='ORDER') order_auto_notice_message(order=order, user=user) user_info = UserInfo.objects.filter( user=user, student_status__in=['NEW', 'PERSONAL_FILE', 'ADDED_CC']).exists() if user_info: change_student_status(user.id, 'SUPPLY_ORDER') return
def course_credit_switch(self, request): """学分转换证明""" res = self.common_handle(request, 'course_credit_switch') if request.method == 'PUT': if not UserCourse.objects.filter(user_id=request.user.id, credit_switch_status__in=[ 'POSTED', 'PRE_POSTED' ]).exists(): change_student_status(request.user.id, 'SWITCHED_COURSE') return Response(res)
def update(self, request, *args, **kwargs): instance = super().update(request, *args, **kwargs) score_auto_notice_message(instance.data.get('course'), instance.data.get('user_info')) user_queryset = UserCourse.objects.filter(user_id=instance.data.get( 'user_info')['user']).values_list('score_grade') user_scores_status = all([score[0] for score in user_queryset]) if user_scores_status: change_student_status( instance.data.get('user_info')['user'], 'AFTER_SCORE') return instance
def weixin_authorize(self, validated_data): res = WeixinServer.code_authorize(validated_data['code']) if not (res.get('access_token') and res.get('openid')): raise serializers.ValidationError('无效的code值, 微信网页认证失败') try: user_info = WeixinServer.get_web_user_info(access_token=res['access_token'], openid=res['openid']) except: user_info = None if user_info.get('errorcode', 0) != 0: raise serializers.ValidationError('user info 获取错误') # 创建用户 user = User.objects.filter(openid=res['openid']).first() if not user: if not user_info: user = User.objects.create(**{ 'channel_id': validated_data.get('channel_id', 1), 'username': res['openid'], 'role': 'STUDENT', 'openid': res['openid'], }) logger.info('--------------------------------------无unionid 创建用户') logger.info(user.id) else: user = User.objects.create(**{ 'channel_id': validated_data.get('channel_id', 1), 'username': res['openid'], 'role': 'STUDENT', 'openid': res['openid'], 'unionid': user_info.get('unionid') }) logger.info('++++++++++++++++++++++++++++++++++++++有unionid 创建用户') logger.info(user.id) ticket = AuthorizeServer.create_ticket(user.id) user.last_login = datetime.datetime.now() user.save() if validated_data.get('channel_id'): UserChannel.objects.create(channel_id=validated_data.get('channel_id', 1), user=user) # 创建用户信息 student_info = UserInfo.objects.filter(user=user).first() if not student_info: student_info = UserInfo.objects.create(user=user) change_student_status(user.id, 'NEW') if user_info.get('unionid'): student_info.unionid = user_info.get('unionid') student_info.headimgurl = user_info['headimgurl'] student_info.wx_name = user_info['nickname'] student_info.openid = res['openid'] student_info.save() logging.info('--->' + str(datetime.datetime.now())) return user, student_info, ticket
def create(self, validated_data): instance = super().create(validated_data) self.create_course_notice(validated_data) user_all_course = UserCourse.objects.filter(user=instance.user).count() user_max_course = Order.objects.filter(user=instance.user, status='CONFIRMED').values_list( 'orderchartrelation__chart__course_num') user_info = UserInfo.objects.filter(user=instance.user, student_status__in=['NEW', 'PERSONAL_FILE', 'ADDED_CC', 'SUPPLY_ORDER', 'PAYMENT_CONFIRM', 'TO_CHOOSE_COURSE']).exists() if user_info and user_all_course == sum([number[0] for number in user_max_course]): change_student_status(instance.user.id, 'PICKUP_COURSE') return instance
def update(self, request, *args, **kwargs): instance = super().update(request, *args, **kwargs) user_id_dict = UserCourse.objects.filter( id=kwargs.get('pk')).values_list('user_id').first() switch_auto_notice_message( instance.data.get('user_info'), instance.data.get('course'), instance.data.get('credit_switch_status'), ) if not UserCourse.objects.filter( user_id=user_id_dict[0], credit_switch_status='PRE_POSTED').exists(): change_student_status(user_id_dict[0], 'SWITCH_CREDIT') return instance
def personal_file(self, request, pk): instance = self.get_object() if request.method != 'GET': partial = True if request.method == 'PATCH' else False serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) instance = self.get_object() if UserInfo.objects.filter(user_id=pk, student_status='PERSONAL_FILE'): change_student_status(pk, 'ADDED_CC') return Response(self.get_serializer(instance).data)
def student_confirm_course(self, request): """学生审课""" res = self.common_handle(request, 'student_confirm_course') if request.method == 'PUT': queruset = Order.objects.filter( user_id=request.user.id, status='CONFIRMED').values( 'orderchartrelation__chart__course_num') choose_course = sum([ order.get('orderchartrelation__chart__course_num') for order in queruset ]) if choose_course == UserCourse.objects.filter( user_id=request.user.id, status='TO_CONFIRM').count(): change_student_status(request.user.id, 'TO_CONFIRMED') return Response(res)
def sales_man(self, request, pk): """ 获取销售人员信息 """ user = request.user if request.method == 'GET': res = auto_assign_sales_man(user) change_student_status(user.id, 'PERSONAL_FILE') return Response(res) else: data = request.data data['user'] = user serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response({'msg': '操作成功'})
def create(self, validated_data): order = validated_data['order'] order.status = 'TO_CONFIRM' order.currency = validated_data.pop('currency') order.payment = validated_data.pop('payment') order.save(update_fields=['status', 'currency', 'payment']) instance = super().create(validated_data) HistoryFactory.create_record(operator=self.context['request'].user, source=instance.order, key='UPDATE', remark='上传了订单支付信息', source_type='ORDER') if not StudentScoreDetail.objects.filter(user=order.user).exists(): StudentScoreDetail.objects.create(user=order.user) user_info = UserInfo.objects.filter(user=self.context['request'].user, student_status__in=[ 'NEW', 'PERSONAL_FILE', 'ADDED_CC', 'SUPPLY_ORDER' ]).exists() if user_info: change_student_status(self.context['request'].user.id, 'PAYMENT_CONFIRM') return instance
def confirm_course(self, request): if request.method == 'GET': user_id = self.request.query_params.get('user') try: user = User.objects.get(id=user_id) except: raise exceptions.ValidationError('无效的user') user_course = UserCourse.objects.filter(user=user) return Response( ConfirmCourseSerializer(user_course, many=True).data) else: serializer = ConfirmUserCourseSerializer(data=request.data) serializer.is_valid(raise_exception=True) data = serializer.validated_data self.queryset.filter( user=data['user'], course=data['course'], project=data['project'], order=data['order']).update(status=data['status']) instance = self.queryset.filter(user=data['user'], course=data['course'], order=data['order']).first() confirm_auto_notice_message(usercourse=instance, user=data['user']) course_not_all_pass = UserCourse.objects.filter( user=data['user']).exclude(status='PASS').exists() userinfo_status = UserInfo.objects.filter( user=data['user'], student_status__in=[ 'NEW', 'PERSONAL_FILE', 'ADDED_CC', 'SUPPLY_ORDER', 'PAYMENT_CONFIRM', 'TO_CHOOSE_COURSE', 'PICKUP_COURSE', 'TO_CONFIRMED' ]) if userinfo_status.exists() and not course_not_all_pass: change_student_status(data['user'].id, 'CONFIRMED_COURSE') # if userinfo_status.filter(student_status='CONFIRMED_COURSE').exists(): # change_student_status(data['user'].id, 'AFTER_SCORE') return Response(self.get_serializer(instance).data)
def update(self, instance, validated_data): if validated_data.get('status') == 'CONFIRMED': status = 'CONFIRMED' remark = '订单支付成功:%s' % validated_data.get('remark') pay_fee = validated_data.get('pay_fee') confirm_status = '订单支付成功' confirm_remark = '您的订单审核成功,请联系您的课程顾问,开始选课吧!' user_info = UserInfo.objects.filter(user=instance.user, student_status__in=['NEW', 'PERSONAL_FILE', 'ADDED_CC', 'SUPPLY_ORDER', 'PAYMENT_CONFIRM']).exists() if user_info: change_student_status(instance.user.id, 'TO_CHOOSE_COURSE') if pay_fee: instance.pay_fee = pay_fee elif validated_data.get('status') == 'CONFIRM_FAILED': status = 'CONFIRM_FAILED' remark = '订单支付失败,原因为: %s' % validated_data.get('remark') confirm_status = '订单支付失败' confirm_remark = '很抱歉,您的订单审核失败,失败原因为: %s' % validated_data.get('remark') else: raise exceptions.ValidationError('请传入正确的status参数') if instance.status != 'TO_CONFIRM': raise exceptions.ValidationError('仅能操作待确认状态下的订单') instance.status = status instance.save() if instance.coupon_list: # 如果使用了优惠券,更新优惠券的状态 coupon_list = json.loads(instance.coupon_list) UserCoupon.objects.filter(user=instance.user, coupon_id__in=coupon_list).update( status='USED' if status == 'CONFIRMED' else 'TO_USE') self.notice_to_user(instance, confirm_status, confirm_remark) HistoryFactory.create_record(operator=self.context['request'].user, source=instance, key='UPDATE', remark=remark, source_type='ORDER') return instance
def destroy(self, request, *args, **kwargs): user_instance = UserCourse.objects.filter( id=kwargs.get('pk')).values('user_id').first() instance = super().destroy(request, *args, **kwargs) change_student_status(user_instance.get('user_id'), 'TO_CHOOSE_COURSE') return instance