1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import os
- from PIL import Image
- from django.shortcuts import redirect, render
- from django.template import loader
- from django.http import HttpResponse, FileResponse
- from django.urls import reverse_lazy
- from django.views.generic import UpdateView
- from django.views.decorators.cache import cache_control
- from django.contrib import auth
- from django.contrib.auth.decorators import permission_required
- from django.contrib.auth.mixins import PermissionRequiredMixin
- from .models import *
- from .forms import *
- from website.settings import BASE_DIR
- def index(request, group_id=None):
- if request.user.has_perm('stammbaum.view'):
- if group_id == None:
- persons = Person.objects
- else:
- persons = Person.objects.filter(group_id=group_id)
- partners = Person.partners.through.objects
- context = {
- 'persons': list(persons.values_list()),
- 'pairs': list(partners.values_list()),
- 'groups': list(Group.objects.values())
- }
- else:
- context = {}
- template = loader.get_template('stammbaum/index.html')
- return HttpResponse(template.render(context, request))
- class Login(auth.views.LoginView):
- template_name = 'stammbaum/login.html'
- def register(request):
- if request.method == 'POST':
- form = SignUpForm(request.POST)
- if form.is_valid():
- user = form.save()
- user.refresh_from_db()
- # load the profile instance created by the signal
- user.save()
- raw_password = form.cleaned_data.get('password1')
- # login user after signing up
- user = auth.authenticate(username=user.username, password=raw_password)
- auth.login(request, user)
- # redirect user to home page
- return redirect('/stammbaum/')
- else:
- form = SignUpForm()
- return render(request, 'stammbaum/register.html', context={"form": SignUpForm})
- def logout(request):
- auth.logout(request)
- template = loader.get_template('stammbaum/index.html')
- return HttpResponse(template.render(None, request))
- @permission_required("stammbaum.view")
- @cache_control(max_age=3600)
- def image_preview(request, person_id):
- person = Person.objects.filter(id=person_id).first()
- if person and person.image:
- img = Image.open(os.path.join(BASE_DIR, '../data', str(person.image)))
- else:
- img = Image.open(os.path.join(BASE_DIR, '../data', "default.jpeg"))
- img.thumbnail((256, 256))
- res = HttpResponse(content_type="image/jpeg")
- img.save(res, format='JPEG')
- return res
- @permission_required("stammbaum.view")
- @cache_control(max_age=3600)
- def image(request, person_id):
- person = Person.objects.filter(id=person_id).first()
- if person and person.image:
- img = open(os.path.join(BASE_DIR, '../data', str(person.image)), 'rb')
- else:
- img = open(os.path.join(BASE_DIR, '../data', "default.jpeg"), 'rb')
- return FileResponse(img)
- class UploadImageView(PermissionRequiredMixin, UpdateView):
- model = Person
- form_class = UploadImage
- permission_required = ("stammbaum.view", "stammbaum.upload_image")
- template_name = "stammbaum/upload.html"
- success_url = "/stammbaum/"
|