new checker for remaining tickets

This commit is contained in:
Martin Quarda 2024-10-13 08:15:52 +02:00
parent d95b003e56
commit 57792b908d
2 changed files with 9 additions and 2 deletions

View File

@ -64,6 +64,9 @@ class Product(models.Model):
price = models.IntegerField() price = models.IntegerField()
quantity = models.IntegerField() quantity = models.IntegerField()
def __str__(self):
f"<Product {self.name} {self.price} Kč zbývá {self.quantity}>"
class Cart(models.Model): class Cart(models.Model):
user = models.ForeignKey(DjangoUser, on_delete=models.RESTRICT) user = models.ForeignKey(DjangoUser, on_delete=models.RESTRICT)

View File

@ -93,8 +93,6 @@ def register_racer(request):
return HttpResponse('{"reason":"Jméno je povinné!"}', status=400, content_type='application/json') return HttpResponse('{"reason":"Jméno je povinné!"}', status=400, content_type='application/json')
if not request.POST['last_name']: if not request.POST['last_name']:
return HttpResponse('{"reason":"Přijmení je povinné!"}', status=400, content_type='application/json') return HttpResponse('{"reason":"Přijmení je povinné!"}', status=400, content_type='application/json')
if Racer.objects.filter(alkator_class=ALKATOR_CLASS, paid=True).count() >= 100:
return HttpResponse('{"reason":"Kapacita závodu byla naplněna!"}', status=400, content_type='application/json')
try: try:
dob = datetime.strptime(request.POST['date_of_birth'], "%Y-%m-%d").date() dob = datetime.strptime(request.POST['date_of_birth'], "%Y-%m-%d").date()
if dob > date(2006, 10, 5): if dob > date(2006, 10, 5):
@ -122,6 +120,12 @@ def register_racer(request):
product = Product.objects.get(id=1) product = Product.objects.get(id=1)
price = product.price price = product.price
if product.quantity <= 0:
return HttpResponse('{"reason":"Jsme vyprodaní!"}', status=400, content_type='application/json')
product.quantity -= 1
product.save()
racer = Racer( racer = Racer(
product=product, product=product,
quantity=1, quantity=1,