website/personalWebpage/webpage/views.py

53 lines
1.8 KiB
Python

from django.http import HttpResponseNotFound
from django.shortcuts import render
import os, random
from .models import Booksigner
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
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/")))
if not ad or not banner_list or not gif_list:
return HttpResponseNotFound()
else:
context = {
"title": "THE INDEX",
"ad": ad,
"banner_list": banner_list,
"gif_list": gif_list,
"banger_list": banger_list,
}
return render(request,
"index.html", context)
def about(request):
return render(request, "about.html", {"title": "About Me"})
def signbook(request):
signer_list = Booksigner.objects.all()
context = {
"title": "THE GUESTBOOK",
"signer_list": signer_list
}
return render(request, "signbook.html", context)