-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_person.py
274 lines (234 loc) · 11.9 KB
/
contact_person.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
from django.shortcuts import render, redirect
from django.http import Http404, JsonResponse, HttpResponse
from django.conf import settings
from infinity_fire_solutions.aws_helper import *
from infinity_fire_solutions.permission import *
from .models import *
from .serializers import ContactPersonSerializer
from infinity_fire_solutions.response_schemas import create_api_response, render_html_response
from django.contrib import messages
from rest_framework import generics, status, filters
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.renderers import TemplateHTMLRenderer, JSONRenderer
class CustomerContactPersonView(CustomAuthenticationMixin, generics.CreateAPIView):
"""
View for adding or updating a contact.
Supports both HTML and JSON response formats.
"""
serializer_class = ContactPersonSerializer
renderer_classes = [TemplateHTMLRenderer, JSONRenderer]
template_name = 'customer_contact_person.html'
swagger_schema = None
queryset = ContactPerson.objects.all()
def get_customer(self):
customer_id = self.kwargs.get('customer_id', '')
if customer_id:
return User.objects.filter(id=customer_id).first()
return None
def get_queryset(self, customer):
"""
Get the queryset for listing Conatct items.
Returns:
QuerySet: A queryset of Conatct items filtered based on the authenticated user's ID.
"""
queryset = super().get_queryset()
if customer and queryset:
queryset = queryset.filter(customer=customer.customermeta).all()
return queryset
return None
def get_object(self, queryset):
pk=self.kwargs.get('address_id', None)
instance = None
if pk and queryset:
instance = queryset.filter(pk=pk).first()
return instance
return instance
def get(self, request, *args, **kwargs):
"""
Handle GET request to display a form for updating a contact.
If the contact exists, retrieve the serialized data and render the HTML template.
If the contact does not exist, render the HTML template with an empty serializer.
"""
# Call the handle_unauthenticated method to handle unauthenticated access
authenticated_user, data_access_value = check_authentication_and_permissions(
self,"customer", HasUpdateDataPermission, 'change'
)
if isinstance(authenticated_user, HttpResponseRedirect):
return authenticated_user # Redirect the user to the page specified in the HttpResponseRedirect
customer = self.get_customer()
if not customer:
messages.error(request, "You are not authorized to perform this action")
return redirect(reverse('customer_list'))
queryset = self.get_queryset(customer)
instance = self.get_object(queryset)
if not instance and kwargs.get('address_id', ''):
messages.error(request, "You are not authorized to perform this action")
return redirect(reverse('customer_contact_person', kwargs={'customer_id': customer.id}))
serializer = self.serializer_class(instance=instance) if instance else self.serializer_class()
if request.accepted_renderer.format == 'html':
context = {
'serializer':serializer,
'customer_instance': customer,
'contact_person_list': queryset
}
return render_html_response(context,self.template_name)
else:
messages.error(request, "Customer not found OR You are not authorized to perform this action.")
return redirect(reverse('customer_list'))
def post(self, request, *args, **kwargs):
customer = self.get_customer()
if not customer:
messages.error(request, "You are not authorized to perform this action")
return redirect(reverse('customer_list'))
queryset = self.get_queryset(customer)
instance = self.get_object(queryset)
if not instance and kwargs.get('address_id', ''):
messages.error(request, "You are not authorized to perform this action")
return redirect(reverse('customer_contact_person', kwargs={'customer_id': customer.id}))
serializer = self.serializer_class(data=request.data, instance=instance) if instance else self.serializer_class(data=request.data)
message = f"Your Customer contact person has been {'updated' if instance else 'added'} successfully!"
if serializer.is_valid():
serializer.validated_data['customer'] = customer.customermeta
# If the serializer data is valid, save the billing address instance.
serializer.update(instance=instance, validated_data=serializer.validated_data) if instance else serializer.save()
if request.accepted_renderer.format == 'html':
# For HTML requests, display a success message and redirect to the customer's billing address list.
messages.success(request, message)
return redirect(reverse('customer_contact_person', kwargs={'customer_id': kwargs['customer_id']}))
else:
# For API requests, return a success response with serialized data.
return create_api_response(message=message, data=serializer.data, status=status.HTTP_200_OK)
else:
message = "Unable to submit your data please validate and try again."
if request.accepted_renderer.format == 'html':
# For HTML requests with invalid data, render the template with error messages.
context = {
'serializer':serializer,
'customer_instance': customer,
'site_address_list': queryset
}
return render(request, self.template_name, context)
else:
# For API requests with invalid data, return an error response with serializer errors.
return create_api_response(message=message, data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class CustomerRemoveContactPersonView(CustomAuthenticationMixin, generics.DestroyAPIView):
"""
View to remove a ContactPerson associated with a Customer.
"""
renderer_classes = [TemplateHTMLRenderer, JSONRenderer]
swagger_schema = None
queryset = ContactPerson.objects.all()
def get_customer(self):
customer_id = self.kwargs.get('customer_id', '')
if customer_id:
return User.objects.filter(id=customer_id).first()
return None
def get_queryset(self, customer):
"""
Get the queryset for listing Conatct items.
Returns:
QuerySet: A queryset of Conatct items filtered based on the authenticated user's ID.
"""
queryset = super().get_queryset()
if customer and queryset:
queryset = queryset.filter(customer=customer.customermeta).all()
return queryset
return None
def get_object(self, queryset):
pk=self.kwargs.get('address_id', None)
instance = None
if pk and queryset:
instance = queryset.filter(pk=pk).first()
return instance
return instance
def destroy(self, request, *args, **kwargs):
"""
Handles DELETE request to remove the document associated with a conversation.
"""
# Call the handle_unauthenticated method to handle unauthenticated access
authenticated_user, data_access_value = check_authentication_and_permissions(
self,"customer", HasUpdateDataPermission, 'change'
)
if isinstance(authenticated_user, HttpResponseRedirect):
return authenticated_user # Redirect the user to the page specified in the HttpResponseRedirect
customer = self.get_customer()
if not customer:
messages.error(request, "You are not authorized to perform this action")
return create_api_response(status_code=status.HTTP_404_NOT_FOUND, message='You are not authorized to perform this action')
queryset = self.get_queryset(customer)
instance = self.get_object(queryset)
if not instance and kwargs.get('address_id', ''):
messages.error(request, "You are not authorized to perform this action")
return create_api_response(status_code=status.HTTP_404_NOT_FOUND, message='You are not authorized to perform this action')
if instance.user:
instance.user.delete()
instance.delete()
messages.success(request, 'Customer contact person has been deleted successfully!')
return create_api_response(status_code=status.HTTP_200_OK, message='Customer contact person has been deleted successfully!' )
class CustomerContactPersonDetailView(CustomAuthenticationMixin, generics.CreateAPIView):
"""
View for adding or updating a contact.
Supports both HTML and JSON response formats.
"""
serializer_class = ContactPersonSerializer
renderer_classes = [TemplateHTMLRenderer, JSONRenderer]
template_name = 'customer_contact_view.html'
swagger_schema = None
queryset = ContactPerson.objects.all()
def get_customer(self):
customer_id = self.kwargs.get('customer_id', '')
if customer_id:
return User.objects.filter(id=customer_id).first()
return None
def get_queryset(self, customer):
"""
Get the queryset for listing Conatct items.
Returns:
QuerySet: A queryset of Conatct items filtered based on the authenticated user's ID.
"""
queryset = super().get_queryset()
if customer and queryset:
queryset = queryset.filter(customer=customer.customermeta).all()
return queryset
return None
def get_object(self, queryset):
pk=self.kwargs.get('address_id', None)
instance = None
if pk and queryset:
instance = queryset.filter(pk=pk).first()
return instance
return instance
def get(self, request, *args, **kwargs):
"""
Handle GET request to display a form for updating a contact.
If the contact exists, retrieve the serialized data and render the HTML template.
If the contact does not exist, render the HTML template with an empty serializer.
"""
# Call the handle_unauthenticated method to handle unauthenticated access
authenticated_user, data_access_value = check_authentication_and_permissions(
self,"customer", HasUpdateDataPermission, 'change'
)
if isinstance(authenticated_user, HttpResponseRedirect):
return authenticated_user # Redirect the user to the page specified in the HttpResponseRedirect
customer = self.get_customer()
if not customer:
messages.error(request, "You are not authorized to perform this action")
return redirect(reverse('customer_list'))
queryset = self.get_queryset(customer)
instance = self.get_object(queryset)
if not instance:
messages.error(request, "You are not authorized to perform this action")
return redirect(reverse('customer_contact_person', kwargs={'customer_id': customer.id}))
serializer = self.serializer_class(instance=instance)
if request.accepted_renderer.format == 'html':
context = {
'serializer':serializer.data,
'customer_instance': customer,
'customer_id': customer.id,
'contact_person_list': queryset
}
return render_html_response(context,self.template_name)
else:
messages.error(request, "Customer not found OR You are not authorized to perform this action.")
return redirect(reverse('customer_list'))