diff --git a/frameworks/django/requirements.txt b/frameworks/django/requirements.txt index b492b47..3dc1335 100644 --- a/frameworks/django/requirements.txt +++ b/frameworks/django/requirements.txt @@ -1 +1 @@ -django == 4.0.3 +django==4.1.2 \ No newline at end of file diff --git a/frameworks/django/views.py b/frameworks/django/views.py index e2ea823..7902153 100644 --- a/frameworks/django/views.py +++ b/frameworks/django/views.py @@ -1,63 +1,62 @@ +import json import time + from uuid import uuid4 +from django.http import HttpResponse, JsonResponse +from django.views.generic import View from django.urls import path -from django.http import HttpResponse, JsonResponse, HttpResponseNotAllowed, HttpResponseBadRequest -import json - - -async def html(request): - """Return HTML content and a custom header.""" - content = "HTML OK" - headers = {'x-time': f"{time.time()}"} - return HttpResponse(content, headers=headers) - -async def upload(request): - """Load multipart data and store it as a file.""" - # 2021-04-18 Django 3.2 django.views.decorators.http.require_http_methods hasn't async support - if request.method != 'POST': - return HttpResponseNotAllowed(['POST']) - formdata = request.FILES - if 'file' not in formdata: - return HttpResponseBadRequest('ERROR') +class HtmlView(View): + """ Return HTML content and a custom header. """ + async def get(self, request): + resp = HttpResponse('HTML OK') + resp.headers['x-time'] = time.time() + return resp - with open(f"/tmp/{uuid4().hex}", 'wb') as target: - target.write(formdata['file'].read()) - return HttpResponse(target.name, content_type="text/plain") +class UploadView(View): + """ Load multipart data and store it as a file. """ + async def post(self, request): + file = request.FILES.get('file') + if not file: + return HttpResponse('ERROR', status=400) + + with open(f"/tmp/{uuid4().hex}", 'wb') as target: + target.write(file.read()) + return HttpResponse(target.name, content_type="text/plain") -async def api(request, user, record): - """Check headers for authorization, load JSON/query data and return as JSON.""" - if request.method != 'PUT': - return HttpResponseNotAllowed(['PUT']) - if not request.headers.get('authorization'): - return HttpResponse('ERROR', status=401) +class ApiView(View): + """ Check headers for authorization, load JSON/query data and return as JSON. """ + async def put(self, request, **kwargs): + if not request.headers.get('authorization'): + return HttpResponse('ERROR', status=401) - data = json.loads(request.body.decode()) - return JsonResponse({ - 'params': {'user': user, 'record': record}, - 'query': dict(request.GET), - 'data': data, - }) + data = json.loads(request.body) + return JsonResponse({ + 'params': kwargs, + 'query': request.GET.dict(), + 'data': data, + }) urlpatterns = [ - path('html', html), - path('upload', upload), - path('api/users//records/', api), + path('html', HtmlView.as_view()), + path('upload', UploadView.as_view()), + path('api/users//records/', ApiView.as_view()), ] # More load for routing system # ---------------------------- -async def req_ok(*args, **kwargs): - return HttpResponse('ok') +class ReqOk(View): + async def get(self, request): + return HttpResponse('ok') for n in range(5): - urlpatterns.insert(0, path(f"route-{n}", req_ok)) - urlpatterns.insert(0, path(f"route-dyn-{n}/", req_ok)) + urlpatterns.insert(0, path(f"route-{n}", ReqOk.as_view())) + urlpatterns.insert(0, path(f"route-dyn-{n}/", ReqOk.as_view())) \ No newline at end of file