Mercurial > public > sg101
comparison user_photos/views.py @ 971:4f265f61874b
Hotlink image form is functioning.
The user can now submit a URL via a form and the URL will be downloaded
and uploaded to a S3 bucket if it is an image.
Tests to follow.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 22 Sep 2015 20:23:50 -0500 |
parents | 8b027e7a7977 |
children | 7138883966b3 |
comparison
equal
deleted
inserted
replaced
970:bd594bcba5eb | 971:4f265f61874b |
---|---|
3 | 3 |
4 from django.conf import settings | 4 from django.conf import settings |
5 from django.contrib.auth import get_user_model | 5 from django.contrib.auth import get_user_model |
6 from django.contrib.auth.decorators import login_required | 6 from django.contrib.auth.decorators import login_required |
7 from django.http import (HttpResponse, HttpResponseForbidden, | 7 from django.http import (HttpResponse, HttpResponseForbidden, |
8 HttpResponseNotAllowed) | 8 HttpResponseNotAllowed, JsonResponse) |
9 from django.shortcuts import render, redirect, get_object_or_404 | 9 from django.shortcuts import render, redirect, get_object_or_404 |
10 from django.views.generic import ListView | 10 from django.views.generic import ListView |
11 from django.views.decorators.http import require_POST | 11 from django.views.decorators.http import require_POST |
12 from django.utils.decorators import method_decorator | 12 from django.utils.decorators import method_decorator |
13 from django.contrib import messages | 13 from django.contrib import messages |
14 | 14 |
15 from user_photos.forms import UploadForm | 15 from user_photos.forms import HotLinkImageForm, UploadForm |
16 from user_photos.models import Photo | 16 from user_photos.models import Photo |
17 from user_photos.s3 import delete_photos | 17 from user_photos.s3 import delete_photos |
18 | 18 |
19 | 19 |
20 @login_required | 20 @login_required |
138 messages.add_message(request, | 138 messages.add_message(request, |
139 messages.SUCCESS if count > 0 else messages.WARNING, | 139 messages.SUCCESS if count > 0 else messages.WARNING, |
140 msg) | 140 msg) |
141 | 141 |
142 return redirect(ret_view, username) | 142 return redirect(ret_view, username) |
143 | |
144 | |
145 def hotlink_image(request): | |
146 """This view is responsible for accepting an image URL from a user and | |
147 converting it to a URL pointing into our S3 bucket if necessary. | |
148 | |
149 We return a JSON object response to the client with the following name | |
150 & value pairs: | |
151 'error_msg': string error message if an error occurred | |
152 'url': the image URL as a string if success | |
153 """ | |
154 ret = {'error_msg': '', 'url': ''} | |
155 status_code = 400 | |
156 | |
157 if not request.user.is_authenticated(): | |
158 ret['error_msg'] = 'Please login to use this service' | |
159 return JsonResponse(ret, status=status_code) | |
160 if not request.is_ajax() or request.method != 'POST': | |
161 ret['error_msg'] = 'This method is not allowed' | |
162 return JsonResponse(ret, status=status_code) | |
163 | |
164 if settings.USER_PHOTOS_ENABLED: | |
165 form = HotLinkImageForm(request.POST, request.FILES, user=request.user) | |
166 if form.is_valid(): | |
167 try: | |
168 ret['url'] = form.save() | |
169 status_code = 200 | |
170 except Exception as ex: | |
171 ret['error_msg'] = str(ex) | |
172 status_code = 500 | |
173 else: | |
174 # gather form error messages | |
175 errors = [] | |
176 non_field_errors = form.non_field_errors().as_text() | |
177 if non_field_errors: | |
178 errors.append(non_field_errors) | |
179 for field_errors in form.errors.values(): | |
180 errors.append(field_errors.as_text()) | |
181 ret['error_msg'] = '\n'.join(errors) | |
182 else: | |
183 ret['error_msg'] = 'Image linking is temporarily disabled' | |
184 status_code = 403 | |
185 | |
186 return JsonResponse(ret, status=status_code) | |
187 |