123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import os
- from PIL import Image
- from django.shortcuts import redirect, render
- from django.template import loader
- from django.core import mail
- 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.utils.translation import gettext
- 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())
- }
- return render(request, 'stammbaum/index.html', context, status=200)
- else:
- return render(request, 'stammbaum/index.html', status=403)
- 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: 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)
- # notify admin
- with mail.get_connection() as connection:
- mail.mail_admins(
- f"{user.get_full_name()} has registered to 'Stammbaum'",
- f"{user.username} ({user.get_full_name()}) <{user.email}> has registered to 'Stammbaum'.\n" +
- f"Please update their permissions accordingly.\n" +
- f"https://pi.justprojects.de/admin/auth/user/{user.id}/change/",
- False,
- connection
- )
- # 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/"
- def get_context_data(self, **kwargs):
- kwargs['image_upload_text'] = gettext("Upload image for {}").format(self.object.name)
- return super().get_context_data(**kwargs)
|