views.py 840 B

123456789101112131415161718192021222324252627
  1. from django.contrib.auth.decorators import login_required
  2. from django.core.exceptions import PermissionDenied
  3. from django.shortcuts import render, get_object_or_404
  4. from .models import User, Corporation
  5. @login_required
  6. def user(request):
  7. # request.user is concrete model whereas user is proxy model
  8. user = User.objects.get(pk=request.user.pk)
  9. return render(request, 'adhesions/user.html', {
  10. 'adherent': user,
  11. 'adhesion': user.adhesion,
  12. })
  13. @login_required
  14. def corporation(request, pk):
  15. corporation = get_object_or_404(Corporation, pk=pk)
  16. try:
  17. corporation.members.get(pk=request.user.pk)
  18. except User.DoesNotExist:
  19. raise PermissionDenied
  20. return render(request, 'adhesions/corporation.html', {
  21. 'adherent': corporation,
  22. 'adhesion': corporation.adhesion,
  23. })