Django项目:CRM(客户关系管理系统)--59--49PerfectCRM实现CRM客户报名流程学生合同表单验证
1 # sales_views.py 2 # ————————47PerfectCRM实现CRM客户报名流程———————— 3 from django.db import IntegrityError #主动捕捉错误信息 4 from django.shortcuts import render #页面返回 5 from crm import models #数据库 6 from bpm.bpm_auxiliary import bpm_forms #自定制 forms 7 from django.contrib.auth.decorators import login_required# 登陆后页面才能访问 8 9 # ————————47PerfectCRM实现CRM客户报名流程———————— 10 from django.core.mail import send_mail 11 # send_mail的参数分别是 邮件标题,邮件内容,发件箱(settings.py中设置过的那个),收件箱列表(可以发送给多个人),失败静默(若发送失败,报错提示我们) 12 import random 13 import datetime # 获取时间#登陆过期 14 #发送邮件的功能 #验证码#密码 15 class stmp() : 16 def __init__(self): 17 self.emaillist=[] #发送给谁 18 self.code=None #验证码#密码 19 def stmps(self,request,email,msg_mail): #传参数#页面,session #邮箱,发送给谁 #内容 20 self.emaillist.append(email) #将邮箱地址添加到调用Django发送邮件功能 21 # ——————生成验证码—————— 22 _letter_cases = "abcdefghjkmnpqrstuvwxy" # 小写字母,去除可能干扰的i,l,o,z 23 _upper_cases = _letter_cases.upper() # 大写字母 24 _numbers = ''.join(map(str, range(3, 10))) # 数字 25 chars = ''.join((_letter_cases, _upper_cases, _numbers)) # 变成一条字符串 26 list = random.sample(chars, 4) # 从一条字符串随机选4个字符变成列表 27 self.code = ''.join(list) # 列表变字符串 28 # ——————生成验证码—————— 29 # ——————调用Django发送邮件—————— 30 title= 'PerfectCRM项目自动邮件:%s'%self.code # 邮件标题#防止一样的内容被邮箱屏蔽 31 send_mail(title, # 邮件标题 32 msg_mail, # 验证码内容 33 'perfectcrm@sina.cn', # 发送的邮箱 #根据情况重新配置 34 self.emaillist, # 接受的邮箱 35 fail_silently=False, # 静默,抛出异常 36 ) 37 print('发送邮件成功!没收到要换标题!检查发送邮箱的配置!') 38 # ——————调用Django发送邮件—————— 39 40 # ————————47PerfectCRM实现CRM客户报名流程———————— 41 # ————————47PerfectCRM实现CRM客户报名流程———————— 42 43 #报名填写 销售 44 @login_required # 登陆后页面才能访问 45 def enrollment(request,customer_id): 46 msgs={} #错误信息 47 customer_obj=models.Customer.objects.get(id=customer_id)#取到客户信息记录 #返回到页面#报名人 48 consultant_obj=models.UserProfile.objects.get(id=request.user.id)#报名课程顾问 49 50 stmp_mail = {} #邮件发送成功 51 stmpemail = stmp() #实例化发送邮件的功能 52 email = request.POST.get('email') # 让页面POST提交的值,在页面GET后仍然存在显示 53 if request.method=="POST": 54 enroll_form= bpm_forms.EnrollmentForm(request.POST)#获取数据 55 if enroll_form.is_valid():#表单验证 56 msg = "http://127.0.0.1:8000/bpm/customer/registration/{enroll_obj_id}/" 57 try: 58 enroll_form.cleaned_data['customer']=customer_obj#添加学员对象 记录 #报名人 59 enroll_form.cleaned_data['consultant'] = consultant_obj#报名课程顾问 60 enroll_obj=models.Enrollment.objects.create(**enroll_form.cleaned_data)#创建记录 61 msgs['msg']=msg.format(enroll_obj_id=enroll_obj.id)#报名记录对应的id 62 except IntegrityError as e: 63 #取到这条记录 64 enroll_obj=models.Enrollment.objects.get(customer_id=customer_obj.id, 65 enrolled_class_id=enroll_form.cleaned_data['enrolled_class'].id) 66 enroll_form.add_error('__all__','记录已经存在,不能重复创建!') 67 msgs['msg']=msg.format(enroll_obj_id=enroll_obj.id)#报名记录对应的id 68 if email: 69 msg_mail = "http://127.0.0.1:8000/bpm/customer/registration/%s/"%enroll_obj.id 70 stmpemail.stmps(request, email,msg_mail) # 发送邮件 71 stmp_mail['ok'] = "邮件已发送成功!" 72 73 else: 74 enroll_form= bpm_forms.EnrollmentForm()#modelform表单 75 return render(request, 'bpm_sales/enrollment.html', locals()) 76 # ————————47PerfectCRM实现CRM客户报名流程———————— 77 78 79 # ————————48PerfectCRM实现CRM客户报名流程学生合同———————— 80 #学员合同签定 81 def stu_registration(request,enroll_id): 82 enroll_obj=models.Enrollment.objects.get(id=enroll_id)#获取报名记录 83 # ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证———————— 84 if request.method == "POST": 85 customer_form = bpm_forms.CustomerForm(request.POST, instance=enroll_obj.customer) # 生成表单验证 86 if customer_form.is_valid(): # 表单验证通过 87 customer_form.save() # 保存 88 enroll_obj.contract_agreed = True # 同意协议 89 enroll_obj.save() # 保存 90 status = 1 # 修改报名状态 # 1 已经报名 91 return render(request, 'bpm_sales/stu_registration.html', locals()) 92 93 else: 94 if enroll_obj.contract_agreed == True: # 如果协议已经签订 95 status = 1 # 修改报名状态 # 1 已经报名 96 else: 97 status = 0 98 customer_form = bpm_forms.CustomerForm(instance=enroll_obj.customer) # 生成表单 99 # customer_form = bpm_forms.CustomerForm(instance=enroll_obj.customer) # 生成表单 100 # ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证———————— 101 102 return render(request,'bpm_sales/stu_registration.html',locals()) 103 # ————————48PerfectCRM实现CRM客户报名流程学生合同————————
1 {#stu_registration.html#} 2 {## ————————48PerfectCRM实现CRM客户报名流程学生合同————————#} 3 {% extends 'bpm_master/bpm_sample.html' %} 4 {% load bpm_tags %} 5 {% block right-container-content %} {#自定义内容开始 右边页面内容#} 6 <div class="container col-lg-7 col-md-offset-2"> 7 <div class="panel panel-warning"> 8 <div class=" panel-heading"> 9 <h3 class="panel-title container">报名入学|信息填写</h3> 10 </div> 11 <div class="panel-body "><!--返回提交函数--> 12 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 13 {% if status != 1 %} 14 <span class="errors">{{ customer_form.errors }}</span><!--错误提示--> 15 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 16 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 17 {# <form method="post" class="form-horizontal" role="form">{% csrf_token %}#} 18 <form method="post" class="form-horizontal" role="form" 19 onsubmit="return RegisterFormCheck()">{% csrf_token %} 20 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 21 {% for foo in customer_form %} 22 <div class="form-group"> 23 <label for="inputEmail3" class="col-sm-2 control-label">{{ foo.label }}</label> 24 <div class="col-sm-8"> 25 {{ foo }} 26 </div> 27 </div> 28 {% endfor %} 29 <hr> 30 {#返回06学员报名信息表的数据#} 31 <div class="form-group"> 32 <label for="inputEmail3" class="col-sm-2 control-label">所报班级</label> 33 <div class="col-sm-8"> 34 {{ enroll_obj.enrolled_class }} 35 </div> 36 </div> 37 <div class="form-group"> 38 <label for="inputEmail3" class="col-sm-2 control-label">课程费用</label> 39 <div class="col-sm-8"> 40 {{ enroll_obj.enrolled_class.course.price }} 41 </div> 42 </div> 43 <div class="form-group"> 44 <label for="inputEmail3" class="col-sm-2 control-label">开课日期</label> 45 <div class="col-sm-8"> 46 {{ enroll_obj.enrolled_class.start_date }} 47 </div> 48 </div> 49 <div class="form-group"> 50 <label for="inputEmail3" class="col-sm-2 control-label">合同</label> 51 <div class="col-sm-10"> 52 <div style="width: 550px"> 53 <pre style="height: 300px">{% render_enrolled_contract enroll_obj %} </pre> 54 </div> 55 56 </div> 57 </div> 58 <div class="form-group"> 59 <div class="col-sm-12"> 60 <input type="checkbox" value="{{ enroll_obj.contract_agreed }}" name="contract_agreed" 61 checked> 62 我已经认真阅读完协议并接受所有条款 63 </div> 64 </div> 65 <div class="text-center"> 66 <input type="submit" class="btn btn-info" value="提交"> 67 </div> 68 </form> 69 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 70 {% else %} 71 <h3>报名成功,同合进入审核流程,感谢您的选择!</h3> 72 {% endif %} 73 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 74 </div> 75 <div class="panel-footer"> 76 <input type="button" class="btn btn-danger right" value="关闭" onclick="CloseWebPage()"> 77 </div> 78 </div> 79 </div> 80 {% endblock %} 81 82 {% block js %} 83 <script> 84 function CloseWebPage() { 85 if (confirm("您确定要关闭本页吗?")) { 86 window.opener = null; 87 window.open('', '_self'); 88 window.close(); 89 } 90 else { 91 } 92 } 93 94 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 95 96 function RegisterFormCheck() { 97 if ($('form :checkbox').prop("checked")) { 98 $("form").find("[disabled]").removeAttr("disabled");//移除不可修改 便于提交表单 #qq 99 return true; 100 } else { 101 alert('请认真阅读并且同意条款,才可以报名'); 102 return false; 103 } 104 } 105 106 {## ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————#} 107 108 109 </script> 110 111 112 113 {% endblock %} 114 {## ————————48PerfectCRM实现CRM客户报名流程学生合同————————#}
1 # forms.py 2 # ————————47PerfectCRM实现CRM客户报名流程———————— 3 from crm import models #数据库 4 from django.forms import ModelForm #继承forms自定制 5 #报名 销售填写 6 class EnrollmentForm(ModelForm): 7 def __new__(cls, *args, **kwargs): 8 for field_name,field_obj in cls.base_fields.items(): 9 field_obj.widget.attrs['class'] = 'form-control'## 前端的样式 10 return ModelForm.__new__(cls) 11 class Meta: 12 model= models.Enrollment 13 fields= ['enrolled_class'] 14 # ————————47PerfectCRM实现CRM客户报名流程———————— 15 16 17 # ————————48PerfectCRM实现CRM客户报名流程学生合同———————— 18 #报名学员填 写 19 class CustomerForm(ModelForm): 20 def __new__(cls, *args, **kwargs): 21 for field_name,field_obj in cls.base_fields.items(): 22 field_obj.widget.attrs['class'] = 'form-control'## 前端的样式 23 if field_name in cls.Meta.readonly_fields:#如果不可修改 24 field_obj.widget.attrs['disabled'] = True## 前端的样式 灰色 25 return ModelForm.__new__(cls) 26 27 # ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证———————— 28 def clean_qq(self): 29 #print(self.instance.qq,self.cleaned_data['qq'],'9696969696') 30 if self.instance.qq != self.cleaned_data['qq']: #输入的值和 cleaned_data的值 31 self.add_error('qq',"大神,请勿非法修改!") 32 return self.cleaned_data['qq'] 33 34 def clean_consultant(self): 35 if self.instance.consultant != self.cleaned_data['consultant']: 36 self.add_error('consultant',"非法修改!不要尝试!") 37 return self.cleaned_data['consultant'] 38 39 def clean_source(self): 40 if self.instance.source != self.cleaned_data['source']: 41 self.add_error('source',"非法修改!不能通过!") 42 return self.cleaned_data['source'] 43 # ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证———————— 44 45 class Meta: 46 model=models.Customer#客户表 47 fields='__all__' 48 exclude=['tags','content','memo','status','referral_from','consult_courses']#排除,不显示 49 readonly_fields=['qq','consultant','source']#不可修改 50 # ————————48PerfectCRM实现CRM客户报名流程学生合同————————
http://127.0.0.1:8000/bpm/customer/registration/1/
您的资助是我最大的动力!
金额随意,欢迎来赏!
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的
因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【颜言】!