From 2b842a723849d30e05776468ab114f7bf0f9288b Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 06:56:34 +0200 Subject: [PATCH] uploadfiles --- alkator/urls.py | 3 ++- alkatorapi/views.py | 11 +++++++++++ frontend/src/scripts/index.js | 35 ++++++++++++++++++++++++----------- photos/{ => 1}/.gitkeep | 0 photos/2/.gitkeep | 0 photos/uploads/.gitkeep | 0 resize_photos.py | 2 +- 7 files changed, 38 insertions(+), 13 deletions(-) rename photos/{ => 1}/.gitkeep (100%) mode change 100644 => 100755 create mode 100755 photos/2/.gitkeep create mode 100755 photos/uploads/.gitkeep diff --git a/alkator/urls.py b/alkator/urls.py index 4f1c9ea..ad6f677 100644 --- a/alkator/urls.py +++ b/alkator/urls.py @@ -16,7 +16,7 @@ Including another URLconf """ from django.contrib import admin from django.urls import path -from alkatorapi.views import register, results, photos, payment_result, payment_state, invoice +from alkatorapi.views import register, results, photos, payment_result, payment_state, invoice, upload_files urlpatterns = [ path('admin/', admin.site.urls), @@ -26,4 +26,5 @@ urlpatterns = [ path('api/payment_result', payment_result), path('api/payment_state', payment_state), path('api/invoice', invoice), + path('api/upload_files', upload_files) ] diff --git a/alkatorapi/views.py b/alkatorapi/views.py index 1cdca50..b2b9de9 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -196,6 +196,17 @@ def photos(request): return HttpResponse(json.dumps(rtn), content_type='application/json') +@csrf_exempt +def upload_files(request): + for name, file request.FILES.items(): + print(name) + rand = random.randint(9999999999999) + random_name = f'photos/uploads/{rand}.jpg' + with open(random_name, "wb+") as destination: + for chunk in f.chunks(): + destination.write(chunk) + + @staff_member_required def invoice(request): user = User.objects.get(invoice_id=request.GET['invoice_id']) diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 3c15d83..df54180 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -57,8 +57,9 @@ class Main extends Component { onSubmit(event){ event.preventDefault(); let form = document.getElementById("form"); + let api_endpoint = form.action; let formData = new FormData(form); - fetch(addr_prefix + "/api/register", { + fetch(addr_prefix + api_endpoint, { method:"POST", body: formData, }).then(resp => resp.json()).then(json => { @@ -72,7 +73,9 @@ class Main extends Component { text: json.success, status: "success", }) - window.open(json.redirect ,"_self") + if (json.redirect){ + window.open(json.redirect ,"_self") + } } }); } @@ -122,12 +125,30 @@ class Main extends Component { + + {this.state.status == "success" && +
+ {this.state.text} +
} + {this.state.status == "failed" && +
+ {this.state.text} +
} + {this.state.page == "#upload_files" && +
+
this.onSubmit(e)}> + + +
+
} {this.state.page == "#payment" &&
Platební bránu poskytuje firma Comgate, a.s. Platit je možné pomocí platební karty nebo bankovním převodem. Podrobnosti a přesný postup platby včetně animací najdete v odkazech.
@@ -144,14 +165,6 @@ class Main extends Component { } {this.state.page == "" &&
- {this.state.status == "success" && -
- {this.state.text} -
} - {this.state.status == "failed" && -
- {this.state.text} -
} {this.state.faq &&
} {this.state.faq && @@ -286,7 +299,7 @@ class Main extends Component {

Stále nevíš, jestli se zúčastníš? Přečti si náš

-
this.onSubmit(e)}> + this.onSubmit(e)}>

Registrace

diff --git a/photos/.gitkeep b/photos/1/.gitkeep old mode 100644 new mode 100755 similarity index 100% rename from photos/.gitkeep rename to photos/1/.gitkeep diff --git a/photos/2/.gitkeep b/photos/2/.gitkeep new file mode 100755 index 0000000..e69de29 diff --git a/photos/uploads/.gitkeep b/photos/uploads/.gitkeep new file mode 100755 index 0000000..e69de29 diff --git a/resize_photos.py b/resize_photos.py index ed1c20a..dd76887 100644 --- a/resize_photos.py +++ b/resize_photos.py @@ -1,7 +1,7 @@ import glob import PIL.Image -photos = glob.glob('photos/*.jpg') +photos = glob.glob('photos/2/*.jpg') for image in photos: print(image)