Mercurial > public > sg101
comparison user_photos/views.py @ 722:71d17d267e27
Added an ajax form to upload photos & update post box w/image code.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 21 Sep 2013 17:00:49 -0500 |
parents | bf5340705d0c |
children | c0199d3d3b86 |
comparison
equal
deleted
inserted
replaced
721:378b55b81de3 | 722:71d17d267e27 |
---|---|
1 """Views for the user_photos application.""" | 1 """Views for the user_photos application.""" |
2 import json | |
3 | |
2 from django.conf import settings | 4 from django.conf import settings |
3 from django.contrib.auth import get_user_model | 5 from django.contrib.auth import get_user_model |
4 from django.contrib.auth.decorators import login_required | 6 from django.contrib.auth.decorators import login_required |
7 from django.http import (HttpResponse, HttpResponseForbidden, | |
8 HttpResponseNotAllowed) | |
5 from django.shortcuts import render, redirect, get_object_or_404 | 9 from django.shortcuts import render, redirect, get_object_or_404 |
6 from django.views.generic import ListView | 10 from django.views.generic import ListView |
7 from django.views.decorators.http import require_POST | 11 from django.views.decorators.http import require_POST |
8 from django.utils.decorators import method_decorator | 12 from django.utils.decorators import method_decorator |
9 from django.contrib import messages | 13 from django.contrib import messages |
17 def upload(request): | 21 def upload(request): |
18 """This view function receives an uploaded image file from a user. | 22 """This view function receives an uploaded image file from a user. |
19 The photo will be resized if necessary and a thumbnail image will be | 23 The photo will be resized if necessary and a thumbnail image will be |
20 created. The image and thumbnail will then be uploaded to the Amazon | 24 created. The image and thumbnail will then be uploaded to the Amazon |
21 S3 service for storage. | 25 S3 service for storage. |
22 | |
23 TODO: rate limiting | |
24 pass off the processing to a celery task | |
25 ajax version of this view | |
26 | 26 |
27 """ | 27 """ |
28 form = None | 28 form = None |
29 uploads_enabled = settings.USER_PHOTOS_ENABLED | 29 uploads_enabled = settings.USER_PHOTOS_ENABLED |
30 | 30 |
40 return render(request, 'user_photos/upload_form.html', { | 40 return render(request, 'user_photos/upload_form.html', { |
41 'enabled': uploads_enabled, | 41 'enabled': uploads_enabled, |
42 'form': form, | 42 'form': form, |
43 }, | 43 }, |
44 status=200 if uploads_enabled else 503) | 44 status=200 if uploads_enabled else 503) |
45 | |
46 | |
47 def upload_ajax(request): | |
48 """This view is the ajax version of the upload function, above. | |
49 | |
50 We return a JSON object response to the client with the following name | |
51 & value pairs: | |
52 'success' : false for failure and true for success | |
53 'msg' : string error message if success is false | |
54 'url' : the image URL as a string if success | |
55 | |
56 If a non-200 status code is returned the response will simply be a text | |
57 string error message. | |
58 | |
59 """ | |
60 if not request.user.is_authenticated(): | |
61 return HttpResponseForbidden | |
62 if not request.is_ajax() or request.method != 'POST': | |
63 return HttpResponseNotAllowed | |
64 | |
65 ret = {'success': False, 'msg': '', 'url': ''} | |
66 if settings.USER_PHOTOS_ENABLED: | |
67 form = UploadForm(request.POST, request.FILES, user=request.user) | |
68 if form.is_valid(): | |
69 photo = form.save() | |
70 ret['success'] = True | |
71 ret['url'] = photo.url | |
72 else: | |
73 # gather form error messages | |
74 errors = [] | |
75 non_field_errors = form.non_field_errors().as_text() | |
76 if non_field_errors: | |
77 errors.append(non_field_errors) | |
78 for field_errors in form.errors.values(): | |
79 errors.append(field_errors.as_text()) | |
80 ret['msg'] = '\n'.join(errors) | |
81 else: | |
82 ret['msg'] = 'Photo uploads are temporarily disabled' | |
83 | |
84 return HttpResponse(json.dumps(ret), content_type='application/json') | |
45 | 85 |
46 | 86 |
47 class GalleryView(ListView): | 87 class GalleryView(ListView): |
48 """A ListView for displaying a user's photos""" | 88 """A ListView for displaying a user's photos""" |
49 | 89 |