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
16 # 发送邮件的功能 #验证码#密码
17 class stmp():
18 def __init__(self):
19 self.emaillist = [] # 发送给谁
20 self.code = None # 验证码#密码
21
22 def stmps(self, request, email, msg_mail): # 传参数#页面,session #邮箱,发送给谁 #内容
23 self.emaillist.append(email) # 将邮箱地址添加到调用Django发送邮件功能
24 # ——————生成验证码——————
25 _letter_cases = "abcdefghjkmnpqrstuvwxy" # 小写字母,去除可能干扰的i,l,o,z
26 _upper_cases = _letter_cases.upper() # 大写字母
27 _numbers = ''.join(map(str, range(3, 10))) # 数字
28 chars = ''.join((_letter_cases, _upper_cases, _numbers)) # 变成一条字符串
29 list = random.sample(chars, 4) # 从一条字符串随机选4个字符变成列表
30 self.code = ''.join(list) # 列表变字符串
31 # ——————生成验证码——————
32 # ——————调用Django发送邮件——————
33 title = 'PerfectCRM项目自动邮件:%s' % self.code # 邮件标题#防止一样的内容被邮箱屏蔽
34 send_mail(title, # 邮件标题
35 msg_mail, # 验证码内容
36 'perfectcrm@sina.cn', # 发送的邮箱 #根据情况重新配置
37 self.emaillist, # 接受的邮箱
38 fail_silently=False, # 静默,抛出异常
39 )
40 print('发送邮件成功!没收到要换标题!检查发送邮箱的配置!')
41 # ——————调用Django发送邮件——————
42
43
44 # ————————47PerfectCRM实现CRM客户报名流程————————
45 # ————————47PerfectCRM实现CRM客户报名流程————————
46 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
47 import random # 随机
48 import string # 字母 数字
49 from django.core.cache import cache # 缓存
50
51
52 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
53 # 报名填写 销售
54 @login_required # 登陆后页面才能访问
55 def enrollment(request, customer_id):
56 msgs = {} # 错误信息
57 customer_obj = models.Customer.objects.get(id=customer_id) # 取到客户信息记录 #返回到页面#报名人
58 consultant_obj = models.UserProfile.objects.get(id=request.user.id) # 报名课程顾问
59
60 stmp_mail = {} # 邮件发送成功
61 stmpemail = stmp() # 实例化发送邮件的功能
62 email = request.POST.get('email') # 让页面POST提交的值,在页面GET后仍然存在显示
63 if request.method == "POST":
64 enroll_form = bpm_forms.EnrollmentForm(request.POST) # 获取数据
65 if enroll_form.is_valid(): # 表单验证
66
67 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
68 # msg = "http://127.0.0.1:8000/bpm/customer/registration/{enroll_obj_id}/"
69 msg = "http://127.0.0.1:8000/bpm/customer/registration/{enroll_obj_id}/{random_str}/ "
70 random_str = ''.join(random.sample(string.ascii_lowercase + string.digits, 8)) # 生成8位随机字符串 #URL使用
71 url_str = '''customer/registration/{enroll_obj_id}/{random_str}/''' # 报名链接
72 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
73
74 try:
75 enroll_form.cleaned_data['customer'] = customer_obj # 添加学员对象 记录 #报名人
76 enroll_form.cleaned_data['consultant'] = consultant_obj # 报名课程顾问
77 enroll_obj = models.Enrollment.objects.create(**enroll_form.cleaned_data) # 创建记录
78
79 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
80 # msgs['msg']=msg.format(enroll_obj_id=enroll_obj.id)#报名记录对应的id,随机字符串,报名链接
81 sort_url = enroll_obj.id # 获取报名表对应的ID
82 cache.set(enroll_obj.id, random_str, 61000) # 加入过期时间 #URL使用 # cache缓存
83 msgs['msg'] = msg.format(enroll_obj_id=enroll_obj.id, random_str=random_str) # 报名记录对应的id,随机字符串,报名链接
84 url_str = url_str.format(enroll_obj_id=enroll_obj.id, random_str=random_str) # 报名链接
85 print(url_str)
86 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
87 except IntegrityError as e:
88 # 取到这条记录
89 enroll_obj = models.Enrollment.objects.get(customer_id=customer_obj.id,
90 enrolled_class_id=enroll_form.cleaned_data[
91 'enrolled_class'].id)
92 enroll_form.add_error('__all__', '记录已经存在,不能重复创建!')
93
94 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
95 # msgs['msg']=msg.format(enroll_obj_id=enroll_obj.id)#报名记录对应的id
96 cache.set(enroll_obj.id, random_str, 61000) # 加入过期时间 #URL使用 # cache缓存
97 msgs['msg'] = msg.format(enroll_obj_id=enroll_obj.id, random_str=random_str) # 报名记录对应的id
98 url_str = url_str.format(enroll_obj_id=enroll_obj.id, random_str=random_str) # 报名链接
99 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
100
101 if email:
102 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
103 # msg_mail = "http://127.0.0.1:8000/bpm/customer/registration/%s" %enroll_obj.id
104 msg_mail = "http://127.0.0.1:8000/bpm/customer/registration/%s/%s" %(enroll_obj.id,random_str)
105 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
106 stmpemail.stmps(request, email, msg_mail) # 发送邮件
107 stmp_mail['ok'] = "邮件已发送成功!"
108
109 else:
110 enroll_form = bpm_forms.EnrollmentForm() # modelform表单
111 return render(request, 'bpm_sales/enrollment.html', locals())
112
113
114 # ————————47PerfectCRM实现CRM客户报名流程————————
115
116 # ————————48PerfectCRM实现CRM客户报名流程学生合同————————
117 #学员合同签定
118 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
119 from django.shortcuts import HttpResponse #页面返回
120 # def stu_registration(request,enroll_id):
121 def stu_registration(request,enroll_id,random_str):
122 # enroll_obj=models.Enrollment.objects.get(id=enroll_id)#获取报名记录
123 if cache.get(enroll_id) == random_str: # 判断链接失效了没有
124 enroll_obj = models.Enrollment.objects.get(id=enroll_id) # 报名记录
125 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
126
127 # ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————
128 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
129 if request.method == "POST":
130 customer_form = bpm_forms.CustomerForm(request.POST, instance=enroll_obj.customer) # 生成表单验证
131 if customer_form.is_valid(): # 表单验证通过
132 customer_form.save() # 保存
133 enroll_obj.contract_agreed = True # 同意协议
134 enroll_obj.save() # 保存
135 status = 1 # 修改报名状态 # 1 已经报名
136 return render(request, 'bpm_sales/stu_registration.html', locals())
137
138 else:
139 if enroll_obj.contract_agreed == True: # 如果协议已经签订
140 status = 1 # 修改报名状态 # 1 已经报名
141 else:
142 status = 0
143 customer_form = bpm_forms.CustomerForm(instance=enroll_obj.customer) # 生成表单
144 # customer_form = bpm_forms.CustomerForm(instance=enroll_obj.customer) # 生成表单
145 # ————————49PerfectCRM实现CRM客户报名流程学生合同表单验证————————
146
147 return render(request,'bpm_sales/stu_registration.html',locals())
148 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
149 # ————————48PerfectCRM实现CRM客户报名流程学生合同————————
150 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————
151 else:
152 return HttpResponse('链接失效,非法链接,请自重!')
153 # ————————50PerfectCRM实现CRM客户报名流程学生合同URL随机码————————