class StudentHandler(PermissionHandler, StarkHandler): model_form_class = StudentModelForm has_add_btn = False def display_checkscore(self, obj=None, is_header=None): """为公共客户表新增一个查看客户跟进记录的功能""" if is_header: return "学分管理" # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 nickname = "%s:%s" % (self.site.namespace, "web_scorerecord_list" ) # 修改按钮的别名 url = reverse(nickname, kwargs={"student_id": obj.pk}) button = mark_safe("""<a target='_blank' href="{0}">{1}</a>""".format( url, obj.score)) return button list_display = [ "customer", "qq", "mobile", get_Many_to_Many_text("已报班级", "class_list"), display_checkscore, StarkHandler.get_choice_text("学员状态", "student_status"), ] search_list = ["customer__name", "qq", "mobile"] search_group = [ SearchOption("class_list", text_func=lambda x: "%s-%s" % (x.school.title, str(x))) ] def get_action_list(self): """ 获取自定义批量操作的钩子 :return: """ return [] def get_urls(self): patterns = [ url("^list/$", self.wapper(self.changelist_view), name=self.get_list_url_name), url("^edit/(?P<pk>\d+)/$", self.wapper(self.edit_view), name=self.get_edit_url_name), url("^del/(?P<pk>\d+)/$", self.wapper(self.delete_view), name=self.get_delete_url_name), ] patterns.extend(self.extra_urls()) return patterns
def display_operation(self, obj=None, is_header=None, *args, **kwargs): """ 这里可以写上业务逻辑来根据用户的权限高低判断需要是否需要展示页面中的修改删除按钮 :param obj:每一行的记录对象 :param is_header:用于判断是放在表头还是在表内 :return:mark_safe(字符串格式的标签) 或者其他内容 """ if is_header: # 如果这是是True,就表示这是对表头进行的编辑 return "操作" else: #这表示对行内进行编辑 # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 edit_name = "%s:%s" % (self.site.namespace, self.get_edit_url_name ) # 修改按钮的别名 delete_name = "%s:%s" % ( self.site.namespace, self.get_delete_url_name) # 删除按钮的别名 class_id = kwargs.get("class_id") edit_url = StarkHandler.memory_url( self.request, edit_name, **{ "class_id": class_id, "pk": obj.pk }, ) delete_url = StarkHandler.memory_url( self.request, delete_name, **{ "class_id": class_id, "pk": obj.pk }, ) # 这里放了编辑和修改两个按钮 button = mark_safe(""" <a style="color: #333333;" href="{0}"> <i class="fa fa-edit" aria-hidden="true"></i></a> | <a style="color: #d9534f;" href="{1}"><i class="fa fa-trash-o"></i></a> """.format(edit_url, delete_url)) return button
def display_checkrecord(self, obj=None, is_header=None, *args, **kwargs): """为公共客户表新增一个查看客户跟进记录的功能""" if is_header: return "查看跟进记录" # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 nickname = "%s:%s" % (self.site.namespace, self.get_checkrecord_url_name) # 修改按钮的别名 url = StarkHandler.memory_url(self.request, nickname, *[obj.pk]) # 这里放了编辑和修改两个按钮 button = mark_safe("""<a href="{0}">点击查看</a>""".format(url)) return button
def display_reset(self, obj=None, is_header=None): """为员工表多加一列,用于重置密码""" if is_header: return "重置密码" # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 reset_name = "%s:%s" % (self.site.namespace, self.get_reset_url_name ) # 修改按钮的别名 reset_url = StarkHandler.memory_url(self.request, reset_name, *[obj.pk]) # 这里放了编辑和修改两个按钮 button = mark_safe( """<a style="font-size: 16px;" href="{0}"><i class="fa fa-gears" aria-hidden="true"></i></a>""" .format(reset_url)) return button
def reset_view(self, request, pk, *args, **kwargs): """重置密码操作""" current_obj = self.model_class.objects.filter( id=pk).first() # 获取当前要重置密码的用户对象 if not current_obj: return render(request, "rbac/refuse.html") if request.method == "GET": form = ResetPWDModelForm( ) # 把这个角色对象传入form组件里面,页面点开后的input输入框内会自带上这个对象的各种值 return render(request, "stark/change.html", {"form": form}) form = ResetPWDModelForm(instance=current_obj, data=request.POST) if form.is_valid(): # 如果数据合法,就进行保存 self.save(form, is_update=False) name = "%s:%s" % (self.site.namespace, self.get_list_url_name) return redirect(StarkHandler.memory_reverse(self.request, name)) # 跳转会列表页面 return render(request, 'stark/change.html', {'form': form})
class StaffHandler(PermissionHandler, StarkHandler): """用于对Staff表设置各种功能""" def display_reset(self, obj=None, is_header=None): """为员工表多加一列,用于重置密码""" if is_header: return "重置密码" # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 reset_name = "%s:%s" % (self.site.namespace, self.get_reset_url_name ) # 修改按钮的别名 reset_url = StarkHandler.memory_url(self.request, reset_name, *[obj.pk]) # 这里放了编辑和修改两个按钮 button = mark_safe( """<a style="font-size: 16px;" href="{0}"><i class="fa fa-gears" aria-hidden="true"></i></a>""" .format(reset_url)) return button list_display = [ StarkHandler.display_checkbox, "name", StarkHandler.get_choice_text("性别", "gender"), "age", "phone", "email", "depart", display_reset, ] def get_model_form(self, add_or_edit, request, *args, **kwargs): if add_or_edit == "add": return StaffAddModelForm elif add_or_edit == "edit": return StaffEditModelForm @property def get_reset_url_name(self): """获取重置密码的路由别名""" return self.get_url_name("reset") def reset_view(self, request, pk, *args, **kwargs): """重置密码操作""" current_obj = self.model_class.objects.filter( id=pk).first() # 获取当前要重置密码的用户对象 if not current_obj: return render(request, "rbac/refuse.html") if request.method == "GET": form = ResetPWDModelForm( ) # 把这个角色对象传入form组件里面,页面点开后的input输入框内会自带上这个对象的各种值 return render(request, "stark/change.html", {"form": form}) form = ResetPWDModelForm(instance=current_obj, data=request.POST) if form.is_valid(): # 如果数据合法,就进行保存 self.save(form, is_update=False) name = "%s:%s" % (self.site.namespace, self.get_list_url_name) return redirect(StarkHandler.memory_reverse(self.request, name)) # 跳转会列表页面 return render(request, 'stark/change.html', {'form': form}) def extra_urls(self): """ 这里可以为当前表设定需要增加的url 注意:加URL的同时要在这里写好视图函数 def test_view(self, request):pass """ patterns = [ url("^reset/(?P<pk>\d+)/$", self.wapper(self.reset_view), name=self.get_reset_url_name), ] return patterns search_group = [ # 配置进行组合搜索的字段,默认为空 SearchOption("gender"), # 这是都用默认设置的展示(基本足够了),is_choices设置该字段是否支持多选 SearchOption("depart", is_multi=True), ] search_list = [ "name__contains", "username__contains", "phone__contains", ] # 为当前表配置搜索的字段,_contains表示或的意思
class PaymentRecordHandler(PermissionHandler, StarkHandler): def get_model_form(self, add_or_edit, request, *args, **kwargs): """判断一下,如果是新生就需要录入一些基本信息,如果是已经有资料的学生就不用了""" customer_id = kwargs.get("customer_id") student_exists = Student.objects.filter(id=customer_id).exists() if student_exists: return PayementRecordModelForm return NewStudentPayementRecordModelForm def display_operation(self, obj=None, is_header=None, *args, **kwargs): """ 这里可以写上业务逻辑来根据用户的权限高低判断需要是否需要展示页面中的修改删除按钮 :param obj:每一行的记录对象 :param is_header:用于判断是放在表头还是在表内 :return:mark_safe(字符串格式的标签) 或者其他内容 """ if is_header: # 如果这是是True,就表示这是对表头进行的编辑 return "操作" else: #这表示对行内进行编辑 # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 edit_name = "%s:%s" % (self.site.namespace, self.get_edit_url_name ) # 修改按钮的别名 delete_name = "%s:%s" % ( self.site.namespace, self.get_delete_url_name) # 删除按钮的别名 customer_id = kwargs.get("customer_id") edit_url = StarkHandler.memory_url( self.request, edit_name, **{ "customer_id": customer_id, "pk": obj.pk }, ) delete_url = StarkHandler.memory_url( self.request, delete_name, **{ "customer_id": customer_id, "pk": obj.pk }, ) # 这里放了编辑和修改两个按钮 button = mark_safe(""" <a style="color: #333333;" href="{0}"> <i class="fa fa-edit" aria-hidden="true"></i></a> | <a style="color: #d9534f;" href="{1}"><i class="fa fa-trash-o"></i></a> """.format(edit_url, delete_url)) return button list_display = [ StarkHandler.get_choice_text("费用类型", "pay_type"), "paid_fee", "class_list", StarkHandler.get_choice_text("确认状态", "confirm_status"), "confirm_user", "note", ] def get_urls(self): patterns = [ url(r"^list/(?P<customer_id>\d+)/$", self.wapper(self.changelist_view), name=self.get_list_url_name), url(r"^add/(?P<customer_id>\d+)/$", self.wapper(self.add_view), name=self.get_add_url_name), url(r"^edit/(?P<customer_id>\d+)/(?P<pk>\d+)/$", self.wapper(self.edit_view), name=self.get_edit_url_name), url(r"^del/(?P<customer_id>\d+)/(?P<pk>\d+)/$", self.wapper(self.delete_view), name=self.get_delete_url_name), ] patterns.extend(self.extra_urls()) return patterns def get_queryset(self, request, *args, **kwargs): customer_id = int(kwargs.get("customer_id")) query_set = self.model_class.objects.filter( customer=customer_id, customer__consultant_id=request.user.pk) return query_set def save(self, request, form, is_update, *args, **kwargs): customer_id = kwargs.get("customer_id") object_exists = Customer.objects.filter( id=customer_id, consultant_id=request.user.pk).exists() if not object_exists: return render(request, "rbac/refuse.html", {"error_msg": "违规操作:这个客户还不是你的私人客户,无法进行操作!"}) if not is_update: form.instance.consultant_id = request.user.pk form.instance.customer_id = customer_id form.save() customer_id = kwargs.get("customer_id") class_list = form.cleaned_data["class_list"] student_obj = Student.objects.filter(id=customer_id).first() if not student_obj: student_qq = form.cleaned_data.get("qq") student_mobile = form.cleaned_data.get("mobile") student_emergency_contract = form.cleaned_data.get( "emergency_contract") student_obj = Student.objects.create( customer_id=customer_id, qq=student_qq, mobile=student_mobile, emergency_contract=student_emergency_contract) student_obj.class_list.add(class_list.id) else: student_obj.class_list.add(class_list.id) def get_edit_object(self, request, pk, *args, **kwargs): """方便扩展修改功能的数据校验""" customer_id = kwargs.get("customer_id") return PaymentRecord.objects.filter( id=pk, customer_id=customer_id, customer__consultant_id=request.user.pk).first() def delete_object(self, request, pk, *args, **kwargs): customer_id = kwargs.get("customer_id") return PaymentRecord.objects.filter(id=pk, customer_id=customer_id)
class PrivateCustomerHandler(PermissionHandler, StarkHandler): model_form_class = PrivateCustomerModelForm def save(self, request, form, is_update, *args, **kwargs): if not is_update: form.instance.consultant_id = request.user.pk form.save() def display_checkrecord(self, obj=None, is_header=None, *args, **kwargs): """为公共客户表新增一个查看客户跟进记录的功能""" if is_header: return "跟进记录" # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 nickname = "%s:%s" % (self.site.namespace, "web_consultrecord_list" ) # 修改按钮的别名 url = reverse(nickname, kwargs={"customer_id": obj.pk}) # 这里放了编辑和修改两个按钮 button = mark_safe( """<a target='_blank' href="{0}">点击查看</a>""".format(url)) return button def display_checkpayment(self, obj=None, is_header=None, *args, **kwargs): """为公共客户表新增一个查看客户缴费记录的功能""" if is_header: return "缴费操作" # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 nickname = "%s:%s" % (self.site.namespace, "web_paymentrecord_list" ) # 修改按钮的别名 url = reverse(nickname, kwargs={"customer_id": obj.pk}) # 这里放了编辑和修改两个按钮 button = mark_safe( """<a target='_blank' href="{0}">点击查看</a>""".format(url)) return button list_display = [ StarkHandler.display_checkbox, "name", "qq", StarkHandler.get_choice_text("客户状态", "status"), get_Many_to_Many_text("咨询课程", "course"), get_datetime_text("咨询日期", "date"), get_datetime_text("最后跟进日期", "last_consult_date"), display_checkrecord, display_checkpayment, ] def get_queryset(self, request, *args, **kwargs): """获取当前销售员私户内的客户列表""" return self.model_class.objects.filter(consultant=request.user.pk) def multi_apply_to_public(self, request, *args, **kwargs): """批量移除到公户,""" pk_list = request.POST.getlist("pk") # 通过getlist把页面传来的所有被选中的数据ID都获取到 self.model_class.objects.filter( id__in=pk_list, status=2, consultant=request.user.pk).update(consultant=None) multi_apply_to_public.text = "批量移除到公共客户" action_list = [ multi_apply_to_public, ]
class StudyRecordHandler(PermissionHandler, StarkHandler): list_display = [ "course_record", "student", StarkHandler.get_choice_text("上课记录", "record"), ] def display_operation(self, obj=None, is_header=None, *args, **kwargs): """ 这里可以写上业务逻辑来根据用户的权限高低判断需要是否需要展示页面中的修改删除按钮 :param obj:每一行的记录对象 :param is_header:用于判断是放在表头还是在表内 :return:mark_safe(字符串格式的标签) 或者其他内容 """ if is_header: # 如果这是是True,就表示这是对表头进行的编辑 return "操作" else: # 这表示对行内进行编辑 # 这里进行别名拼接,用url的别名和路由分发的名称空间这两个值进行拼接 # 这里进行拼接的namespace来自于StarkSite中的namespace属性 edit_name = "%s:%s" % (self.site.namespace, self.get_edit_url_name ) # 修改按钮的别名 delete_name = "%s:%s" % ( self.site.namespace, self.get_delete_url_name) # 删除按钮的别名 course_record_id = kwargs.get("course_record_id") edit_url = StarkHandler.memory_url( self.request, edit_name, **{ "course_record_id": course_record_id, "pk": obj.pk }, ) delete_url = StarkHandler.memory_url( self.request, delete_name, **{ "course_record_id": course_record_id, "pk": obj.pk }, ) # 这里放了编辑和修改两个按钮 button = mark_safe(""" <a style="color: #333333;" href="{0}"> <i class="fa fa-edit" aria-hidden="true"></i></a> | <a style="color: #d9534f;" href="{1}"><i class="fa fa-trash-o"></i></a> """.format(edit_url, delete_url)) return button def save(self, request, form, is_update, *args, **kwargs): course_record_id = kwargs.get("course_record_id") class_object = CourseRecord.objects.filter(id=course_record_id).first() if not class_object: return render(request, "rbac/refuse.html", {"error_msg": "违规操作:查找不到当前学员,请重新选择!"}) form.save() def get_queryset(self, request, *args, **kwargs): course_record_id = int(kwargs.get("course_record_id")) query_set = self.model_class.objects.filter( course_record=course_record_id) return query_set def get_edit_object(self, request, pk, *args, **kwargs): """方便扩展修改功能的数据校验""" course_record_id = kwargs.get("course_record_id") return self.model_class.objects.filter( id=pk, course_record=course_record_id).first() def delete_object(self, request, pk, *args, **kwargs): course_record_id = kwargs.get("course_record_id") return self.model_class.objects.filter(id=pk, course_record=course_record_id) def get_urls(self): patterns = [ url("^list/(?P<course_record_id>\d+)/$", self.wapper(self.changelist_view), name=self.get_list_url_name), url("^add/(?P<course_record_id>\d+)/$", self.wapper(self.add_view), name=self.get_add_url_name), url("^edit/(?P<course_record_id>\d+)/(?P<pk>\d+)/$", self.wapper(self.edit_view), name=self.get_edit_url_name), url("^del/(?P<course_record_id>\d+)/(?P<pk>\d+)/$", self.wapper(self.delete_view), name=self.get_delete_url_name), ] patterns.extend(self.extra_urls()) return patterns
class CheckPaymentRecordHandler(PermissionHandler, StarkHandler): order_list = ["confirm_status", "-id"] has_add_btn = False def multi_confirm(self, request, *args, **kwargs): """批量审核确认功能""" confirm_list = request.POST.getlist("pk") for pk in confirm_list: payment_obj = self.model_class.objects.filter( id=pk, confirm_status=1).first() # 拿到缴费记录 if not payment_obj: continue payment_obj.confirm_status = 2 # 缴费状态变成已确认 payment_obj.save() payment_obj.customer.status = 1 # 变更客户表中的状态 payment_obj.customer.save() customer_id = payment_obj.customer.id student_obj = Student.objects.filter( customer_id=customer_id).first() student_obj.student_status = 2 # 变更学员表中的状态 student_obj.save() multi_confirm.text = "批量审核确认" def multi_reject(self, request, *args, **kwargs): """批量审核驳回""" reject_list = request.POST.getlist("pk") self.model_class.objects.filter( id__in=reject_list, confirm_status=1).update(confirm_status=3) multi_reject.text = "批量审核驳回" list_display = [ StarkHandler.display_checkbox, "customer", "consultant", StarkHandler.get_choice_text("费用类型", "pay_type"), "paid_fee", "class_list", get_datetime_text("申请日期", "apply_date"), StarkHandler.get_choice_text("确认状态", "confirm_status"), "note", ] def get_list_display(self, request, *args, **kwargs): """ 开发者可以根据用户的不同在'Model'Handler类中通过复写(get_list_display)方法来控制页面上应该显示的列 这个跟'Model'Handler类复写list_display不影响,因为get_list_display方法是根据用户不同来控制的,里面还有控制的逻辑 :return: """ value = [] if self.list_display: value.extend(self.list_display) return value def get_action_list(self): """ 获取自定义批量操作的钩子 :return: """ return [ self.multi_confirm, self.multi_reject, ] def get_urls(self): patterns = [ url("^list/$", self.wapper(self.changelist_view), name=self.get_list_url_name), ] patterns.extend(self.extra_urls()) return patterns