Commits

b7w  committed 31f839c

Add checking allow archiving for children

  • Participants
  • Parent commits 6220524

Comments (0)

Files changed (4)

File bviewer/core/controllers.py

 
 
 class BaseController(object):
-    def __init__(self, holder, user, uid):
+    def __init__(self, holder, user, uid=None, obj=None):
         """
         :type holder: bviewer.core.models.ProxyUser
         :type user: django.contrib.auth.models.User
         """
         self.holder = holder
         self.user = user
-        self.uid = uid
+        if uid:
+            self.uid = uid
+        else:
+            self.uid = obj.id
+        self.obj = obj
 
     def is_owner(self):
         """
         """
         return self.holder == self.user
 
+    @staticmethod
+    def from_obj(obj):
+        raise NotImplementedError()
+
     def get_object(self):
         raise NotImplementedError()
 
         if sorting == Gallery.DESK:
             return query_set.order_by('-time')
 
+    @staticmethod
+    def from_obj(obj):
+        """
+        :type obj: bviewer.core.models.Gallery
+        """
+        return GalleryController(obj.user, obj.user, obj=obj)
+
     @cache_method
     def get_object(self):
         """
 
         :rtype: bviewer.core.models.Gallery or None
         """
+        if self.obj:
+            return self.obj
         if self.is_owner():
             return Gallery.objects.safe_get(pk=self.uid, user_id=self.holder.id)
         return Gallery.objects.safe_get(Q(pk=self.uid), Q(user_id=self.holder.id), self.OPEN)
         return result
 
     def pre_cache(self):
-        ids = [self.get_object().id,]
+        ids = [self.get_object().id, ]
         ids.extend(i.id for i in self.get_all_sub_galleries())
         images = Image.objects.filter(gallery__in=ids)
         sizes = [i for i in settings.VIEWER_IMAGE_SIZE.keys() if i != 'full']
             as_job(image_async.process, waite=False)
 
     def is_archiving_allowed(self):
-        obj = self.get_object()
-        return obj and obj.allow_archiving
+        return self.get_object().allow_archiving
+
+    def set_archiving(self, value):
+        self.get_object().allow_archiving = value
+        for gallery in self.get_all_sub_galleries():
+            gallery.allow_archiving = value
+            gallery.save()
 
     def is_album(self):
         """

File bviewer/core/tests/test_controllers.py

         galleries = controller.get_all_sub_galleries(parents=False)
         self.assertEqual(len(galleries), 6)
 
+    def test_gallery_controller_from_obj(self):
+        """
+        Tests GalleryController.from_obj
+        """
+        data = TestData()
+        data.load_users()
 
+        user = data.user_b7w
+        top_gallery = user.top_gallery
+
+        controller = GalleryController(user, user, uid=top_gallery.id)
+        self.assertEqual(controller.uid, top_gallery.id)
+        self.assertIsNone(controller.obj)
+        self.assertEqual(controller.get_object(), top_gallery)
+
+        controller = GalleryController(user, user, obj=top_gallery)
+        self.assertEqual(controller.uid, top_gallery.id)
+        self.assertEqual(controller.obj, top_gallery)
+        self.assertEqual(controller.get_object(), top_gallery)
+
+

File bviewer/profile/admin.py

 from django.utils.encoding import smart_text
 
 from bviewer.core.admin import ProxyUserForm
+from bviewer.core.controllers import GalleryController
 from bviewer.core.files.storage import ImageStorage
 from bviewer.core.models import Gallery, Image, ProxyUser, Video
 from bviewer.profile.actions import bulk_time_update, update_time_from_exif
             obj.thumbnail_id = thumbnail_id
         else:
             obj.thumbnail = None
+        # allow archiving
+        if change and 'allow_archiving' in form.changed_data:
+            controller = GalleryController.from_obj(obj)
+            controller.set_archiving(obj.allow_archiving)
         super(ProfileGalleryAdmin, self).save_model(request, obj, form, change)
 
     def get_form(self, request, obj=None, **kwargs):

File bviewer/settings/debug.py

 #
 # Debug toolbar configs
 #
-
 DEBUG_TOOLBAR_PANELS = (
     'debug_toolbar.panels.versions.VersionsPanel',
     'debug_toolbar.panels.timer.TimerPanel',
     'debug_toolbar.panels.headers.HeadersPanel',
     'debug_toolbar.panels.request.RequestPanel',
     'debug_toolbar.panels.sql.SQLPanel',
+    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
     'debug_toolbar.panels.templates.TemplatesPanel',
     'debug_toolbar.panels.cache.CachePanel',
     'debug_toolbar.panels.signals.SignalsPanel',