-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
214 lines (166 loc) · 5.86 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from django.shortcuts import render,redirect,reverse
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
from django.views import generic
from django.core.mail import send_mail
from django.contrib.auth.mixins import LoginRequiredMixin
from agents.mixins import OrganiserandLoginRequiredMixin
from .models import Lead,Agent
from .forms import Leadform,Leadmodelform,CustomUserCreationForm
#CRUD - create, retrieve, update and delete + list
#Class based views
class SignupView(generic.CreateView):
template_name='registration/signup.html'
form_class=CustomUserCreationForm
def get_success_url(self):
return reverse("login")
class Landingpageview(generic.TemplateView):
template_name="landing.html"
class LeadListView(LoginRequiredMixin,generic.ListView):
template_name="leads/leads_list.html"
context_object_name="leads"
def get_queryset(self):
user=self.request.user
if user.is_organiser:
queryset=Lead.objects.filter(organisation=user.userprofile)
else:
queryset=Lead.objects.filter(organisation=user.agent.organisation)
#filter for the agent that is logged in
queryset=queryset.filter(agent__user=user)
return queryset
class LeadDetailView(LoginRequiredMixin,generic.DetailView):
template_name="leads/lead_detail.html"
context_object_name="lead"
def get_queryset(self):
user=self.request.user
if user.is_organiser:
queryset=Lead.objects.filter(organisation=user.userprofile)
else:
queryset=Lead.objects.filter(organisation=user.agent.organisation)
#filter for the agent that is logged in
queryset=queryset.filter(agent__user=user)
return queryset
class LeadCreateView(OrganiserandLoginRequiredMixin,generic.CreateView):
template_name="leads/lead_create.html"
form_class=Leadmodelform
def get_success_url(self):
return reverse("leads:lead-list")
#to send email
def form_valid(self,form):
send_mail(subject="Lead has been created",
message="Go to site to see the new lead",from_email='[email protected]',
recipient_list=["[email protected]"]
)
return super(LeadCreateView,self).form_valid(form)
context_object_name="lead"
class LeadUpdateView(OrganiserandLoginRequiredMixin,generic.UpdateView):
template_name="leads/lead_update.html"
form_class=Leadmodelform
def get_queryset(self):
user=self.request.user
return Lead.objects.filter(organisation=user.userprofile)
def get_success_url(self):
return reverse('login')
class LeadDeleteView(OrganiserandLoginRequiredMixin,generic.DeleteView):
template_name="leads/lead_delete.html"
def get_queryset(self):
user=self.request.user
return Lead.objects.filter(organisation=user.userprofile)
def get_sucess_url(self):
return reverse("leads:lead-list")
# Create your views here.
def landing_page(request):
return render(request,'landing.html')
def lead_list(request):
leads=Lead.objects.all()
context={
"leads":leads
}
return render(request,"leads/leads_list.html",context)
#return HttpResponse("Hello World")
def second_page(request):
return render(request,'second_page.html')
def lead_detail(request,pk):
lead=Lead.objects.get(id=pk)
print(lead)
context={
"lead":lead
}
return render(request,'leads/lead_detail.html',context)
# return HttpResponse('here is the detailed view')
def lead_create(request):
form=Leadmodelform()
if request.method=='POST':
form=Leadmodelform(request.POST)
if form.is_valid():
form.save()
return redirect('/leads')
context={
"form":form
}
return render(request,"leads/lead_create.html",context)
def lead_update(request,pk):
lead=Lead.objects.get(id=pk)
form=Leadmodelform(instance=lead)
if request.method=='POST':
form=Leadmodelform(request.POST,instance=lead)
if form.is_valid():
form.save()
return redirect('/leads')
context={
"form":form,
"lead":lead
}
return render(request, "leads/lead_update.html",context)
def lead_delete(request,pk):
lead=Lead.objects.get(id=pk)
lead.delete()
return redirect("/leads")
"""
def lead_update(request,pk):
lead=Lead.objects.get(id=pk)
form=Leadform()
if request.method=='POST':
#print("recieving post request")
form=Leadform(request.POST)
if form.is_valid():
#print("form is valid")
#print(form.cleaned_data)
first_name=form.cleaned_data['first_name']
last_name=form.cleaned_data['last_name']
age=form.cleaned_data['age']
agent=Agent.objects.first()
lead.first_name=first_name
lead.last_name=last_name
lead.age=age
lead.save()
return redirect('/leads')
context={
"form":form,
"lead":lead
}
return render(request, "leads/lead_update.html",context)
def lead_create(request):
form=Leadform()
if request.method=='POST':
#print("recieving post request")
form=Leadform(request.POST)
if form.is_valid():
#print("form is valid")
#print(form.cleaned_data)
first_name=form.cleaned_data['first_name']
last_name=form.cleaned_data['last_name']
age=form.cleaned_data['age']
agent=Agent.objects.first()
Lead.objects.create(
first_name=first_name,
last_name=last_name,
age=age,
agent=agent
)
return redirect('/leads')
context={
"form":form
}
return render(request,"leads/lead_create.html",context)
"""