JS-P04/app/views.py
2020-06-08 23:54:15 +02:00

129 lines
3.8 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect, reverse
from .models import Album, Photo
from .forms import PhotoForm, AlbumForm
from django.contrib.auth.forms import UserCreationForm as RegisterForm
from django.contrib.auth import login
from django.core.exceptions import PermissionDenied
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from app.forms import PhotoEditForm
def register(request):
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
login(request, form.instance)
return redirect(reverse("index"))
else:
form = RegisterForm()
return render(request, "registration/register.html", {"form":form})
# Create your views here.
def albums(request):
albums = Album.objects.order_by('created_at').prefetch_related('photo_set')
return render(request, "album/index.html", locals())
def photos(request, album_id):
album = get_object_or_404(Album, pk=album_id)
photos = album.photo_set.all()
is_my = album.is_owned_by(request.user)
return render(request, "album/photos.html", locals())
def photo(request, photo_id, album_id):
photo = get_object_or_404(Photo, pk=photo_id)
is_my = photo.is_owned_by(request.user)
return render(request, "album/photo.html", locals())
@login_required
def add_album(request):
if request.method == 'POST':
form = AlbumForm(request.POST, request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()
return redirect(reverse('album', args=[form.instance.id]))
else:
form = AlbumForm()
return render(request, "album/add.html", locals())
def add_photo(request, album_id):
album = get_object_or_404(Album, pk=album_id)
if not album.is_owned_by(request.user):
raise PermissionDenied()
if request.method == 'POST':
form = PhotoForm(request.POST, request.FILES)
photo = form.instance
photo.album = album
photo.user = request.user
if form.is_valid():
form.save()
return redirect(reverse('album', args=[album.id]))
else:
form = PhotoForm()
return render(request, "album/add_photo.html", locals())
def edit_album(request, album_id):
album = get_object_or_404(Album, pk=album_id)
if not album.is_owned_by(request.user):
raise PermissionDenied()
form = AlbumForm(request.POST or None, instance=album)
if request.method == 'POST' and form.is_valid():
form.save()
return redirect(reverse('album', args=[album.id]))
return render(request, "album/edit_album.html", locals())
@require_http_methods(["POST"])
def delete_album(request, album_id):
album = get_object_or_404(Album, pk=album_id)
if not album.is_owned_by(request.user):
raise PermissionDenied()
album.delete()
return redirect(reverse('index'))
def edit_photo(request, album_id, photo_id):
photo = get_object_or_404(Photo, pk=photo_id)
if not photo.is_owned_by(request.user):
raise PermissionDenied()
form = PhotoEditForm(request.POST or None, request.FILES or None, instance=photo)
if request.method == 'POST' and form.is_valid():
form.save()
return redirect(reverse('photo', args=[photo.album.id, photo.id]))
return render(request, "album/edit_photo.html", locals())
@require_http_methods(["POST"])
def delete_photo(request, album_id, photo_id):
photo = get_object_or_404(Photo, pk=photo_id)
if not photo.is_owned_by(request.user):
raise PermissionDenied()
photo.delete()
return redirect(reverse('album', args=[album_id]))