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.
98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
from django.shortcuts import render, get_object_or_404, HttpResponse, HttpResponseRedirect
|
|
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
|
|
|
|
|
|
def index(request):
|
|
""" Main page of call, with 2bitcharactergenerator, receives POST to save a new creature """
|
|
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.
|
|
return HttpResponse("ok")
|
|
else:
|
|
print ("FORM NOT VALID")
|
|
else:
|
|
form = CreatureForm()
|
|
return render(request, "call/index.html", {"form": form})
|
|
|
|
class Guestbook(generic.ListView):
|
|
""" public guestbook page """
|
|
paginate_by = 20
|
|
model=Creature
|
|
template_name = "call/guestbook.html"
|
|
queryset = Creature.objects.filter(published=True)
|
|
|
|
class CreatureList(generic.ListView):
|
|
""" public guestbook page """
|
|
paginate_by = 20
|
|
model=Creature
|
|
template_name = "call/creature_list.html"
|
|
queryset = Creature.objects.filter(published=True)
|
|
|
|
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"))
|
|
context['scale'] = int(request.GET.get("scale", "12"))
|
|
context['width'] = int(request.GET.get("width", "500"))
|
|
context['height'] = int(request.GET.get("height", "500"))
|
|
seed(creature_id)
|
|
|
|
context['bg_hue'] = randint(0, 360)
|
|
context['bg_sat'] = 100
|
|
context['bg_lightness'] = randint(50, 100)
|
|
|
|
# context['bg_hue'] = 0 # randint(0, 360)
|
|
# context['bg_sat'] = 0 # 100
|
|
# context['bg_lightness'] = 66 # randint(50, 100)
|
|
|
|
return render(request, "call/creature_detail.html", context)
|
|
|
|
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 = int(request.GET.get("scale", "6")) # 24
|
|
# limit from 1-100
|
|
scale = max(1, min(100, scale))
|
|
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
|
|
|
|
def call_block(request):
|
|
""" Call text for animation """
|
|
context = {}
|
|
context['width'] = int(request.GET.get("width", "432"))
|
|
context['height'] = int(request.GET.get("height", "768"))
|
|
|
|
# context['bg_hue'] = randint(0, 360)
|
|
# context['bg_sat'] = 100
|
|
# context['bg_lightness'] = randint(50, 100)
|
|
|
|
context['bg_hue'] = 0 # randint(0, 360)
|
|
context['bg_sat'] = 0 # 100
|
|
context['bg_lightness'] = 66 # randint(50, 100)
|
|
|
|
return render(request, "call/call_block.html", context)
|