Commits

b7w committed 8751958

Add exists method to base controller and refactor code

  • Participants
  • Parent commits 31f839c

Comments (0)

Files changed (7)

File bviewer/archive/views.py

     if not holder:
         return message_view(request, message=NO_USER_DEFINED)
 
-    controller = GalleryController(holder, request.user, gid)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=gid)
+    if not controller.exists():
         return message_view(request, message=NO_GALLERY_FOUND)
 
     if not controller.is_album():
     # links for redirect to download, and check status
     redirect = reverse('archive.download', kwargs=dict(gid=gid, uid=z.uid))
     link = reverse('archive.status', kwargs=dict(gid=gid, uid=z.uid))
+    main = controller.get_object()
 
     if z.status == 'DONE':
         return HttpResponseRedirect(redirect)
         raise Http404(NO_USER_DEFINED)
 
     controller = GalleryController(holder, request.user, gid)
-    main = controller.get_object()
-    if not main:
+    if not controller.exists():
         return HttpResponse(json.dumps(dict(error=NO_GALLERY_FOUND)))
 
     if not controller.is_album():
     if not holder:
         raise Http404(NO_USER_DEFINED)
 
-    controller = GalleryController(holder, request.user, gid)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=gid)
+    if not controller.exists():
         raise Http404(NO_GALLERY_FOUND)
 
     if not controller.is_album():
     if z == 'NONE':
         raise Http404('No file found')
 
+    main = controller.get_object()
     logger.info(smart_text('download archive "%s"'), main.title)
     name = smart_text('{0} - {1}.zip').format(main.time.strftime('%Y-%m-%d'), main.title)
     return download_response(z.archive, name=name)

File bviewer/core/controllers.py

         """
         return self.holder == self.user
 
+    def exists(self):
+        return bool(self.get_object())
+
     @staticmethod
     def from_obj(obj):
         raise NotImplementedError()

File bviewer/core/tests/test_controllers.py

 
         g121 = Gallery.objects.create(parent=g12, user=user, title='1.2.1')
 
-        controller = GalleryController(user, user, top_gallery.id)
+        controller = GalleryController(user, user, uid=top_gallery.id)
         galleries = controller.get_all_sub_galleries()
         self.assertEqual(len(galleries), 10)
 
-        controller = GalleryController(user, user, top_gallery.id)
+        controller = GalleryController(user, user, uid=top_gallery.id)
         galleries = controller.get_all_sub_galleries(parents=False)
         self.assertEqual(len(galleries), 6)
 

File bviewer/core/views.py

 # -*- coding: utf-8 -*-
 import logging
-
 from django.conf import settings
 from django.contrib.auth.views import login, logout
 from django.http import Http404
     if not holder:
         return message_view(request, message='No user defined')
 
-    controller = GalleryController(holder, request.user, holder.top_gallery_id)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=holder.top_gallery_id)
+    if not controller.exists():
         return message_view(request, message='No main gallery')
 
     year_filter = get_year_parameter(request)
 
+    main = controller.get_object()
     galleries = controller.get_galleries(year=year_filter)
     years = controller.get_available_years()
 
     if not holder:
         return message_view(request, message='No user defined')
 
-    controller = GalleryController(holder, request.user, uid)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=uid)
+    if not controller.exists():
         return message_view(request, message='No such gallery')
 
+    main = controller.get_object()
     galleries, images, videos, years = None, None, None, None
     year_filter = get_year_parameter(request)
     if controller.is_album():

File bviewer/profile/views.py

     if not holder:
         raise Http404()
 
-    controller = GalleryController(holder, request.user, uid)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=uid)
+    if not controller.exists():
         return message_view(request, message='No such gallery')
 
     images = controller.get_images()
         logger.exception(e)
         return message_view(request, message=smart_text(e))
     return render(request, 'profile/images.html', {
-        'gallery': main,
+        'gallery': controller.get_object(),
         'folder': folder,
         'title': 'Select images',
     })
     if not holder:
         raise Http404()
 
-    controller = GalleryController(holder, request.user, uid)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=uid)
+    if not controller.exists():
         return message_view(request, message='No such gallery')
 
     try:

File bviewer/slideshow/controllers.py

 # -*- coding: utf-8 -*-
 import logging
 import random
-
 from django.db.models import Q
 
 from bviewer.core.controllers import GalleryController
         self.redis = redis
         self.holder = slideshow.gallery.user
         self.user = slideshow.user
-        self.gallery_ctrl = GalleryController(self.holder, self.user, slideshow.gallery_id)
+        self.gallery_ctrl = GalleryController(self.holder, self.user, uid=slideshow.gallery_id)
 
     def get_key(self):
         return 'slideshow-id:' + self.slideshow.id

File bviewer/slideshow/views.py

 # -*- coding: utf-8 -*-
 import logging
-
 from django.core.urlresolvers import reverse
 from django.shortcuts import render
 
     if not holder:
         return message_view(request, message='No user defined')
 
-    controller = GalleryController(holder, request.user, holder.top_gallery_id)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=holder.top_gallery_id)
+    if not controller.exists():
         return message_view(request, message='No such gallery')
 
+    main = controller.get_object()
     link = reverse('actions-slideshow-get-or-create') + '?gallery={0}'.format(holder.top_gallery_id)
     return render(request, 'slideshow/index.html', {
         'holder': holder,
     if not holder:
         return message_view(request, message='No user defined')
 
-    controller = GalleryController(holder, request.user, gallery_id)
-    main = controller.get_object()
-    if not main:
+    controller = GalleryController(holder, request.user, uid=gallery_id)
+    if not controller.exists():
         return message_view(request, message='No such gallery')
 
+    main = controller.get_object()
     link = reverse('actions-slideshow-get-or-create') + '?gallery={0}'.format(gallery_id)
     return render(request, 'slideshow/index.html', {
         'holder': holder,