|
1 | 1 | import re
|
2 | 2 |
|
3 | 3 | import django.db.models
|
| 4 | +import django.shortcuts |
4 | 5 | import pycountry
|
5 | 6 | import rest_framework.exceptions
|
6 | 7 | import rest_framework.generics
|
7 | 8 | import rest_framework.permissions
|
8 | 9 | import rest_framework.response
|
9 | 10 | import rest_framework.serializers
|
10 | 11 | import rest_framework.status
|
| 12 | +import rest_framework.views |
11 | 13 | import rest_framework_simplejwt.exceptions
|
12 | 14 | import rest_framework_simplejwt.tokens
|
13 | 15 | import rest_framework_simplejwt.views
|
@@ -275,3 +277,42 @@ def _validate_limit(self):
|
275 | 277 | raise rest_framework.exceptions.ValidationError(
|
276 | 278 | 'Limit cannot be negative.',
|
277 | 279 | )
|
| 280 | + |
| 281 | + |
| 282 | +class CompanyPromoDetailView(rest_framework.views.APIView): |
| 283 | + permission_classes = [ |
| 284 | + rest_framework.permissions.IsAuthenticated, |
| 285 | + business.permissions.IsCompanyUser, |
| 286 | + ] |
| 287 | + |
| 288 | + lookup_field = 'id' |
| 289 | + |
| 290 | + def get(self, request, id): |
| 291 | + try: |
| 292 | + promo = business.models.Promo.objects.get( |
| 293 | + id=id, |
| 294 | + ) |
| 295 | + except business.models.Promo.DoesNotExist: |
| 296 | + raise rest_framework.exceptions.NotFound( |
| 297 | + 'Promo not found,', |
| 298 | + ) |
| 299 | + |
| 300 | + if promo.company != request.user: |
| 301 | + return rest_framework.response.Response( |
| 302 | + { |
| 303 | + 'status': 'error', |
| 304 | + 'message': ( |
| 305 | + 'The promo code does not belong to this company.' |
| 306 | + ), |
| 307 | + }, |
| 308 | + status=rest_framework.status.HTTP_403_FORBIDDEN, |
| 309 | + ) |
| 310 | + |
| 311 | + serializer = business.serializers.PromoCreateSerializer( |
| 312 | + promo, |
| 313 | + ) |
| 314 | + |
| 315 | + return rest_framework.response.Response( |
| 316 | + serializer.data, |
| 317 | + status=rest_framework.status.HTTP_200_OK, |
| 318 | + ) |
0 commit comments