forked from MicroPyramid/Django-CRM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
303 lines (258 loc) · 12 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.views.generic import CreateView, UpdateView, DetailView, TemplateView, View
from accounts.models import Account
from common.models import User, Comment, Team
from common.forms import BillingAddressForm
from common.utils import COUNTRIES
from contacts.models import Contact
from contacts.forms import ContactForm, ContactCommentForm
class ContactsListView(LoginRequiredMixin, TemplateView):
model = Contact
context_object_name = "contact_obj_list"
template_name = "contacts.html"
def get_queryset(self):
queryset = self.model.objects.all().select_related("account")
request_post = self.request.POST
if request_post:
if request_post.get('first_name'):
queryset = queryset.filter(first_name__icontains=request_post.get('first_name'))
if request_post.get('account'):
queryset = queryset.filter(account_id=request_post.get('account'))
if request_post.get('city'):
queryset = queryset.filter(address__city__icontains=request_post.get('city'))
if request_post.get('phone'):
queryset = queryset.filter(phone__icontains=request_post.get('phone'))
if request_post.get('email'):
queryset = queryset.filter(email__icontains=request_post.get('email'))
return queryset
def get_context_data(self, **kwargs):
context = super(ContactsListView, self).get_context_data(**kwargs)
context["contact_obj_list"] = self.get_queryset()
context["accounts"] = Account.objects.all()
context["per_page"] = self.request.POST.get('per_page')
return context
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
class CreateContactView(LoginRequiredMixin, CreateView):
model = Contact
form_class = ContactForm
template_name = "create_contact.html"
def dispatch(self, request, *args, **kwargs):
self.users = User.objects.filter(is_active=True).order_by('email')
self.accounts = Account.objects.all()
return super(CreateContactView, self).dispatch(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super(CreateContactView, self).get_form_kwargs()
kwargs.update({"assigned_to": self.users, "account": self.accounts})
return kwargs
def post(self, request, *args, **kwargs):
self.object = None
form = self.get_form()
address_form = BillingAddressForm(request.POST)
if form.is_valid() and address_form.is_valid():
return self.form_valid(form, address_form)
else:
return self.form_invalid(form, address_form)
def form_valid(self, form, address_form):
address_obj = address_form.save()
contact_obj = form.save(commit=False)
contact_obj.address = address_obj
contact_obj.created_by = self.request.user
contact_obj.save()
if self.request.POST.getlist('assigned_to', []):
contact_obj.assigned_to.add(*self.request.POST.getlist('assigned_to'))
if self.request.POST.getlist('teams', []):
contact_obj.teams.add(*self.request.POST.getlist('teams'))
if self.request.is_ajax():
return JsonResponse({'error': False})
if self.request.POST.get("savenewform"):
return redirect("contacts:add_contact")
else:
return redirect('contacts:list')
def form_invalid(self, form, address_form):
if self.request.is_ajax():
return JsonResponse({'error': True, 'contact_errors': form.errors,
'address_errors': address_form.errors})
return self.render_to_response(
self.get_context_data(form=form, address_form=address_form))
def get_context_data(self, **kwargs):
context = super(CreateContactView, self).get_context_data(**kwargs)
context["contact_form"] = context["form"]
context["teams"] = Team.objects.all()
context["accounts"] = self.accounts
context["users"] = self.users
context["countries"] = COUNTRIES
context["assignedto_list"] = [
int(i) for i in self.request.POST.getlist('assigned_to', []) if i]
context["teams_list"] = [
int(i) for i in self.request.POST.getlist('teams', []) if i]
if "address_form" in kwargs:
context["address_form"] = kwargs["address_form"]
else:
if self.request.POST:
context["address_form"] = BillingAddressForm(self.request.POST)
else:
context["address_form"] = BillingAddressForm()
return context
class ContactDetailView(LoginRequiredMixin, DetailView):
model = Contact
context_object_name = "contact_record"
template_name = "view_contact.html"
def get_queryset(self):
queryset = super(ContactDetailView, self).get_queryset()
return queryset.select_related("address")
def get_context_data(self, **kwargs):
context = super(ContactDetailView, self).get_context_data(**kwargs)
context.update({"comments": context["contact_record"].contact_comments.all()})
return context
class UpdateContactView(LoginRequiredMixin, UpdateView):
model = Contact
form_class = ContactForm
template_name = "create_contact.html"
def dispatch(self, request, *args, **kwargs):
self.users = User.objects.filter(is_active=True).order_by('email')
self.accounts = Account.objects.all()
return super(UpdateContactView, self).dispatch(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super(UpdateContactView, self).get_form_kwargs()
kwargs.update({"assigned_to": self.users, "account": self.accounts})
return kwargs
def post(self, request, *args, **kwargs):
self.object = self.get_object()
address_obj = self.object.address
form = self.get_form()
address_form = BillingAddressForm(request.POST, instance=address_obj)
if form.is_valid() and address_form.is_valid():
return self.form_valid(form, address_form)
else:
return self.form_invalid(form, address_form)
def form_valid(self, form, address_form):
addres_obj = address_form.save()
contact_obj = form.save(commit=False)
contact_obj.address = addres_obj
contact_obj.save()
contact_obj.assigned_to.clear()
contact_obj.teams.clear()
if self.request.POST.getlist('assigned_to', []):
contact_obj.assigned_to.add(*self.request.POST.getlist('assigned_to'))
if self.request.POST.getlist('teams', []):
contact_obj.teams.add(*self.request.POST.getlist('teams'))
if self.request.is_ajax():
return JsonResponse({'error': False})
return redirect("contacts:list")
def form_invalid(self, form, address_form):
if self.request.is_ajax():
return JsonResponse({'error': True, 'contact_errors': form.errors,
'address_errors': address_form.errors})
return self.render_to_response(
self.get_context_data(form=form, address_form=address_form))
def get_context_data(self, **kwargs):
context = super(UpdateContactView, self).get_context_data(**kwargs)
context["contact_obj"] = self.object
context["address_obj"] = self.object.address
context["contact_form"] = context["form"]
context["teams"] = Team.objects.all()
context["accounts"] = self.accounts
context["users"] = self.users
context["countries"] = COUNTRIES
context["assignedto_list"] = [
int(i) for i in self.request.POST.getlist('assigned_to', []) if i]
context["teams_list"] = [
int(i) for i in self.request.POST.getlist('teams', []) if i]
if "address_form" in kwargs:
context["address_form"] = kwargs["address_form"]
else:
if self.request.POST:
context["address_form"] = BillingAddressForm(
self.request.POST, instance=context["address_obj"])
else:
context["address_form"] = BillingAddressForm(
instance=context["address_obj"])
return context
class RemoveContactView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
contact_id = kwargs.get("pk")
self.object = get_object_or_404(Contact, id=contact_id)
if self.object.address_id:
self.object.address.delete()
self.object.delete()
if self.request.is_ajax():
return JsonResponse({'error': False})
else:
return redirect("contacts:list")
class AddCommentView(LoginRequiredMixin, CreateView):
model = Comment
form_class = ContactCommentForm
http_method_names = ["post"]
def post(self, request, *args, **kwargs):
self.object = None
self.contact = get_object_or_404(Contact, id=request.POST.get('contactid'))
if (
request.user in self.contact.assigned_to.all() or
request.user == self.contact.created_by
):
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
else:
data = {'error': "You don't have permission to comment."}
return JsonResponse(data)
def form_valid(self, form):
comment = form.save(commit=False)
comment.commented_by = self.request.user
comment.contact = self.contact
comment.save()
return JsonResponse({
"comment_id": comment.id, "comment": comment.comment,
"commented_on": comment.commented_on,
"commented_by": comment.commented_by.email
})
def form_invalid(self, form):
return JsonResponse({"error": form['comment'].errors})
class UpdateCommentView(LoginRequiredMixin, View):
http_method_names = ["post"]
def post(self, request, *args, **kwargs):
self.comment_obj = get_object_or_404(Comment, id=request.POST.get("commentid"))
if request.user == self.comment_obj.commented_by:
form = ContactCommentForm(request.POST, instance=self.comment_obj)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
else:
data = {'error': "You don't have permission to edit this comment."}
return JsonResponse(data)
def form_valid(self, form):
self.comment_obj.comment = form.cleaned_data.get("comment")
self.comment_obj.save(update_fields=["comment"])
return JsonResponse({
"commentid": self.comment_obj.id,
"comment": self.comment_obj.comment,
})
def form_invalid(self, form):
return JsonResponse({"error": form['comment'].errors})
class DeleteCommentView(LoginRequiredMixin, View):
def post(self, request, *args, **kwargs):
self.object = get_object_or_404(Comment, id=request.POST.get("comment_id"))
if request.user == self.object.commented_by:
self.object.delete()
data = {"cid": request.POST.get("comment_id")}
return JsonResponse(data)
else:
data = {'error': "You don't have permission to delete this comment."}
return JsonResponse(data)
class GetContactsView(LoginRequiredMixin, TemplateView):
model = Contact
context_object_name = "contacts"
template_name = "contacts_list.html"
def get_context_data(self, **kwargs):
context = super(GetContactsView, self).get_context_data(**kwargs)
context["contacts"] = self.get_queryset()
return context