Commits

b7w committed 8fb77d2

Second part of model refactor. Refactor api. Fix all tests.

Comments (0)

Files changed (12)

bviewer/api/filters.py

 from rest_framework.filters import BaseFilterBackend
 
 
-class UserSelfFilter(BaseFilterBackend):
+class GalleryUserSelfFilter(BaseFilterBackend):
     HTTP_METHODS = ('POST', 'DELETE',)
     GET_PARAM = 'user__self'
     QUERYSET_PARAM = 'user'
         return queryset
 
 
-class ItemUserSelfFilter(UserSelfFilter):
-    HTTP_METHODS = ('POST', 'DELETE',)
-    GET_PARAM = 'user__self'
-    QUERYSET_PARAM = 'album__user'
+class AlbumUserSelfFilter(GalleryUserSelfFilter):
+    QUERYSET_PARAM = 'gallery__user'
+
+
+class ItemUserSelfFilter(GalleryUserSelfFilter):
+    QUERYSET_PARAM = 'album__gallery__user'

bviewer/api/resources.py

 # -*- coding: utf-8 -*-
 from django.contrib.auth.models import User
 from django.db.models import Q
-
 from rest_framework.filters import OrderingFilter, DjangoFilterBackend
 from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
 from rest_framework.viewsets import ModelViewSet
-from bviewer.api.filters import UserSelfFilter, ItemUserSelfFilter
 
-from bviewer.api.serializers import UserSerializer, AlbumSerializer, ImageSerializer, VideoSerializer
+from bviewer.api.filters import GalleryUserSelfFilter, AlbumUserSelfFilter, ItemUserSelfFilter
+from bviewer.api.serializers import UserSerializer, GallerySerializer, AlbumSerializer, ImageSerializer, VideoSerializer
 from bviewer.core.models import Gallery, Album, Image, Video
 
 
 
 class GalleryResource(ModelViewSet):
     queryset = Gallery.objects.all().select_related()
-    serializer_class = UserSerializer
-    http_method_names = ('get',)
+
+    http_method_names = ('get', 'post', 'delete',)
+    serializer_class = GallerySerializer
     permission_classes = (IsAuthenticated,)
 
-    filter_backends = (OrderingFilter, DjangoFilterBackend,)
+    filter_backends = (GalleryUserSelfFilter, OrderingFilter, DjangoFilterBackend,)
     ordering = ('user',)
 
     paginate_by = ITEMS_PER_PAGE
     serializer_class = AlbumSerializer
     permission_classes = (IsAuthenticatedOrReadOnly,)
 
-    filter_backends = (UserSelfFilter, OrderingFilter, DjangoFilterBackend)
+    filter_backends = (AlbumUserSelfFilter, OrderingFilter, DjangoFilterBackend)
     filter_fields = ('id', 'gallery', 'title')
     ordering = ('title', 'time',)
 
     def get_queryset(self):
         user = self.request.user
         if user.is_authenticated():
-            return self.queryset.filter(Q(visibility=Album.VISIBLE) | Q(user=user))
+            return self.queryset.filter(Q(visibility=Album.VISIBLE) | Q(gallery__user=user))
         else:
             return self.queryset.filter(visibility=Album.VISIBLE)
 
     def get_queryset(self):
         user = self.request.user
         if user.is_authenticated():
-            return self.queryset.filter(Q(album__visibility=Album.VISIBLE) | Q(album__user=user))
+            return self.queryset.filter(Q(album__visibility=Album.VISIBLE) | Q(album__gallery__user=user))
         else:
             return self.queryset.filter(album__visibility=Album.VISIBLE)
 
     def get_queryset(self):
         user = self.request.user
         if user.is_authenticated():
-            return self.queryset.filter(Q(album__visibility=Album.VISIBLE) | Q(album__user=user))
+            return self.queryset.filter(Q(album__visibility=Album.VISIBLE) | Q(album__gallery__user=user))
         else:
             return self.queryset.filter(album__visibility=Album.VISIBLE)

