149 lines
5.8 KiB
Python
149 lines
5.8 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
|
|
from urllib.parse import parse_qs
|
|
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 not request.POST['phone']:
|
|
return HttpResponse('{"reason":"Telefoní číslo 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, 10, 5):
|
|
return HttpResponse('{"reason":"Je potřeba mít 18 let v den závodu!"}', status=400, content_type='application/json')
|
|
elif dat < date(1924, 10, 5):
|
|
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')
|
|
|
|
invoice_date = datetime.today()
|
|
invoice_id = invoice_date.year * 1000000 + invoice_date.month * 10000 + invoice_date.day * 100
|
|
|
|
latest_user = User.objects.latest("invoice_id")
|
|
if latest_user.invoice_id < invoice_id:
|
|
invoice_id = invoice_id + 1
|
|
else:
|
|
invoice_id = latest_user.invoice_id + 1
|
|
|
|
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'],
|
|
phone=request.POST['phone'],
|
|
alkator_class=ALKATOR_CLASS,
|
|
invoice_id=invoice_id
|
|
)
|
|
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.invoice_id}',
|
|
'secret': COMGATE_SECRET,
|
|
'prepareOnly': 'true',
|
|
}
|
|
result = requests.post('https://payments.comgate.cz/v1.0/create', data=payment_data)
|
|
|
|
result = parse_qs(result.text)
|
|
|
|
if result['code'][0] != '0':
|
|
user.delete()
|
|
return HttpResponse('{"reason":"Chyba na straně platevní brány: ' + result['message'][0] + '"}', status=400, content_type='application/json')
|
|
|
|
user.trans_id = result['transId'][0]
|
|
user.save()
|
|
|
|
return HttpResponse('{"success":"Úspěšná registrace.", "redirect":"' + result['redirect'][0] + '"}', content_type='application/json')
|
|
|
|
|
|
@csrf_exempt
|
|
def payment_result(request):
|
|
result = parse_qs(request.body.decode('utf8'))
|
|
ref_id = int(result['refId'][0])
|
|
paid = result['status'][0]
|
|
secret_match = result['secret'][0] == COMGATE_SECRET
|
|
test = result['test'][0] != 'false'
|
|
if not secret_match or test != COMGATE_TEST:
|
|
return HttpResponse(status=400)
|
|
user = User.objects.get(invoice_id=ref_id)
|
|
if paid == 'PAID':
|
|
user.paid = True
|
|
user.save()
|
|
elif paid == 'CANCELLED':
|
|
user.delete()
|
|
return HttpResponse(status=200)
|
|
|
|
|
|
def payment_state(request):
|
|
invoice_id = request.GET['refId']
|
|
if User.objects.get(invoice_id=invoice_id).paid:
|
|
return HttpResponse('{"status":"success", "reason":"Úspěšná platba"}', content_type='application/json')
|
|
else:
|
|
return HttpResponse('{"status":"failed", "reason":"Zatím nemáme informace o provedené platbě. Zkuste reload nebo zkontrolujte email."}', 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') |