2023-07-26 10:30:24 +00:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
|
2023-07-14 18:29:07 +00:00
|
|
|
from django.forms import ValidationError
|
2023-07-26 10:30:24 +00:00
|
|
|
from django.http import HttpResponseNotFound
|
2023-06-07 09:53:21 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
2023-06-21 11:30:08 +00:00
|
|
|
from .models import Booksigner
|
2023-06-22 08:27:56 +00:00
|
|
|
from .forms import SignbookForm
|
2023-06-21 11:30:08 +00:00
|
|
|
|
2023-06-07 09:53:21 +00:00
|
|
|
def image_dict_from_name_list(name_list: list[str]):
|
|
|
|
dict_list = []
|
|
|
|
for name in name_list:
|
|
|
|
url = name.split(".")
|
|
|
|
url.pop()
|
|
|
|
url = ".".join(url)
|
|
|
|
dict_list.append({
|
|
|
|
"name": name,
|
|
|
|
"url": url
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return dict_list
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
# Get random ad from ad dir
|
|
|
|
ad_list = os.listdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), "webpage", "static/ads/hor/"))
|
|
|
|
ad = random.choice(ad_list)
|
|
|
|
# Get list of 81x33 icons
|
|
|
|
banner_list = image_dict_from_name_list(os.listdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), "webpage", "static/icons/88x31/")))
|
|
|
|
# Get list of funni gifs
|
2023-06-07 17:55:25 +00:00
|
|
|
gif_list = image_dict_from_name_list(os.listdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), "webpage", "static/icons/gif/")))
|
|
|
|
# Get list of banners
|
|
|
|
banger_list = image_dict_from_name_list(os.listdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), "webpage", "static/icons/banner/")))
|
2023-06-07 09:53:21 +00:00
|
|
|
if not ad or not banner_list or not gif_list:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
else:
|
|
|
|
context = {
|
2023-06-07 17:21:42 +00:00
|
|
|
"title": "THE INDEX",
|
2023-06-07 09:53:21 +00:00
|
|
|
"ad": ad,
|
|
|
|
"banner_list": banner_list,
|
2023-06-07 17:55:25 +00:00
|
|
|
"gif_list": gif_list,
|
|
|
|
"banger_list": banger_list,
|
2023-06-07 09:53:21 +00:00
|
|
|
}
|
2023-07-14 18:03:52 +00:00
|
|
|
return render(request,"index.html", context)
|
2023-06-07 17:21:42 +00:00
|
|
|
|
|
|
|
def about(request):
|
|
|
|
return render(request, "about.html", {"title": "About Me"})
|
2023-06-21 11:30:08 +00:00
|
|
|
|
|
|
|
def signbook(request):
|
|
|
|
signer_list = Booksigner.objects.all()
|
2023-06-22 08:27:56 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = SignbookForm(request.POST)
|
2023-07-14 18:29:07 +00:00
|
|
|
try:
|
|
|
|
if form.is_valid() and form.validate_capcha():
|
|
|
|
newsigner = {}
|
|
|
|
newsigner["name"] = form.cleaned_data['name']
|
|
|
|
newsigner["website"] = form.cleaned_data['website']
|
|
|
|
newsigner["email"] = form.cleaned_data['email']
|
|
|
|
newsigner["comment"] = form.cleaned_data['comment']
|
2023-06-22 08:27:56 +00:00
|
|
|
|
2023-07-14 18:29:07 +00:00
|
|
|
signer_list = Booksigner.objects.all()
|
|
|
|
if newsigner:
|
|
|
|
Booksigner.objects.create(
|
|
|
|
name=newsigner["name"],
|
|
|
|
email=newsigner["email"],
|
|
|
|
website=newsigner["website"],
|
|
|
|
comment=newsigner["comment"],
|
|
|
|
)
|
|
|
|
context = {
|
|
|
|
"title": "THE GUESTBOOK",
|
|
|
|
"signer_list": signer_list
|
|
|
|
}
|
|
|
|
return render(request, "signbook.html", context)
|
2023-06-22 08:27:56 +00:00
|
|
|
context = {
|
|
|
|
"title": "THE GUESTBOOK",
|
2023-07-14 18:29:07 +00:00
|
|
|
"signer_list": signer_list,
|
|
|
|
"error_message": "You didn't input a required input.",
|
2023-06-22 08:27:56 +00:00
|
|
|
}
|
2023-07-14 18:29:07 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"signbook.html",
|
|
|
|
context
|
|
|
|
)
|
|
|
|
except ValidationError as e:
|
|
|
|
print("Error: ", e)
|
|
|
|
context = {
|
|
|
|
"title": "THE GUESTBOOK",
|
|
|
|
"signer_list": signer_list,
|
|
|
|
"error_message": "Invalid captcha.",
|
|
|
|
}
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"signbook.html",
|
|
|
|
context
|
|
|
|
)
|
2023-06-22 08:27:56 +00:00
|
|
|
else:
|
|
|
|
context = {
|
2023-06-21 11:30:08 +00:00
|
|
|
"title": "THE GUESTBOOK",
|
2023-06-22 08:27:56 +00:00
|
|
|
"signer_list": signer_list,
|
|
|
|
"form": SignbookForm()
|
2023-06-21 11:30:08 +00:00
|
|
|
}
|
2023-06-22 08:27:56 +00:00
|
|
|
return render(request, "signbook.html", context)
|