class StudentHandler(StarkHandler): def display_score(self, request, obj=None, is_header=None): if is_header: return '积分管理' record_url = reverse('stark:manage_system_scorerecord_list', kwargs={'student_id': obj.pk}) return mark_safe('<a target = "_blank" href="%s">%s</a>' % (record_url, obj.score)) list_display = [ 'customer', 'qq', 'mobile', 'emergency_contract', StarkHandler.get_m2m_text('已报班级', 'class_list'), StarkHandler.get_choice_text('状态', 'student_status'), display_score, StarkHandler.display_edit ] has_add_btn = None model_form_class = New_Student_forms def get_urls(self): patterns = [ url(r'list/$', self.wrapper(self.changelist), name=self.get_list_url_name), # url(r'add/$', self.wrapper(self.add_view), name=self.get_add_url_name), url(r'change/(?P<pk>\d+)/$', self.wrapper(self.change_view), name=self.get_change_url_name), # url(r'del/(?P<pk>\d+)/$', self.wrapper(self.delete_view), name=self.get_delete_url_name) ] patterns.extend(self.extra_urls( )) # 这个self并不是这个基类的self 而是最开始从传入的那个handler开始寻找有没有extra_urls return patterns
class ClassListHandler(StarkHandler): def display_course(self, request, obj=None, is_header=None, *args, **kwargs): if is_header: return '班级' return '%s(%s)期' % (obj.course.name, obj.semester) def display_course_record(self, request, obj=None, is_header=None, *args, **kwargs): if is_header: return '上课记录' record_url = reverse('stark:manage_system_courserecord_normal_list', kwargs={'class_id': obj.pk}) return mark_safe('<a target = "_blank" href="%s">上课记录</a>' % record_url) model_form_class = New_datetime_forms list_display = [ 'school', StarkHandler.get_datetime_text('开班日期', 'start_date'), StarkHandler.get_m2m_text('任课老师', 'tech_teacher'), display_course, display_course_record, StarkHandler.display_edit_del ]
class PrivateCustomerHandler(StarkHandler): def display_record(self, request, obj=None, is_header=None): if is_header: return '跟进记录' record_url = reverse('stark:manage_system_consultrecord_list', kwargs={'customer_id': obj.pk}) return mark_safe('<a target="blank" href="%s">查看跟进</a>' % record_url) def display_pay_record(self, request, obj=None, is_header=None): if is_header: return '缴费记录' record_url = reverse('stark:manage_system_paymentrecord_list', kwargs={'customer_id': obj.pk}) return mark_safe('<a target="blank" href="%s">缴费</a>' % record_url) model_form_class = New_private_Customer_forms list_display = [StarkHandler.display_checkbox, 'name', 'qq', StarkHandler.get_choice_text('状态', 'status'), StarkHandler.get_m2m_text('咨询课程', 'course'), display_pay_record, display_record] def get_queryset(self, request, *args, **kwargs): # request.session['user_info']['id'] current_user_id = request.session['user_info']['id'] return self.model_class.objects.filter(consultant_id=current_user_id) def save(self, request, form, is_update, *args, **kwargs): if not is_update: current_user_id = request.session['user_info']['id'] form.instance.consultant_id = current_user_id form.save() def multi_remove(self, request, *args, **kwargs): ''' 移除到公户(如果想要定制执行成功后的返回值,那么就为action制定返回值 意思就是有一个返回值话 就会执行返回值 比如跳转到百度页面) :return: ''' current_user_id = request.session['user_info']['id'] pk_list = request.POST.getlist('pk') self.model_class.objects.filter(id__in=pk_list, consultant_id=current_user_id).update(consultant=None) return redirect(self.reverse_list_url()) multi_remove.text = '移除到公户' action_list = [multi_remove, ]
class PublicCustomerHandler(StarkHandler): def display_record(self,request, obj=None, is_header=None): if is_header: return '跟进记录' record_url = self.reverse_record_url(pk=obj.pk) return mark_safe('<a target="_blank" href="%s">查看跟进</a>' % record_url) list_display = [StarkHandler.display_checkbox, 'name', 'qq', StarkHandler.get_choice_text('状态', 'status'), StarkHandler.get_m2m_text('咨询课程', 'course'), display_record] def get_queryset(self, request, *args, **kwargs): return self.model_class.objects.filter(consultant__isnull=True) model_form_class = New_Public_Customer_forms @property def get_record_url_name(self): return self.get_url_name('record_view') def reverse_record_url(self, *args, **kwargs): ''' 生成带有原搜索条件的跟进记录的URL :param args: :param kwargs: :return: ''' return self.reverse_commons_url(self.get_record_url_name, *args, **kwargs) def extra_urls(self): patterns = [ url(r'^record/(?P<pk>\d+)/$', self.wrapper(self.record_view), name=self.get_record_url_name) ] return patterns def record_view(self, request, pk): ''' 查看跟进记录 :param request: :param pk: :return: ''' record_list = models.ConsultRecord.objects.filter(customer_id=pk) return render(request, 'record_view.html', {'record_list': record_list}) def multi_apply(self, request, *args, **kwargs): ''' 申请到私户(如果想要定制执行成功后的返回值,那么就为action制定返回值 意思就是有一个返回值话 就会执行返回值 比如跳转到百度页面) :return: ''' current_user_id = request.session['user_info']['id'] pk_list = request.POST.getlist('pk') private_customer_count = models.Customer.objects.filter(consultant_id=current_user_id, status=2).count() if (private_customer_count + len(pk_list)) > models.Customer.MAX_PRIVATE_CUSTOMER_COUNT: return HttpResponse('做人不要太贪心了,你的账户里面已经有了%s个,你还能申请到私户的学员有%s'% ( private_customer_count, models.Customer.MAX_PRIVATE_CUSTOMER_COUNT - private_customer_count)) flag = False with transaction.atomic():#事务 #在数据库中加锁 origin_queryset = models.Customer.objects.filter(id__in=pk_list, status=2, consultant__isnull=True).select_for_update() if len(origin_queryset) == len(pk_list): models.Customer.objects.filter(id__in=pk_list, status=2, consultant__isnull=True).update(consultant_id=current_user_id) flag = True return redirect(self.reverse_list_url()) if not flag: return HttpResponse('手速太慢,选中的客户已被其他人申请,请重新选择') multi_apply.text = '批量添加到私户' action_list = [multi_apply, ]
class PaymentRecordHandler(StarkHandler): model_form_class = New_PaymentRecord_forms list_display = [ StarkHandler.get_choice_text('费用类型', 'pay_type'), 'consultant', 'paid_fee', 'class_list', StarkHandler.get_choice_text('状态', 'confirm_status') ] def get_urls(self): patterns = [ url(r'list/(?P<customer_id>\d+)/$', self.wrapper(self.changelist), name=self.get_list_url_name), url(r'add/(?P<customer_id>\d+)/$', self.wrapper(self.add_view), name=self.get_add_url_name), ] patterns.extend(self.extra_urls( )) # 这个self并不是这个基类的self 而是最开始从传入的那个handler开始寻找有没有extra_urls return patterns def get_queryset(self, request, *args, **kwargs): customer_id = kwargs.get('customer_id') current_user_id = request.session['user_info']['id'] return self.model_class.objects.filter( customer_id=customer_id, customer__consultant_id=current_user_id) def Basic_Forms(self, model_class, add_or_change, request, pk, *args, **kwargs): # 如果当前客户有学生信息,则使用PaymentRecordModelForm;否则StudentPaymentRecordModelForm customer_id = kwargs.get('customer_id') student_exists = models.Student.objects.filter( customer_id=customer_id).exists() if not student_exists: return New_StudentPaymentRecord_forms(self, model_class, add_or_change, request, pk, *args, **kwargs) return New_PaymentRecord_forms(self, model_class, add_or_change, request, pk, *args, **kwargs) def save(self, request, form, is_update, *args, **kwargs): customer_id = kwargs.get('customer_id') current_user_id = request.session['user_info']['id'] object_exists = models.Customer.objects.filter( id=customer_id, consultant_id=current_user_id).exists() if not object_exists: return HttpResponse('非法操作') form.instance.customer_id = customer_id form.instance.consultant_id = current_user_id form.save() #创建学员信息 class_list = form.cleaned_data['class_list'] fetch_student_object = models.Student.objects.filter( customer_id=customer_id).first() if not fetch_student_object: qq = form.cleaned_data['qq'] mobile = form.cleaned_data['mobile'] emergency_contract = form.cleaned_data['emergency_contract'] student_object = models.Student.objects.create( customer_id=customer_id, qq=qq, mobile=mobile, emergency_contract=emergency_contract) student_object.class_list.add(class_list.id) else: fetch_student_object.class_list.add(class_list.id)
class CourseRecordHandler(StarkHandler): model_form_class = New_CourseRecord_forms def display_edit_del(self, request, obj=None, is_header=None, *args, **kwargs): if is_header: return '操作' class_id = kwargs.get('class_id') tpl = '<a href="%s">编辑</a> <a href="%s">删除</a>' % ( self.reverse_change_url(pk=obj.pk, class_id=class_id), self.reverse_delete_url(pk=obj.pk, class_id=class_id)) return mark_safe(tpl) def display_attendance(self, request, obj=None, is_header=None, *args, **kwargs): if is_header: return '考勤' name = '%s:%s' % (self.site.namespace, self.get_url_name('attendance')) attendance_url = reverse(name, kwargs={'course_record_id': obj.pk}) tpl = '<a target="_blank" href="%s">考勤</a>' % attendance_url return mark_safe(tpl) def display_homework(self, request, obj=None, is_header=None, *args, **kwargs): if is_header: return '作业' homework_url = reverse( 'stark:manage_system_courserecord_homework_list', kwargs={'course_record_id': obj.pk}) return mark_safe('<a target="_blank" href="%s">作业发布</a>' % homework_url) def display_homeworkscore(self, request, obj=None, is_header=None, *args, **kwargs): if is_header: return '作业批改' name = '%s:%s' % (self.site.namespace, self.get_url_name('homeworkscore')) attendance_url = reverse(name, kwargs={'course_record_id': obj.pk}) tpl = '<a target="_blank" href="%s">作业批改</a>' % attendance_url return mark_safe(tpl) list_display = [ StarkHandler.display_checkbox, 'class_object', 'day_num', 'teacher', StarkHandler.get_datetime_text('日期', 'date'), display_homework, display_attendance, display_homeworkscore, display_edit_del ] def get_urls(self): patterns = [ url(r'list/(?P<class_id>\d+)/$', self.wrapper(self.changelist), name=self.get_list_url_name), url(r'add/(?P<class_id>\d+)/$', self.wrapper(self.add_view), name=self.get_add_url_name), url(r'change/(?P<class_id>\d+)/(?P<pk>\d+)/$', self.wrapper(self.change_view), name=self.get_change_url_name), url(r'del/(?P<class_id>\d+)/(?P<pk>\d+)/$', self.wrapper(self.delete_view), name=self.get_delete_url_name), url(r'attendance/(?P<course_record_id>\d+)/$', self.wrapper(self.attendance_view), name=self.get_url_name('attendance')), url(r'homeworkscore/(?P<course_record_id>\d+)/$', self.wrapper(self.homework_view), name=self.get_url_name('homeworkscore')), ] patterns.extend(self.extra_urls( )) # 这个self并不是这个基类的self 而是最开始从传入的那个handler开始寻找有没有extra_urls return patterns def get_queryset(self, request, *args, **kwargs): class_id = kwargs.get('class_id') return self.model_class.objects.filter(class_object_id=class_id) def save(self, request, form, is_update, *args, **kwargs): class_id = kwargs.get('class_id') if not is_update: form.instance.class_object_id = class_id form.save() def multi_init(self, request, *args, **kwargs): course_record_id_list = request.POST.getlist('pk') class_id = kwargs.get('class_id') class_object = models.Classlist.objects.filter(id=class_id).first() if not class_object: return HttpResponse('班级不存在') student_object_all = class_object.student_set.all() for course_record_id in course_record_id_list: course_record_object = models.CourseRecord.objects.filter( id=course_record_id, class_object_id=class_id).first() if not course_record_object: continue # 判断此上课记录的考勤记录是否已经存在 study_record_exists = models.StudyRecord.objects.filter( course_record=course_record_object).exists() if study_record_exists: continue # 为每个学生在该天创造考勤记录 study_record_object_list = [ models.StudyRecord(student_id=stu.id, course_record_id=course_record_id) for stu in student_object_all ] models.StudyRecord.objects.bulk_create(study_record_object_list, batch_size=50) def multi_init_homework(self, request, *args, **kwargs): course_record_id_list = request.POST.getlist('pk') class_id = kwargs.get('class_id') class_object = models.Classlist.objects.filter(id=class_id).first() if not class_object: return HttpResponse('班级不存在') student_object_all = class_object.student_set.all() for course_record_id in course_record_id_list: course_record_object = models.CourseRecord.objects.filter( id=course_record_id, class_object_id=class_id).first() if not course_record_object: continue # 判断此上课记录的作业记录是否已经存在 study_record_exists = models.HomeWorkStudentRecord.objects.filter( course_record=course_record_object).exists() if study_record_exists: continue # 为每个学生在该天创造作业记录 study_record_object_list = [ models.HomeWorkStudentRecord(student_id=stu.id, course_record_id=course_record_id) for stu in student_object_all ] models.HomeWorkStudentRecord.objects.bulk_create( study_record_object_list, batch_size=50) def attendance_view(self, request, course_record_id, *args, **kwargs): ''' 考勤的批量操作 :param request: :param course_record_id: :param args: :param kwargs: :return: ''' study_record_object_list = models.StudyRecord.objects.filter( course_record_id=course_record_id) study_model_formset = modelformset_factory(models.StudyRecord, New_StudyRecord_forms, extra=0) if request.method == 'POST': formset = study_model_formset(queryset=study_record_object_list, data=request.POST) if formset.is_valid(): formset.save() return render(request, 'attendance.html', {'formset': formset}) formset = study_model_formset(queryset=study_record_object_list) return render(request, 'attendance.html', {'formset': formset}) def homework_view(self, request, course_record_id, *args, **kwargs): ''' 作业的批量操作 :param request: :param course_record_id: :param args: :param kwargs: :return: ''' homework_score_object_list = models.HomeWorkStudentRecord.objects.filter( course_record_id=course_record_id) homework_score_formset = modelformset_factory( models.HomeWorkStudentRecord, New_HomeWorkScore_forms, extra=0) if request.method == 'POST': formset = homework_score_formset( queryset=homework_score_object_list, data=request.POST) if formset.is_valid(): formset.save() return render(request, 'HomeworkScore.html', {'formset': formset}) formset = homework_score_formset(queryset=homework_score_object_list) return render(request, 'HomeworkScore.html', {'formset': formset}) multi_init.text = '批量初始化考勤' multi_init_homework.text = '批量初始化作业' action_list = [ multi_init, multi_init_homework, ]
class UserInfoHandler(StarkHandler): # search_list = ['nickname__contains','name__contains'] # search_group = [ # SearchOption(field='gender'), # SearchOption(field='depart') # ] def display_reset(self, request, obj=None, is_header=None): if is_header: return '重置密码' reset_url = self.reverse_reset_pwd_url(pk=obj.pk) return mark_safe('<a href="%s">重置密码</a>' % reset_url) list_display = [ 'nickname', StarkHandler.get_choice_text('性别', 'gender'), 'phone', 'email', 'depart', display_reset, StarkHandler.display_edit_del ] def Basic_Forms(self, model_class, add_or_change, request, pk, *args, **kwargs): if add_or_change == 'add': return New_add_forms(self, model_class) elif add_or_change == 'change': return New_change_forms(self, model_class) def reset_password(self, request, pk): ''' 重置密码页面 :param request: :param pk: :return: ''' User_obj = models.UserInfo.objects.filter(id=pk).first() if not User_obj: return HttpResponse('用户不存在') form = ResetPasswordForm() if request.method == 'GET': return render(request, 'stark/change.html', {'form': form}) form = ResetPasswordForm(data=request.POST) if form.is_valid(): # 密码更新到数据库 User_obj.password = form.cleaned_data['password'] User_obj.save() return redirect(self.reverse_list_url()) return render(request, 'stark/change.html', {'form': form}) @property def get_reset_pwd_url_name(self): return self.get_url_name('reset_pwd') def reverse_reset_pwd_url(self, *args, **kwargs): ''' 生成带有原搜索条件的重置密码URL :param args: :param kwargs: :return: ''' return self.reverse_commons_url(self.get_reset_pwd_url_name, *args, **kwargs) def extra_urls(self): patterns = [ url(r'^reset/password/(?P<pk>\d+)/$', self.wrapper(self.reset_password), name=self.get_reset_pwd_url_name) ] return patterns
class CheckPaymentRecordHandler(StarkHandler): order_list = ['id', 'confirm_status'] has_add_btn = None list_display = [ StarkHandler.display_checkbox, 'customer', StarkHandler.get_choice_text('缴费类型', 'pay_type'), 'paid_fee', StarkHandler.get_datetime_text('申请时间', 'apply_date'), 'class_list', StarkHandler.get_choice_text('确认状态', 'confirm_status'), 'consultant' ] def get_urls(self): patterns = [ url(r'list/$', self.wrapper(self.changelist), name=self.get_list_url_name), # url(r'add/$', self.wrapper(self.add_view), name=self.get_add_url_name), # url(r'change/(?P<pk>\d+)/$', self.wrapper(self.change_view), name=self.get_change_url_name), # url(r'del/(?P<pk>\d+)/$', self.wrapper(self.delete_view), name=self.get_delete_url_name) ] patterns.extend(self.extra_urls( )) # 这个self并不是这个基类的self 而是最开始从传入的那个handler开始寻找有没有extra_urls return patterns def multi_confirm(self, request, *args, **kwargs): ''' 批量确认 :param request: :param args: :param kwargs: :return: ''' pk_list = request.POST.getlist('pk') for pk in pk_list: payment_object = self.model_class.objects.filter( id=pk, confirm_status=1).first() if not payment_object: continue payment_object.confirm_status = 2 payment_object.save() payment_object.customer.status = 1 payment_object.customer.save() models.Student.objects.filter( customer_id=payment_object.customer.pk).update( student_status=2) return redirect(self.reverse_list_url()) multi_confirm.text = '批量确认' def multi_cancel(self, request, *args, **kwargs): ''' 批量驳回 :param request: :param args: :param kwargs: :return: ''' pk_list = request.POST.getlist('pk') judge_object = self.model_class.objects.filter( id__in=pk_list, confirm_status=1).update(confirm_status=3) print(judge_object) if not judge_object: return HttpResponse('抱歉 你的之前被确认 已经无法再驳回了') return redirect(self.reverse_list_url()) multi_cancel.text = '批量驳回' action_list = [multi_confirm, multi_cancel]