-
Notifications
You must be signed in to change notification settings - Fork 30
/
views.py
80 lines (65 loc) · 3.06 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
import uuid
from celery.result import AsyncResult
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from core.common.tasks import bulk_import, bulk_priority_import
task_param = openapi.Parameter(
'task', openapi.IN_QUERY, description="task uuid (mandatory)", type=openapi.TYPE_STRING
)
result_param = openapi.Parameter(
'result', openapi.IN_QUERY, description="result format (json | report) (optional)", type=openapi.TYPE_STRING
)
update_if_exists_param = openapi.Parameter(
'update_if_exists', openapi.IN_QUERY, description="true | false (mandatory)", type=openapi.TYPE_STRING,
default='true'
)
class BulkImportView(APIView):
permission_classes = (IsAuthenticated, )
@swagger_auto_schema(
manual_parameters=[update_if_exists_param],
request_body=openapi.Schema(type=openapi.TYPE_OBJECT)
)
def post(self, request):
user = self.request.user
username = user.username
update_if_exists = request.GET.get('update_if_exists', 'true')
if update_if_exists not in ['true', 'false']:
return Response(
dict(exception="update_if_exists must be either 'true' or 'false'"),
status=status.HTTP_400_BAD_REQUEST
)
update_if_exists = update_if_exists == 'true'
task_id = str(uuid.uuid4()) + '-' + username
data = request.body.decode('utf-8') if isinstance(request.body, bytes) else request.body
if user.is_staff:
task = bulk_priority_import.apply_async((data, username, update_if_exists), task_id=task_id)
else:
task = bulk_import.apply_async((data, username, update_if_exists), task_id=task_id)
return Response(dict(task=task.id, state=task.state))
@swagger_auto_schema(manual_parameters=[task_param, result_param])
def get(self, request): # pylint: disable=too-many-return-statements
task_id = request.GET.get('task')
result_format = request.GET.get('result')
if not task_id:
return Response(dict(exception='Required task id'), status=status.HTTP_400_BAD_REQUEST)
username = task_id[37:]
user = self.request.user
if not user.is_staff and user.username != username:
return Response(status=status.HTTP_403_FORBIDDEN)
task = AsyncResult(task_id)
if task.successful():
result = task.get()
if result_format == 'json':
response = Response(result.json, content_type="application/json")
response['Content-Encoding'] = 'gzip'
return response
if result_format == 'report':
return Response(result.report)
return Response(result.detailed_summary)
if task.failed():
return Response(dict(exception=str(task.result)), status=status.HTTP_400_BAD_REQUEST)
return Response(dict(task=task.id, state=task.state))