bviewer/api/serializers.py

 from rest_framework.relations import HyperlinkedRelatedField, HyperlinkedIdentityField
 from rest_framework.serializers import ModelSerializer
 
-from bviewer.core.models import Album, Image, Video
+from bviewer.core.models import Gallery, Album, Image, Video
 
 
 class UserSerializer(ModelSerializer):
-    detail = HyperlinkedIdentityField(view_name='proxyuser-detail')
+    detail = HyperlinkedIdentityField(view_name='user-detail')
 
     class Meta:
         model = User
         fields = ('detail', 'id', 'username', 'date_joined', 'last_login', 'about_title', 'about_text',)
 
 
+class GallerySerializer(ModelSerializer):
+    detail = HyperlinkedIdentityField(view_name='gallery-detail')
+    user_detail = HyperlinkedRelatedField(source='user', view_name='user-detail', read_only=True)
+
+    class Meta:
+        model = Gallery
+
+
 class AlbumSerializer(ModelSerializer):
     detail = HyperlinkedIdentityField(view_name='album-detail')
-    user_detail = HyperlinkedRelatedField(source='user', view_name='proxyuser-detail', read_only=True)
+    gallery_detail = HyperlinkedRelatedField(source='gallery', view_name='gallery-detail', read_only=True)
     parent_detail = HyperlinkedRelatedField(source='parent', view_name='album-detail', read_only=True)
     thumbnail_detail = HyperlinkedRelatedField(source='thumbnail', view_name='image-detail', read_only=True)
 
     detail = HyperlinkedIdentityField(view_name='image-detail')
     album_detail = HyperlinkedRelatedField(source='album', view_name='album-detail', read_only=True)
 
-    def __init__(self, *args, **kwargs):
-        super(ImageSerializer, self).__init__(*args, **kwargs)
-
     def to_native(self, obj):
         if obj and isinstance(obj, self.Meta.model):
             user = self.context['request'].user
-            if not (user.is_authenticated() and obj.album.user_id == user.id):
+            if not (user.is_authenticated() and obj.album.gallery.user_id == user.id):
                 obj.path = None
         return super(ImageSerializer, self).to_native(obj)
 

bviewer/api/tests/base.py

         settings.TEST = True
 
         self.client = ResourceClient()
-        self.data = TestData()
-        self.data.load_all()
+        self.data = TestData().load_all()
 
     def login_user(self, user):
         self.assertTrue(self.client.login(username=user.username, password=TestData.PASSWORD))

bviewer/api/tests/test_advance.py

 class AdvanceTestCase(BaseResourceTestCase):
     def test_filter_self(self):
         self.login_user(self.data.user_b7w)
+        response = self.client.rest_get('/api/v1/gallery/?user__self')
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+        self.assertEqual(len(response.objects), 1)
+
         response = self.client.rest_get('/api/v1/album/?user__self')
         self.assertEqual(response.status_code, status.HTTP_200_OK)
         self.assertEqual(len(response.objects), 5)

bviewer/api/tests/test_edit.py

     def setUp(self):
         super(EditTestCase, self).setUp()
         user_id = self.data.user_b7w.id
+        gallery_id = self.data.gallery_b7w.id
         album_id = self.data.album_b7w.id
 
-        self.new_album = dict(title='New', user=user_id)
+        self.new_gallery = dict(title='New gallery', user=user_id, url='test')
+        self.new_album = dict(title='New album', gallery=gallery_id)
         self.new_image = dict(path='image1.jpg', album=album_id)
-        self.data.generate_image(self.data.user_b7w.home, 'image1.jpg')
+        self.data.generate_image(self.data.gallery_b7w.home, 'image1.jpg')
         self.new_video = dict(uid='123456', title='New', album=album_id)
 
     def test_public_post(self):
