You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.5 KiB
Python

from django.shortcuts import render, get_object_or_404, HttpResponse, HttpResponseRedirect
1 week ago
from call.forms import CreatureForm
from django.views import generic
from .models import Creature
from django.urls import reverse
# from io import StringIO
from PIL import Image
from random import randint, seed
1 week ago
def index(request):
""" Main page of call, with 2bitcharactergenerator, receives POST to save a new creature """
1 week ago
if request.method == "POST":
print ("post", request.POST)
print ("files", request.FILES)
form = CreatureForm(request.POST, request.FILES)
# form = CreatureFormNo(request.POST, request.FILES)
# formset = AuthorFormSet(request.POST, request.FILES)
if form.is_valid():
print ("FORM IS VALID")
form.save()
return HttpResponseRedirect(reverse("statement", args=(form.instance.id,)))
# do something.
else:
print ("FORM NOT VALID")
else:
form = CreatureForm()
return render(request, "call/index.html", {"form": form})
class Guestbook(generic.ListView):
""" public guestbook page """
1 week ago
paginate_by = 20
model=Creature
template_name = "call/guestbook.html"
queryset = Creature.objects.filter(published=True)
1 week ago
class CreatureList(generic.ListView):
""" public guestbook page """
paginate_by = 20
model=Creature
template_name = "call/creature_list.html"
queryset = Creature.objects.filter(published=True)
1 week ago
def creature_detail(request, creature_id):
""" Generates Pixelated PNG of creature """
context = {}
context['creature'] = get_object_or_404(Creature, pk=creature_id)
context['cell'] = int(request.GET.get("cell", "0"))
seed(creature_id)
context['bg_hue'] = randint(0, 360)
context['bg_sat'] = 100
context['bg_lightness'] = randint(50, 100)
return render(request, "call/creature_detail.html", context)
1 week ago
def creature_frame (request, creature_id):
""" Generates Pixelated PNG of creature """
cell = int(request.GET.get("cell", "0"))
creature = get_object_or_404(Creature, pk=creature_id)
im = Image.open(creature.image.path)
response = HttpResponse(content_type="image/png")
scale = 6 # 24
im = im.crop((16*cell, 0, (16*cell)+16, 16))
w, h = im.size
im = im.resize((w*scale,h*scale), resample=Image.NEAREST)
im.save(response, "PNG")
#im_with_background = Image.new("RGBA", (w*scale, h*scale), (128,0,0))
#im_with_background.paste(im, mask=im)
#im_with_background.save(response, "PNG")
return response