1
|
|
|
import json |
2
|
|
|
from braces import views |
3
|
|
|
from django.http.response import HttpResponse |
4
|
|
|
from django.http.response import HttpResponseRedirect |
5
|
|
|
from django.shortcuts import render |
6
|
|
|
from django.views import generic |
7
|
|
|
|
8
|
|
|
from wye.profiles.models import Profile |
9
|
|
|
from . import forms, models |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class RegionalListView(views.StaffuserRequiredMixin, generic.ListView): |
13
|
|
|
model = models.RegionalLead |
14
|
|
|
template_name = 'regions/index.html' |
15
|
|
|
|
16
|
|
|
def get_context_data(self, *args, **kwargs): |
17
|
|
|
context = super( |
18
|
|
|
RegionalListView, self).get_context_data(*args, **kwargs) |
19
|
|
|
context['state_list'] = models.State.objects.all() |
20
|
|
|
context['location_list'] = models.Location.objects.all() |
21
|
|
|
context['regional_list'] = models.RegionalLead.objects.all() |
22
|
|
|
context['user'] = self.request.user |
23
|
|
|
return context |
24
|
|
|
|
25
|
|
|
|
26
|
|
View Code Duplication |
class StateCreateView(views.StaffuserRequiredMixin, generic.CreateView): |
|
|
|
|
27
|
|
|
model = models.State |
28
|
|
|
form_class = forms.StateForm |
29
|
|
|
success_url = '/region/' |
30
|
|
|
template_name = 'regions/state/create.html' |
31
|
|
|
|
32
|
|
|
def post(self, request, *args, **kwargs): |
33
|
|
|
form = forms.StateForm(data=request.POST) |
34
|
|
|
if form.is_valid(): |
35
|
|
|
form.modified_by = request.user |
36
|
|
|
form.created_by = request.user |
37
|
|
|
form.instance.save() |
38
|
|
|
return HttpResponseRedirect(self.success_url) |
39
|
|
|
else: |
40
|
|
|
return render(request, self.template_name, {'form': form}) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
class StateEditView(views.StaffuserRequiredMixin, generic.UpdateView): |
44
|
|
|
model = models.State |
45
|
|
|
form_class = forms.StateForm |
46
|
|
|
success_url = '/region/' |
47
|
|
|
template_name = 'regions/state/edit.html' |
48
|
|
|
|
49
|
|
|
|
50
|
|
View Code Duplication |
class LocationCreateView(views.StaffuserRequiredMixin, generic.CreateView): |
|
|
|
|
51
|
|
|
model = models.Location |
52
|
|
|
form_class = forms.LocationForm |
53
|
|
|
success_url = '/region/' |
54
|
|
|
template_name = 'regions/location/create.html' |
55
|
|
|
|
56
|
|
|
def post(self, request, *args, **kwargs): |
57
|
|
|
form = forms.LocationForm(data=request.POST) |
58
|
|
|
if form.is_valid(): |
59
|
|
|
form.modified_by = request.user |
60
|
|
|
form.created_by = request.user |
61
|
|
|
form.instance.save() |
62
|
|
|
return HttpResponseRedirect(self.success_url) |
63
|
|
|
else: |
64
|
|
|
return render(request, self.template_name, {'form': form}) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
class LocationUpdateView(views.StaffuserRequiredMixin, generic.UpdateView): |
68
|
|
|
model = models.Location |
69
|
|
|
form_class = forms.LocationForm |
70
|
|
|
success_url = '/region/' |
71
|
|
|
template_name = 'regions/location/edit.html' |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
class RegionalLeadCreateView(views.StaffuserRequiredMixin, generic.CreateView): |
75
|
|
|
model = models.RegionalLead |
76
|
|
|
form_class = forms.RegionalLeadForm |
77
|
|
|
success_url = '/region/' |
78
|
|
|
template_name = 'regions/lead/create.html' |
79
|
|
|
|
80
|
|
|
def get_leads(self, request, l_id): |
81
|
|
|
lead_choices = Profile.objects.filter( |
82
|
|
|
location=l_id |
83
|
|
|
).values_list('user__id', 'user__username') |
84
|
|
|
return HttpResponse(json.dumps(list(lead_choices))) |
85
|
|
|
|
86
|
|
|
def post(self, request, *args, **kwargs): |
87
|
|
|
if request.method == 'POST' and request.is_ajax: |
88
|
|
|
form = forms.RegionalLeadForm(data=request.POST) |
89
|
|
|
lead_choices = request.POST.get('leads') |
90
|
|
|
form.fields['leads'].choices = (lead_choices, lead_choices) |
91
|
|
|
|
92
|
|
|
if form.is_valid(): |
93
|
|
|
form.modified_by = request.user |
94
|
|
|
form.created_by = request.user |
95
|
|
|
form.save() |
96
|
|
|
return HttpResponseRedirect(self.success_url) |
97
|
|
|
else: |
98
|
|
|
return render(request, self.template_name, {'form': form}) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
class RegionalLeadUpdateView(views.StaffuserRequiredMixin, generic.UpdateView): |
102
|
|
|
model = models.RegionalLead |
103
|
|
|
form_class = forms.RegionalLeadForm |
104
|
|
|
success_url = '/region/' |
105
|
|
|
template_name = 'regions/lead/edit.html' |
106
|
|
|
|