From 2b842a723849d30e05776468ab114f7bf0f9288b Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 06:56:34 +0200 Subject: [PATCH 01/12] 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) From 23bfee6aa1fd773e7511d47bbcf4268154eafa25 Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:05:40 +0200 Subject: [PATCH 02/12] fix of for loop --- alkatorapi/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index b2b9de9..e48012a 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -198,13 +198,14 @@ def photos(request): @csrf_exempt def upload_files(request): - for name, file request.FILES.items(): + for name, file in 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) + return HttpResponse('{"success":"Úspěšně nahráno! Počkejte až dojde ke schválení nahraných souborů.}', content_type='application/json') @staff_member_required From b48b24cc0e0c358d68ff5f2b36bea5b57766b1ac Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:06:33 +0200 Subject: [PATCH 03/12] fix of kokotina --- alkatorapi/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index e48012a..2cd2490 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -203,8 +203,8 @@ def upload_files(request): 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) + for chunk in f.chunks(): + destination.write(chunk) return HttpResponse('{"success":"Úspěšně nahráno! Počkejte až dojde ke schválení nahraných souborů.}', content_type='application/json') From 86e97dd55ad7bd2d89e84060d2b78442fe3dbc4e Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:26:08 +0200 Subject: [PATCH 04/12] hopefully workings uploads --- frontend/src/scripts/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index df54180..94058fc 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -15,6 +15,7 @@ class Main extends Component { constructor(props) { super(props); this.state = { + files: [], text: "", status: "", faq: false, @@ -54,11 +55,18 @@ class Main extends Component { onHashChange(event){ this.setState({page: window.location.hash}) } + handleChange(event) { + this.setState({files: [...event.target.files]}) + } onSubmit(event){ event.preventDefault(); let form = document.getElementById("form"); let api_endpoint = form.action; let formData = new FormData(form); + this.state.files.forEach((file, _)=>{ + formData.append(`${file.name}`, file); + }) + formData fetch(addr_prefix + api_endpoint, { method:"POST", body: formData, @@ -145,7 +153,7 @@ class Main extends Component { {this.state.page == "#upload_files" &&
this.onSubmit(e)}> - +
} From f2614ab9af9d4cfa8e20a4be70661dd126032aca Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:39:54 +0200 Subject: [PATCH 05/12] fix of uploading files --- alkatorapi/views.py | 2 +- frontend/src/scripts/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index 2cd2490..76836cf 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -205,7 +205,7 @@ def upload_files(request): with open(random_name, "wb+") as destination: for chunk in f.chunks(): destination.write(chunk) - return HttpResponse('{"success":"Úspěšně nahráno! Počkejte až dojde ke schválení nahraných souborů.}', content_type='application/json') + return HttpResponse('{"success":"Úspěšně nahráno! Počkejte až dojde ke schválení nahraných souborů."}', content_type='application/json') @staff_member_required diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 94058fc..11b6837 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -55,7 +55,7 @@ class Main extends Component { onHashChange(event){ this.setState({page: window.location.hash}) } - handleChange(event) { + handleChangeFiles(event) { this.setState({files: [...event.target.files]}) } onSubmit(event){ @@ -63,10 +63,10 @@ class Main extends Component { let form = document.getElementById("form"); let api_endpoint = form.action; let formData = new FormData(form); + console.log(this.state.files) this.state.files.forEach((file, _)=>{ - formData.append(`${file.name}`, file); + formData.append(file.name, file); }) - formData fetch(addr_prefix + api_endpoint, { method:"POST", body: formData, From 357537b6d2b3ab674276e919aba1e931d010d68f Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:49:12 +0200 Subject: [PATCH 06/12] hopefully last fix --- frontend/src/scripts/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 11b6837..801f302 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -63,10 +63,11 @@ class Main extends Component { let form = document.getElementById("form"); let api_endpoint = form.action; let formData = new FormData(form); - console.log(this.state.files) - this.state.files.forEach((file, _)=>{ - formData.append(file.name, file); - }) + if (this.state.files && api_endpoint == "/api/upload_files"){ + this.state.files.forEach((file, _)=>{ + formData.append(file.name, file); + }) + } fetch(addr_prefix + api_endpoint, { method:"POST", body: formData, @@ -153,7 +154,7 @@ class Main extends Component { {this.state.page == "#upload_files" &&
this.onSubmit(e)}> - +
} @@ -276,7 +277,7 @@ class Main extends Component {

Sportem ku chlastu!

-

Výsledky 1. ročníku

+

Výsledky 1. ročníku

{this.state.results && From 6af29143bf4c7995d54ef9ccbb7602406479dc41 Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:54:08 +0200 Subject: [PATCH 07/12] add name to files selector --- frontend/src/scripts/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 801f302..7fbb97f 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -154,7 +154,7 @@ class Main extends Component { {this.state.page == "#upload_files" &&
this.onSubmit(e)}> - +
} From 50bc8a2034d0bb5a0120232f1525d4fdcaa34e78 Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 07:55:05 +0200 Subject: [PATCH 08/12] reoder bind --- frontend/src/scripts/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 7fbb97f..bcb8a5d 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -154,7 +154,7 @@ class Main extends Component { {this.state.page == "#upload_files" &&
this.onSubmit(e)}> - +
} From b0b5449ddbe1d557a64b03f64faae545deffd361 Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 10:51:51 +0200 Subject: [PATCH 09/12] working files? --- alkatorapi/views.py | 2 +- frontend/src/scripts/index.js | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index 76836cf..bc7e0f6 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -203,7 +203,7 @@ def upload_files(request): rand = random.randint(9999999999999) random_name = f'photos/uploads/{rand}.jpg' with open(random_name, "wb+") as destination: - for chunk in f.chunks(): + for chunk in file.chunks(): destination.write(chunk) return HttpResponse('{"success":"Úspěšně nahráno! Počkejte až dojde ke schválení nahraných souborů."}', content_type='application/json') diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index bcb8a5d..e5f4ac4 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -63,11 +63,6 @@ class Main extends Component { let form = document.getElementById("form"); let api_endpoint = form.action; let formData = new FormData(form); - if (this.state.files && api_endpoint == "/api/upload_files"){ - this.state.files.forEach((file, _)=>{ - formData.append(file.name, file); - }) - } fetch(addr_prefix + api_endpoint, { method:"POST", body: formData, @@ -151,10 +146,10 @@ class Main extends Component {
{this.state.text}
} - {this.state.page == "#upload_files" && + {this.state.page == "#upload_files" &&
this.onSubmit(e)}> - +
} From 0fbc776526beaf29e179687aef2d645aa070ee1a Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 10:55:13 +0200 Subject: [PATCH 10/12] working maybe files? --- alkatorapi/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index bc7e0f6..c16f94a 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -200,7 +200,7 @@ def photos(request): def upload_files(request): for name, file in request.FILES.items(): print(name) - rand = random.randint(9999999999999) + rand = random.randint(0, 9999999999999) random_name = f'photos/uploads/{rand}.jpg' with open(random_name, "wb+") as destination: for chunk in file.chunks(): From a0d89a611f01b3573486d407b2c68a4e7ea774c0 Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 11:13:59 +0200 Subject: [PATCH 11/12] working uploads --- alkatorapi/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index c16f94a..970f25a 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -200,8 +200,8 @@ def photos(request): def upload_files(request): for name, file in request.FILES.items(): print(name) - rand = random.randint(0, 9999999999999) - random_name = f'photos/uploads/{rand}.jpg' + rand = random.randint(0, 99999) + random_name = f'photos/uploads/{rand}{file.name}' with open(random_name, "wb+") as destination: for chunk in file.chunks(): destination.write(chunk) From 6d36c38dda3f76f01d077a81edfedd5f0ab11efc Mon Sep 17 00:00:00 2001 From: Martin Quarda Date: Mon, 7 Oct 2024 11:14:39 +0200 Subject: [PATCH 12/12] working uploads --- alkatorapi/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alkatorapi/views.py b/alkatorapi/views.py index 970f25a..52ec62b 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -201,7 +201,7 @@ def upload_files(request): for name, file in request.FILES.items(): print(name) rand = random.randint(0, 99999) - random_name = f'photos/uploads/{rand}{file.name}' + random_name = f'photos/uploads/{rand}_{file.name}' with open(random_name, "wb+") as destination: for chunk in file.chunks(): destination.write(chunk)