+        response = self.client.rest_post('/api/v1/gallery/', data=self.new_gallery)
+        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
+
         response = self.client.rest_post('/api/v1/album/', data=self.new_album)
         self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
 
 
     def test_login_post(self):
         self.login_user(self.data.user_b7w)
+        response = self.client.rest_post('/api/v1/gallery/', data=self.new_gallery)
+        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+
         response = self.client.rest_post('/api/v1/album/', data=self.new_album)
         self.assertEqual(response.status_code, status.HTTP_201_CREATED)
 
         self.assertEqual(response.status_code, status.HTTP_201_CREATED)
 
     def test_public_delete(self):
+        response = self.client.rest_delete('/api/v1/gallery/{0}/'.format(self.data.gallery_b7w.id))
+        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
+
         response = self.client.rest_delete('/api/v1/album/{0}/'.format(self.data.album1.id))
         self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
 
         self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
 
         response = self.client.rest_delete('/api/v1/album/{0}/'.format(self.data.album1.id))
+        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
+
+        response = self.client.rest_delete('/api/v1/gallery/{0}/'.format(self.data.gallery_b7w.id))
         self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

bviewer/api/versions.py

 # -*- coding: utf-8 -*-
-from bviewer.api.resources import UserResource, AlbumResource, ImageResource, VideoResource
+from bviewer.api.resources import UserResource, GalleryResource, AlbumResource, ImageResource, VideoResource
 from bviewer.api.router import ExtraRouter
 from bviewer.slideshow.resources import SlideShowResource
 
 
 version1 = ExtraRouter()
 version1.register(r'user', UserResource)
+version1.register(r'gallery', GalleryResource)
 version1.register(r'album', AlbumResource)
 version1.register(r'image', ImageResource)
 version1.register(r'video', VideoResource)

bviewer/core/models.py

     CACHE_ARCHIVE_SIZE_MIN = 128
     CACHE_ARCHIVE_SIZE_MAX = 2048
 
+    title = models.CharField(max_length=256)
     user = models.ForeignKey(User)
 
     url = models.CharField(max_length=16, unique=True)
         """
         Check path exists
         """
-        storage = ImageStorage(self.album.user)
+        storage = ImageStorage(self.album.gallery)
         if not storage.exists(self.path):
             raise ValidationError(smart_text('No {0} path exists').format(self.path))
 

bviewer/core/tests/data.py

         self.load_albums()
         self.load_images()
         self.load_videos()
+        return self
 
     def load_users(self):
         self.user_b7w = User(username='B7W')

bviewer/slideshow/controllers.py

     def __init__(self, slideshow, redis=None):
         self.slideshow = slideshow
         self.redis = redis
-        self.gallery = slideshow.album.user
+        self.gallery = slideshow.album.gallery
         self.user = slideshow.user
-        self.album_ctrl = AlbumController(selfgalleryr, self.user, uid=slideshow.album_id)
+        self.album_ctrl = AlbumController(self.gallery, self.user, uid=slideshow.album_id)
 
     def get_key(self):
         return 'slideshow-id:' + self.slideshow.id

bviewer/slideshow/tests.py

         # Force create session
         self.client.cookies[settings.SESSION_COOKIE_NAME] = 'session_test'
 
-        album_id = self.data.user_b7w.top_album.id
+        album_id = self.data.gallery_b7w.top_album.id
         response = self.client.rest_get('/api/v1/actions/slideshow/get_or_create/?album={0}'.format(album_id))
         obj1 = SlideShow.objects.safe_get(id=response.object['id'])
         self.assertIsNotNone(obj1)
         self.assertEqual(response.error, 'No slideshow with id "None"')
 
         obj = SlideShow.objects.create(
-            album=self.data.user_b7w.top_album,
+            album=self.data.gallery_b7w.top_album,
             user=self.data.user_b7w,
             session_key=self.client.session.session_key,
         )

docs/ref/settings.rst

 ====================
 
 VIEWER_GALLERY_ID
----------------
+-----------------
 
 .. _CONF_VIEWER_GALLERY_ID: