diff user_photos/views.py @ 1096:d9cd3180c12c

More GCalendar V3 conversion in progress. Built a brand new post editor. It is hardcoded into GCalendar right now. We will make it more general in the future.
author Brian Neal <bgneal@gmail.com>
date Tue, 14 Jun 2016 21:16:09 -0500
parents 7138883966b3
children 364f8ec48612
line wrap: on
line diff
--- a/user_photos/views.py	Mon May 30 21:09:26 2016 -0500
+++ b/user_photos/views.py	Tue Jun 14 21:16:09 2016 -0500
@@ -84,6 +84,50 @@
     return HttpResponse(json.dumps(ret), content_type='application/json')
 
 
+def upload_ajax_v3(request):
+    """This view is the V3 ajax version of the upload function, above.
+
+    We return a JSON object response to the client with the following name
+    & value pairs:
+            'msg' : string error message if success is false
+            'url' : the image URL as a string if success
+    """
+    ret = {'error_msg': '', 'url': ''}
+    status_code = 400
+
+    if not request.user.is_authenticated():
+        ret['error_msg'] = 'Please login to use this service'
+        return JsonResponse(ret, status=403)
+    if not request.is_ajax() or request.method != 'POST':
+        ret['error_msg'] = 'This method is not allowed'
+        return JsonResponse(ret, status=405)
+
+    if settings.USER_PHOTOS_ENABLED:
+        form = UploadForm(request.POST, request.FILES, user=request.user)
+        if form.is_valid():
+            try:
+                photo = form.save()
+                ret['url'] = photo.url
+                status_code = 200
+            except Exception as ex:
+                ret['error_msg'] = str(ex)
+                status_code = 500
+        else:
+            # gather form error messages
+            errors = []
+            non_field_errors = form.non_field_errors().as_text()
+            if non_field_errors:
+                errors.append(non_field_errors)
+            for field_errors in form.errors.values():
+                errors.append(field_errors.as_text())
+            ret['msg'] = '\n'.join(errors)
+    else:
+        ret['msg'] = 'Photo uploads are temporarily disabled'
+        status_code = 403
+
+    return JsonResponse(ret, status=status_code)
+
+
 class GalleryView(ListView):
     """A ListView for displaying a user's photos"""