torreta-del-drac-server/server/views.py
2025-04-23 16:00:09 +02:00

45 lines
1.4 KiB
Python

import json
import random
import profanity.templatetags.profanity
from django.db import IntegrityError
from django.http import HttpResponse, Http404
from server.models import Dragon
def add(request, origin, name, color, shirt, hat, decor):
origin = origin.strip()
name = name.strip()
color = int(color.strip())
shirt = int(shirt.strip())
hat = int(hat.strip())
decor = int(decor.strip())
if profanity.templatetags.profanity.is_profane(name):
print("profanity detected")
return HttpResponse("profanity detected")
if profanity.templatetags.profanity.is_profane(origin):
print("profanity detected")
return HttpResponse("profanity detected")
try:
dragon = Dragon.objects.create(origin=origin, name=name, color=color, shirt=shirt, hat=hat, decor=decor)
print(f'Received dragon ({dragon})')
except IntegrityError:
print('Dragon already exists')
return HttpResponse('')
def get(request, origin):
items = list(Dragon.objects.all().exclude(origin=origin))
if len(items) == 0:
print('hey')
raise Http404
random_item: Dragon = random.choice(items)
response_data = {'origin': random_item.origin, 'name': random_item.name, 'color': random_item.color,
'shirt': random_item.shirt, 'hat': random_item.hat, 'decor': random_item.decor}
return HttpResponse(json.dumps(response_data), content_type="application/json")