bgneal@695
|
1 """Views for the user_photos application."""
|
bgneal@695
|
2 from django.conf import settings
|
bgneal@704
|
3 from django.contrib.auth import get_user_model
|
bgneal@695
|
4 from django.contrib.auth.decorators import login_required
|
bgneal@704
|
5 from django.shortcuts import render, redirect, get_object_or_404
|
bgneal@704
|
6 from django.views.generic import ListView
|
bgneal@710
|
7 from django.views.decorators.http import require_POST
|
bgneal@704
|
8 from django.utils.decorators import method_decorator
|
bgneal@710
|
9 from django.contrib import messages
|
bgneal@695
|
10
|
bgneal@695
|
11 from user_photos.forms import UploadForm
|
bgneal@704
|
12 from user_photos.models import Photo
|
bgneal@718
|
13 from user_photos.s3 import delete_photos
|
bgneal@695
|
14
|
bgneal@695
|
15
|
bgneal@695
|
16 @login_required
|
bgneal@695
|
17 def upload(request):
|
bgneal@695
|
18 """This view function receives an uploaded image file from a user.
|
bgneal@695
|
19 The photo will be resized if necessary and a thumbnail image will be
|
bgneal@695
|
20 created. The image and thumbnail will then be uploaded to the Amazon
|
bgneal@695
|
21 S3 service for storage.
|
bgneal@695
|
22
|
bgneal@695
|
23 TODO: rate limiting
|
bgneal@695
|
24 pass off the processing to a celery task
|
bgneal@695
|
25 ajax version of this view
|
bgneal@695
|
26
|
bgneal@695
|
27 """
|
bgneal@695
|
28 form = None
|
bgneal@696
|
29 uploads_enabled = settings.USER_PHOTOS_ENABLED
|
bgneal@695
|
30
|
bgneal@695
|
31 if uploads_enabled:
|
bgneal@695
|
32 if request.method == 'POST':
|
bgneal@696
|
33 form = UploadForm(request.POST, request.FILES, user=request.user)
|
bgneal@695
|
34 if form.is_valid():
|
bgneal@696
|
35 photo = form.save()
|
bgneal@696
|
36 return redirect(photo)
|
bgneal@695
|
37 else:
|
bgneal@696
|
38 form = UploadForm(user=request.user)
|
bgneal@695
|
39
|
bgneal@695
|
40 return render(request, 'user_photos/upload_form.html', {
|
bgneal@695
|
41 'enabled': uploads_enabled,
|
bgneal@695
|
42 'form': form,
|
bgneal@695
|
43 },
|
bgneal@695
|
44 status=200 if uploads_enabled else 503)
|
bgneal@704
|
45
|
bgneal@704
|
46
|
bgneal@704
|
47 class GalleryView(ListView):
|
bgneal@704
|
48 """A ListView for displaying a user's photos"""
|
bgneal@704
|
49
|
bgneal@704
|
50 template_name = 'user_photos/gallery.html'
|
bgneal@704
|
51 context_object_name = 'photos'
|
bgneal@704
|
52 paginate_by = 50
|
bgneal@704
|
53 allow_empty = True
|
bgneal@704
|
54
|
bgneal@704
|
55 def get_queryset(self):
|
bgneal@704
|
56 self.gallery_owner = get_object_or_404(get_user_model(),
|
bgneal@704
|
57 username=self.kwargs['username'])
|
bgneal@704
|
58 return Photo.objects.filter(user=self.gallery_owner).order_by('-upload_date')
|
bgneal@704
|
59
|
bgneal@704
|
60 def get_context_data(self, **kwargs):
|
bgneal@704
|
61 context = super(GalleryView, self).get_context_data(**kwargs)
|
bgneal@704
|
62 context['gallery_owner'] = self.gallery_owner
|
bgneal@704
|
63 return context
|
bgneal@704
|
64
|
bgneal@704
|
65 @method_decorator(login_required)
|
bgneal@704
|
66 def dispatch(self, *args, **kwargs):
|
bgneal@704
|
67 return super(GalleryView, self).dispatch(*args, **kwargs)
|
bgneal@710
|
68
|
bgneal@710
|
69
|
bgneal@710
|
70 @login_required
|
bgneal@710
|
71 @require_POST
|
bgneal@710
|
72 def delete(request):
|
bgneal@718
|
73 """A view function to allow a user to delete their own photos."""
|
bgneal@718
|
74
|
bgneal@718
|
75 ret_view, username = 'user_photos-gallery', request.user.username
|
bgneal@718
|
76
|
bgneal@718
|
77 if not settings.USER_PHOTOS_ENABLED:
|
bgneal@718
|
78 messages.error(request, "This function is disabled temporarily")
|
bgneal@718
|
79 return redirect(ret_view, username)
|
bgneal@718
|
80
|
bgneal@710
|
81 photo_ids = []
|
bgneal@710
|
82 for photo_id in request.POST.getlist('photo_id'):
|
bgneal@710
|
83 try:
|
bgneal@710
|
84 n = int(photo_id)
|
bgneal@710
|
85 except ValueError:
|
bgneal@710
|
86 continue
|
bgneal@710
|
87 photo_ids.append(n)
|
bgneal@710
|
88
|
bgneal@710
|
89 count = 0
|
bgneal@710
|
90 if photo_ids:
|
bgneal@710
|
91 qs = Photo.objects.filter(user=request.user, pk__in=photo_ids)
|
bgneal@718
|
92 count = len(qs)
|
bgneal@718
|
93 if count:
|
bgneal@718
|
94 delete_photos(qs)
|
bgneal@718
|
95 qs.delete()
|
bgneal@710
|
96
|
bgneal@718
|
97 msg = "{} photo{} deleted".format(count, '' if count == 1 else 's')
|
bgneal@718
|
98 messages.add_message(request,
|
bgneal@718
|
99 messages.SUCCESS if count > 0 else messages.WARNING,
|
bgneal@718
|
100 msg)
|
bgneal@710
|
101
|
bgneal@718
|
102 return redirect(ret_view, username)
|