10.22crm

2018-10-23 08:28:41

由于昨晚上断网,所以今天早上补更一下

越努力,越幸运!永远不要高估自己!

 

crm老师就讲三四天,还有明后两天!

主要是讲一些基本使用,自定义配置陪一些东西!

主要还是配置类

crm/stark.py

from stark.service.stark import site, ModelStark
from django.utils.safestring import mark_safe
from django.conf.urls import url
from django.shortcuts import redirect, render
from .models import *

"""
可以配置字段:
        1. list_display[]   可以添加自定义展示字段,也可以添加函数名
        2. 父类提供的添加该表额外url的接口 def extra_url  重写这个方法即可
2018-10-22 21:33:33
增加部分:
        1. 初始化  course_record,studyrecord,
        2. 考勤
        3. 录入成绩
"""


# 用户配置表
class UserConfig(ModelStark):
    # 自定义展示字段 list_display[]
    list_display = ["name", "email", "depart"]


# 班级配置表
class ClassConfig(ModelStark):
    # 自定义一个展示函数,然后添加到list_display中
    def display_classname(self, obj=None, header=False):
        if header:
            return "班级名称"
        class_name = "%s(%s)" % (obj.course.name, str(obj.semester))
        return class_name

    list_display = [display_classname, "tutor", "teachers"]


# 客户配置表
class CusotmerConfig(ModelStark):
    # 自定义展示性别和课程
    def display_gender(self, obj=None, header=False):
        if header:
            return "性别"
        return obj.get_gender_display()

    def display_course(self, obj=None, header=False):
        if header:
            return "咨询课程"
        temp = []
        for course in obj.course.all():
            s = "<a href='/stark/crm/customer/cancel_course/%s/%s' style='border:1px solid #369;padding:3px 6px'><span>%s</span></a>&nbsp;" % (
                obj.pk, course.pk, course.name,)
            temp.append(s)
        return mark_safe("".join(temp))

    list_display = ["name", display_gender, display_course, "consultant", ]

    def cancel_course(self, request, customer_id, course_id):
        print(customer_id, course_id)
        obj = Customer.objects.filter(pk=customer_id).first()
        obj.course.remove(course_id)
        return redirect(self.get_list_url())

    # 父类给出的添加额外url的接口
    def extra_url(self):
        temp = []
        temp.append(url(r"cancel_course/(\d+)/(\d+)", self.cancel_course))
        return temp


# 咨询配置表
class ConsultConfig(ModelStark):
    # 自定义展示字段
    list_display = ["customer", "consultant", "date", "note"]


# 学生配置表
class StudentConfig(ModelStark):
    # 自定义展示字段
    list_display = ["customer", "class_list"]
    list_display_links = ["customer"]


# 学习情况配置表
class StudyConfig(ModelStark):
    # 自定义展示字段
    list_display = ["student", "course_record", "record", "score"]

    def patch_late(self, request, queryset):
        queryset.update(record="late")

    # 批量更改为 迟到
    patch_late.short_description = "一键迟到"
    actions = [patch_late]


# 课程信息配置表
class CourseRecordConfig(ModelStark):
    # 记录成绩的视图函数
    def score(self, request, course_record_id):
        if request.method == "POST":
            print(request.POST)
            data = {}
            for key, value in request.POST.items():
                if key == "csrfmiddlewaretoken": continue
                print("key:", key)           # key: score_1
                # 取到键值pk 后面数字
                field, pk = key.rsplit("_", 1)
                if pk in data:
                    data[pk][field] = value
                else:
                    data[pk] = {field: value}  # data  {4:{"score":90}}
            print("data", data)
            # 构建成如下的数据 虽然构建数据有些麻烦,但是节省了储存数据库所需时间
            # data {'1': {'score': '100', 'homework_note': '11'}, '2': {'score': '85', 'homework_note': '22'}}
            for pk, update_data in data.items():
                StudyRecord.objects.filter(pk=pk).update(**update_data)
            return redirect(request.path)
        else:
            # 把数据传入前端,然后渲染列表数据
            study_record_list = StudyRecord.objects.filter(course_record=course_record_id)
            score_choices = StudyRecord.score_choices
            return render(request, "score.html", locals())

    # 通过内置接口 分发一个记录成绩的url
    def extra_url(self):
        temp = []
        temp.append(url(r"record_score/(\d+)", self.score))
        return temp

    # 定义考勤的函数
    def record(self, obj=None, header=False):
        if header:
            return "考勤"
        return mark_safe("<a href='/stark/crm/studyrecord/?course_record=%s'>记录</a>" % obj.pk)

    # 定义录入成绩的函数
    def record_score(self, obj=None, header=False):
        if header:
            return "录入成绩"
        return mark_safe("<a href='record_score/%s'>录入成绩</a>" % obj.pk)

    list_display = ["class_obj", "day_num", "teacher", record, record_score]

    # 批量添加学生学习记录
    def patch_studyrecord(self, request, queryset):
        print(queryset)
        temp = []
        for course_record in queryset:
            # 与course_record关联的班级对应所有学生
            student_list = Student.objects.filter(class_list__id=course_record.class_obj.pk)
            for student in student_list:
                obj = StudyRecord(student=student, course_record=course_record)
                temp.append(obj)
        # 在StudyRecord表中批量添加学生学习记录
        StudyRecord.objects.bulk_create(temp)
    patch_studyrecord.short_description = "批量生成学习记录"
    actions = [patch_studyrecord, ]


site.register(UserInfo, UserConfig)
site.register(Customer, CusotmerConfig)
site.register(Student, StudentConfig)
site.register(ConsultRecord, ConsultConfig)
site.register(StudyRecord, StudyConfig)
site.register(CourseRecord, CourseRecordConfig)
site.register(ClassList, ClassConfig)
site.register(School)
site.register(Department)
site.register(Course)

test.py 知识点

# 加个r从右向左分, 取到r是从左向右分
print("yuan_alex_egon".rsplit("_",2))
print("yuan_alex_egon".split("_",2))

放上笔记!

day94:
   
   
   知识点1:

   limit_choice_to={}
   
   ModelForm
       model:
       teachers = models.ManyToManyField(verbose_name='任课老师', to='UserInfo',related_name="abc",limit_choices_to={"depart__in":[1002,1005]})
       form:
       data=UserInfo.objects.filter(depart__in=[1002,1005]).valueslist("pk","title")
       teachers = forms.ModelMultiChoiceField(choices=data)

    知识点2:
    if     

        class Customer(): 
            name=models.Charfield(max...)
            gender = models.IntegerField(verbose_name='性别', choices=[(1,""),(2,"")])
               
        obj=Customer.objects.create(name="alex",gender=1)    
            
        print(obj.gender)
        print(obj.get_gender_display())  # "男"
    
    知识点3:
    扩展URL:
          
    
                temp.extend(self.extra_url())
         
         
         
                def cancel_course(self,request,customer_id,course_id):
                    print(customer_id,course_id)

                    obj=Customer.objects.filter(pk=customer_id).first()
                    obj.course.remove(course_id)
                    return redirect(self.get_list_url())

                def extra_url(self):

                    temp=[]

                    temp.append(url(r"cancel_course/(\d+)/(\d+)",self.cancel_course))

                    return temp
         
         
crm:


    讲师与学生
        1 初始化  course_record,studyrecord,
        2 考勤
        3 录入成绩
        4 显示成绩 ajax 查询  
    
    
    
    
    销售与客户
    
        

 

posted @ 2018-10-23 08:34  我想喝杨枝甘露~  阅读(166)  评论(0编辑  收藏  举报