-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
449 lines (361 loc) · 14.8 KB
/
views.py
File metadata and controls
449 lines (361 loc) · 14.8 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import django.db.models
import django.db.transaction
import django.shortcuts
import rest_framework.generics
import rest_framework.permissions
import rest_framework.response
import rest_framework.status
import rest_framework.views
import rest_framework_simplejwt.tokens
import rest_framework_simplejwt.views
import business.models
import core.pagination
import user.models
import user.permissions
import user.serializers
class UserSignUpView(
rest_framework.generics.CreateAPIView,
):
serializer_class = user.serializers.SignUpSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.save()
refresh = rest_framework_simplejwt.tokens.RefreshToken.for_user(user)
refresh['token_version'] = user.token_version
access_token = refresh.access_token
response_data = {
'access': str(access_token),
'refresh': str(refresh),
}
return rest_framework.response.Response(
response_data,
status=rest_framework.status.HTTP_200_OK,
)
class UserSignInView(
rest_framework_simplejwt.views.TokenObtainPairView,
):
serializer_class = user.serializers.SignInSerializer
class UserProfileView(
rest_framework.generics.RetrieveUpdateAPIView,
):
"""
Retrieve (GET) and partially update (PATCH)
detailed user profile information.
"""
http_method_names = ['get', 'patch', 'options', 'head']
serializer_class = user.serializers.UserProfileSerializer
permission_classes = [rest_framework.permissions.IsAuthenticated]
def get_object(self):
return self.request.user
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
class UserPromoDetailView(rest_framework.generics.RetrieveAPIView):
"""
Retrieve (GET) information about the promo without receiving a promo code.
"""
queryset = business.models.Promo.objects.select_related('company').only(
'id',
'company__id',
'company__name',
'description',
'image_url',
'active',
'active_from',
'active_until',
'mode',
'used_count',
'like_count',
'comment_count',
)
serializer_class = user.serializers.UserPromoDetailSerializer
permission_classes = [
rest_framework.permissions.IsAuthenticated,
]
lookup_field = 'id'
class UserFeedView(rest_framework.generics.ListAPIView):
serializer_class = user.serializers.PromoFeedSerializer
permission_classes = [rest_framework.permissions.IsAuthenticated]
pagination_class = core.pagination.CustomLimitOffsetPagination
def get_queryset(self):
user = self.request.user
user_age = user.other.get('age')
user_country = user.other.get('country').lower()
active_filter = self.request.query_params.get('active')
return business.models.Promo.objects.get_feed_for_user(
user,
active_filter=active_filter,
user_country=user_country,
user_age=user_age,
)
def filter_queryset(self, queryset):
queryset = super().filter_queryset(queryset)
category_param = self.request.query_params.get('category')
if category_param:
needle = f'"{category_param.lower()}"'
queryset = queryset.filter(target__categories__icontains=needle)
return queryset
def list(self, request, *args, **kwargs):
query_serializer = user.serializers.UserFeedQuerySerializer(
data=request.query_params,
)
query_serializer.is_valid(raise_exception=True)
self.validated_query_params = query_serializer.validated_data
return super().list(request, *args, **kwargs)
class UserPromoLikeView(rest_framework.views.APIView):
permission_classes = [rest_framework.permissions.IsAuthenticated]
def get_promo_object(self, promo_id):
return django.shortcuts.get_object_or_404(
business.models.Promo,
id=promo_id,
)
def post(self, request, id):
"""Add a like to the promo code."""
with django.db.transaction.atomic():
promo = self.get_promo_object(id)
like_obj, created = user.models.PromoLike.objects.get_or_create(
user=request.user,
promo=promo,
)
if created:
promo.like_count = django.db.models.F('like_count') + 1
promo.save(update_fields=['like_count'])
promo.refresh_from_db()
return rest_framework.response.Response(
{'status': 'ok'},
status=rest_framework.status.HTTP_200_OK,
)
def delete(self, request, id):
"""Remove a like from the promo code."""
with django.db.transaction.atomic():
promo = self.get_promo_object(id)
# Idempotency: if the like doesn't exist,
# do nothing and still return 200 OK.
like_instance = user.models.PromoLike.objects.filter(
user=request.user,
promo=promo,
).first()
if like_instance:
like_instance.delete()
promo.like_count = django.db.models.F('like_count') - 1
promo.save(update_fields=['like_count'])
promo.refresh_from_db()
return rest_framework.response.Response(
{'status': 'ok'},
status=rest_framework.status.HTTP_200_OK,
)
class PromoObjectMixin:
"""Mixin for retrieving the Promo object and saving it to self.promo"""
def dispatch(self, request, *args, **kwargs):
self.promo = django.shortcuts.get_object_or_404(
business.models.Promo.objects.select_for_update(),
pk=self.kwargs.get('promo_id'),
)
return super().dispatch(request, *args, **kwargs)
class PromoCommentListCreateView(
PromoObjectMixin,
rest_framework.generics.ListCreateAPIView,
):
permission_classes = [rest_framework.permissions.IsAuthenticated]
pagination_class = core.pagination.CustomLimitOffsetPagination
def get_serializer_class(self):
if self.request.method == 'POST':
return user.serializers.CommentCreateSerializer
return user.serializers.CommentSerializer
def get_queryset(self):
return user.models.PromoComment.objects.filter(
promo=self.promo,
).select_related('author')
def perform_create(self, serializer):
serializer.save(author=self.request.user, promo=self.promo)
self.promo.comment_count = django.db.models.F('comment_count') + 1
self.promo.save(update_fields=['comment_count'])
def create(self, request, *args, **kwargs):
create_serializer = self.get_serializer(data=request.data)
create_serializer.is_valid(raise_exception=True)
self.perform_create(create_serializer)
response_serializer = user.serializers.CommentSerializer(
create_serializer.instance,
)
headers = self.get_success_headers(response_serializer.data)
return rest_framework.response.Response(
response_serializer.data,
status=rest_framework.status.HTTP_201_CREATED,
headers=headers,
)
def list(self, request, *args, **kwargs):
query_serializer = user.serializers.UserFeedQuerySerializer(
data=request.query_params,
)
query_serializer.is_valid(raise_exception=True)
return super().list(request, *args, **kwargs)
class PromoCommentDetailView(
PromoObjectMixin,
rest_framework.generics.RetrieveUpdateDestroyAPIView,
):
permission_classes = [
rest_framework.permissions.IsAuthenticated,
user.permissions.IsOwnerOrReadOnly,
]
lookup_url_kwarg = 'comment_id'
http_method_names = ['get', 'put', 'delete', 'options', 'head']
def get_serializer_class(self):
if self.request.method == 'PUT':
return user.serializers.CommentUpdateSerializer
return user.serializers.CommentSerializer
def get_queryset(self):
return user.models.PromoComment.objects.filter(
promo=self.promo,
).select_related('author')
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
update_serializer = self.get_serializer(
instance,
data=request.data,
partial=partial,
)
update_serializer.is_valid(raise_exception=True)
self.perform_update(update_serializer)
response_serializer = user.serializers.CommentSerializer(instance)
return rest_framework.response.Response(response_serializer.data)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
promo = instance.promo
promo.comment_count = django.db.models.F('comment_count') - 1
promo.save(update_fields=['comment_count'])
return rest_framework.response.Response(
{'status': 'ok'},
status=rest_framework.status.HTTP_200_OK,
)
class PromoActivateView(rest_framework.views.APIView):
permission_classes = [rest_framework.permissions.IsAuthenticated]
allowed_methods = ['post', 'options', 'head']
def _validate_targeting(self, user_, promo):
user_age = user_.other.get('age')
user_country = user_.other.get('country').lower()
target = promo.target
if not target:
return None
if target.get('country') and user_country != target['country'].lower():
return rest_framework.response.Response(
{'error': 'Targeting mismatch: country.'},
status=rest_framework.status.HTTP_403_FORBIDDEN,
)
if target.get('age_from') and (
user_age is None or user_age < target['age_from']
):
return rest_framework.response.Response(
{'error': 'Targeting mismatch: age.'},
status=rest_framework.status.HTTP_403_FORBIDDEN,
)
if target.get('age_until') and (
user_age is None or user_age > target['age_until']
):
return rest_framework.response.Response(
{'error': 'Targeting mismatch: age.'},
status=rest_framework.status.HTTP_403_FORBIDDEN,
)
return None
def _validate_is_active(self, promo):
if not promo.active or not promo.is_active:
return rest_framework.response.Response(
{'error': 'Promo is not active.'},
status=rest_framework.status.HTTP_403_FORBIDDEN,
)
return None
def _validate_antifraud(self, user_, promo):
antifraud_response = (
user.antifraud_service.antifraud_service.get_verdict(
user_.email,
str(promo.id),
)
)
if not antifraud_response.get('ok'):
return rest_framework.response.Response(
{'error': 'Activation forbidden by anti-fraud system.'},
status=rest_framework.status.HTTP_403_FORBIDDEN,
)
return None
def _activate_code(self, user_, promo):
try:
with django.db.transaction.atomic():
promo_for_update = (
business.models.Promo.objects.select_for_update().get(
id=promo.id,
)
)
promo_code_value = None
if (
promo_for_update.mode
== business.constants.PROMO_MODE_COMMON
):
if (
promo_for_update.used_count
< promo_for_update.max_count
):
promo_for_update.used_count += 1
promo_for_update.save(update_fields=['used_count'])
promo_code_value = promo_for_update.promo_common
else:
raise ValueError('No common codes left.')
elif (
promo_for_update.mode
== business.constants.PROMO_MODE_UNIQUE
):
unique_code = promo_for_update.unique_codes.filter(
is_used=False,
).first()
if unique_code:
unique_code.is_used = True
unique_code.used_at = django.utils.timezone.now()
unique_code.save(update_fields=['is_used', 'used_at'])
promo_code_value = unique_code.code
else:
raise ValueError('No unique codes left.')
if promo_code_value:
user.models.PromoActivationHistory.objects.create(
user=user_,
promo=promo,
)
serializer = user.serializers.PromoActivationSerializer(
data={'promo': promo_code_value},
)
serializer.is_valid(raise_exception=True)
return rest_framework.response.Response(
serializer.data,
status=rest_framework.status.HTTP_200_OK,
)
raise ValueError('Promo code could not be activated.')
except ValueError as e:
return rest_framework.response.Response(
{'error': str(e)},
status=rest_framework.status.HTTP_403_FORBIDDEN,
)
def post(self, request, id):
promo = django.shortcuts.get_object_or_404(
business.models.Promo,
id=id,
)
user_ = request.user
if (response := self._validate_targeting(user_, promo)) is not None:
return response
if (response := self._validate_is_active(promo)) is not None:
return response
if (response := self._validate_antifraud(user_, promo)) is not None:
return response
return self._activate_code(user_, promo)
class PromoHistoryView(rest_framework.generics.ListAPIView):
"""
Returns the history of activated promo codes for the current user.
"""
serializer_class = user.serializers.UserPromoDetailSerializer
permission_classes = [rest_framework.permissions.IsAuthenticated]
pagination_class = core.pagination.CustomLimitOffsetPagination
def get_queryset(self):
user = self.request.user
queryset = business.models.Promo.objects.filter(
activations_history__user=user,
).order_by('-activations_history__activated_at')
return queryset # noqa: RET504