views.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. from PIL import Image
  3. from django.shortcuts import redirect, render
  4. from django.template import loader
  5. from django.core import mail
  6. from django.http import HttpResponse, FileResponse
  7. from django.urls import reverse_lazy
  8. from django.views.generic import UpdateView
  9. from django.views.decorators.cache import cache_control
  10. from django.utils.translation import gettext
  11. from django.contrib import auth
  12. from django.contrib.auth.decorators import permission_required
  13. from django.contrib.auth.mixins import PermissionRequiredMixin
  14. from .models import *
  15. from .forms import *
  16. from website.settings import BASE_DIR
  17. def index(request, group_id=None):
  18. if request.user.has_perm('stammbaum.view'):
  19. if group_id == None:
  20. persons = Person.objects
  21. else:
  22. persons = Person.objects.filter(group_id=group_id)
  23. partners = Person.partners.through.objects
  24. context = {
  25. 'persons': list(persons.values_list()),
  26. 'pairs': list(partners.values_list()),
  27. 'groups': list(Group.objects.values())
  28. }
  29. return render(request, 'stammbaum/index.html', context, status=200)
  30. else:
  31. return render(request, 'stammbaum/index.html', status=403)
  32. class Login(auth.views.LoginView):
  33. template_name = 'stammbaum/login.html'
  34. def register(request):
  35. if request.method == 'POST':
  36. form = SignUpForm(request.POST)
  37. if form.is_valid():
  38. user: User = form.save()
  39. user.refresh_from_db()
  40. # load the profile instance created by the signal
  41. user.save()
  42. raw_password = form.cleaned_data.get('password1')
  43. # login user after signing up
  44. user = auth.authenticate(username=user.username, password=raw_password)
  45. auth.login(request, user)
  46. # notify admin
  47. with mail.get_connection() as connection:
  48. mail.mail_admins(
  49. f"{user.get_full_name()} has registered to 'Stammbaum'",
  50. f"{user.username} ({user.get_full_name()}) <{user.email}> has registered to 'Stammbaum'.\n" +
  51. f"Please update their permissions accordingly.\n" +
  52. f"https://pi.justprojects.de/admin/auth/user/{user.id}/change/",
  53. False,
  54. connection
  55. )
  56. # redirect user to home page
  57. return redirect('/stammbaum/')
  58. else:
  59. form = SignUpForm()
  60. return render(request, 'stammbaum/register.html', context={"form": SignUpForm})
  61. def logout(request):
  62. auth.logout(request)
  63. template = loader.get_template('stammbaum/index.html')
  64. return HttpResponse(template.render(None, request))
  65. @permission_required("stammbaum.view")
  66. @cache_control(max_age=3600)
  67. def image_preview(request, person_id):
  68. person = Person.objects.filter(id=person_id).first()
  69. if person and person.image:
  70. img = Image.open(os.path.join(BASE_DIR, '../data', str(person.image)))
  71. else:
  72. img = Image.open(os.path.join(BASE_DIR, '../data', "default.jpeg"))
  73. img.thumbnail((256, 256))
  74. res = HttpResponse(content_type="image/jpeg")
  75. img.save(res, format='JPEG')
  76. return res
  77. @permission_required("stammbaum.view")
  78. @cache_control(max_age=3600)
  79. def image(request, person_id):
  80. person = Person.objects.filter(id=person_id).first()
  81. if person and person.image:
  82. img = open(os.path.join(BASE_DIR, '../data', str(person.image)), 'rb')
  83. else:
  84. img = open(os.path.join(BASE_DIR, '../data', "default.jpeg"), 'rb')
  85. return FileResponse(img)
  86. class UploadImageView(PermissionRequiredMixin, UpdateView):
  87. model = Person
  88. form_class = UploadImage
  89. permission_required = ("stammbaum.view", "stammbaum.upload_image")
  90. template_name = "stammbaum/upload.html"
  91. success_url = "/stammbaum/"
  92. def get_context_data(self, **kwargs):
  93. kwargs['image_upload_text'] = gettext("Upload image for {}").format(self.object.name)
  94. return super().get_context_data(**kwargs)