102 lines
4.0 KiB
Python
102 lines
4.0 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from datetime import date, datetime
|
|
import requests
|
|
import json
|
|
import glob
|
|
import PIL.Image
|
|
import random
|
|
|
|
from .models import User, ALKATOR_CHOICES_DICT
|
|
from alkator.settings import COMGATE_MERCHANT, COMGATE_SECRET, COMGATE_TEST
|
|
|
|
|
|
@csrf_exempt
|
|
def register(request):
|
|
ALKATOR_CLASS = 2
|
|
|
|
if date.today() >= date(2024, 10, 5):
|
|
return HttpResponse('{"reason":"Too late!"}', status=400, content_type='application/json')
|
|
if not request.POST.get('agreement'):
|
|
return HttpResponse('{"reason":"Je potřeba souhlasit se zpracováním údajů!"}', status=400, content_type='application/json')
|
|
if not request.POST['first_name']:
|
|
return HttpResponse('{"reason":"Jméno je povinné!"}', status=400, content_type='application/json')
|
|
if not request.POST['last_name']:
|
|
return HttpResponse('{"reason":"Přijmení je povinné!"}', status=400, content_type='application/json')
|
|
if not request.POST['email']:
|
|
return HttpResponse('{"reason":"Email je povinný!"}', status=400, content_type='application/json')
|
|
if not request.POST['address']:
|
|
return HttpResponse('{"reason":"Adresa je povinná!"}', status=400, content_type='application/json')
|
|
if User.objects.filter(email=request.POST['email'], alkator_class=ALKATOR_CLASS):
|
|
return HttpResponse('{"reason":"Email je již registrován!"}', status=400, content_type='application/json')
|
|
try:
|
|
dat = datetime.strptime(request.POST['date_of_birth'], "%Y-%m-%d").date()
|
|
if dat > date(2006, 5, 4):
|
|
return HttpResponse('{"reason":"Je potřeba mít 18 let v den závodu!"}', status=400, content_type='application/json')
|
|
elif dat < date(1924, 5, 4):
|
|
return HttpResponse('{"reason":"Opravdu vám je 100 let?"}', status=400, content_type='application/json')
|
|
except:
|
|
return HttpResponse('{"reason":"Špatný formát datu narození!"}', status=400, content_type='application/json')
|
|
|
|
user = User(
|
|
first_name=request.POST['first_name'],
|
|
last_name=request.POST['last_name'],
|
|
email=request.POST['email'],
|
|
date_of_birth=dat,
|
|
address=request.POST['address'],
|
|
alkator_class=ALKATOR_CLASS,
|
|
)
|
|
user.save()
|
|
|
|
payment_data = {
|
|
'merchant': COMGATE_MERCHANT,
|
|
'test': 'true' if COMGATE_TEST else 'false',
|
|
'price': 69000,
|
|
'curr': 'CZK',
|
|
'method': 'ALL',
|
|
'label': 'Startovné',
|
|
'email': request.POST['email'],
|
|
'fullName': f"{request.POST['first_name']} {request.POST['last_name']}",
|
|
'refId': f'{user.id}',
|
|
'secret': COMGATE_SECRET,
|
|
'prepareOnly': 'true',
|
|
}
|
|
result = requests.post('https://payments.comgate.cz/v1.0/create', data=payment_data)
|
|
|
|
raise Exception(result.text)
|
|
|
|
return HttpResponse('{"success":"Úspěšná registrace."}', content_type='application/json')
|
|
|
|
|
|
def results(request):
|
|
results = []
|
|
n = 1
|
|
for user in User.objects.filter(alkator_class=1).order_by('duration'):
|
|
if user.alkator_category == 1:
|
|
order = f'{n}.'
|
|
n += 1
|
|
else:
|
|
order = 'x.'
|
|
results.append({
|
|
'order': order,
|
|
'duration': str(user.duration),
|
|
'alkator_category': ALKATOR_CHOICES_DICT[user.alkator_category],
|
|
'starting_number': user.starting_number,
|
|
})
|
|
return HttpResponse(json.dumps(results), content_type='application/json')
|
|
|
|
|
|
def photos(request):
|
|
files = glob.glob("photos/*.jpg")
|
|
rtn = []
|
|
random.shuffle(files)
|
|
for file in files:
|
|
img = PIL.Image.open(file)
|
|
rtn.append({
|
|
'original': '/' + file.replace(".jpg", ".webp"),
|
|
'thumbnail': '/' + file.replace('photos/', 'photos/thumbnail/').replace(".jpg", ".webp"),
|
|
'original_width': img.width,
|
|
'original_height': img.height,
|
|
})
|
|
return HttpResponse(json.dumps(rtn), content_type='application/json') |