Commits

b7w committed 69c70bd

Third part of model refactor. Refactor admin sites.

Note. It is not fully ready, just works.

  • Participants
  • Parent commits 8fb77d2

Comments (0)

Files changed (7)

File bviewer/core/admin.py

 import os
 
 from django.contrib.admin import site, ModelAdmin
-from django.contrib.auth.admin import UserAdmin
-from django import forms
-from django.contrib.auth.forms import UserChangeForm
 
-from bviewer.core.utils import RaisingRange
 from bviewer.core.models import Gallery, Album, Image, Video
 
 
+class GalleryAdmin(ModelAdmin):
+    list_select_related = True
+
+
+site.register(Gallery, GalleryAdmin)
+
+
 class AlbumAdmin(ModelAdmin):
     list_select_related = True
 
-    list_display = ('title', 'parent', 'user', 'visibility', 'time',)
-    list_filter = ('parent__title', 'user__username', 'time', )
-    ordering = ('user', 'parent', '-time',)
+    ordering = ('gallery', 'parent', '-time',)
 
     search_fields = ('title', 'description',)
 
     list_select_related = True
 
     list_display = ('path', 'file_name', 'album_user', 'album_title', 'time', )
-    list_filter = ('album__title', 'album__user__username', 'time',)
-    ordering = ('album__user__username', 'path', '-time',)
+    list_filter = ('album__title', 'album__gallery__user__username', 'time',)
+    ordering = ('album__gallery__user__username', 'path', '-time',)
 
     search_fields = ('album__title', 'path',)
 
         return obj.album.title
 
     def album_user(self, obj):
-        return obj.album.user.username
+        return obj.album.gallery.user.username
 
 
 site.register(Image, ImageAdmin)
     list_select_related = True
 
     list_display = ('album_title', 'album_user', 'title', 'type', 'uid', 'time', )
-    list_filter = ('album__title', 'album__user__username', 'time',)
-    ordering = ('album__user__username', '-time',)
+    list_filter = ('album__title', 'album__gallery__user__username', 'time',)
+    ordering = ('album__gallery__user__username', '-time',)
 
     search_fields = ('album__title', 'title',)
 
         return obj.album.title
 
     def album_user(self, obj):
-        return obj.album.user.username
+        return obj.album.gallery.user.username
 
 
 site.register(Video, VideoAdmin)
-
-
-class GalleryForm(UserChangeForm):
-    """
-    Set for UserAdmin Gallery model except User.
-    Add choice field for cache size filed.
-    """
-
-    def __init__(self, *args, **kwargs):
-        super(GalleryForm, self).__init__(*args, **kwargs)
-        self._set_choice('cache_size',
-            cache_max=Gallery.CACHE_SIZE_MAX,
-            cache_min=Gallery.CACHE_SIZE_MIN,
-            base=16
-        )
-        self._set_choice('cache_archive_size',
-            cache_max=Gallery.CACHE_ARCHIVE_SIZE_MAX,
-            cache_min=Gallery.CACHE_ARCHIVE_SIZE_MIN,
-            base=64
-        )
-
-    def _set_choice(self, field_name, cache_max, cache_min, base):
-        raising = RaisingRange(cache_max, start=cache_min, base=base)
-        choice = [(i, '%s MB' % i) for i in raising]
-        self.fields[field_name] = forms.ChoiceField(choices=choice)
-
-    class Meta(object):
-        model = Gallery
-
-
-class GalleryAdmin(UserAdmin, ModelAdmin):
-    list_select_related = True
-
-    list_display = ('username', 'email', 'is_staff', 'home', 'top_album', )
-
-    extra_fieldsets = (
-        ('Account info', {'fields': ('username', 'password', )}),
-        ('Personal info', {'fields': ('email', 'first_name', 'last_name', )}),
-        ('Viewer info', {'fields': ('url', 'home', 'top_album', 'cache_size', 'cache_archive_size', )}),
-        ('Additional info', {'fields': ('about_title', 'about_text',)}),
-    )
-    fieldsets = extra_fieldsets + UserAdmin.fieldsets[2:]
-    readonly_fields = ('is_active', 'is_staff', 'last_login', 'date_joined', )
-
-    form = GalleryForm
-
-
-site.register(Gallery, GalleryAdmin)

File bviewer/core/models.py

             self.url = '{0}.{1}'.format(url, domain)
         super(Gallery, self).save(*args, **kwargs)
 
+    def __str__(self):
+        return self.title
+
+    __unicode__ = __str__
+
     class Meta(object):
         ordering = ['user']
+        verbose_name = 'Gallery'
+        verbose_name_plural = 'Galleries'
         permissions = (
-            ('user_gallery', 'User is albums gallery'),
+            ('user_holder', 'User is gallery holder'),
         )
 
 

File bviewer/core/templates/core/base.html

                     <li class="divider-vertical"></li>
                 {% endif %}
                 {% if request.user.is_authenticated %}
-                    {% if perms.core.user_gallery %}
+                    {% if perms.core.user_holder %}
                         <li class="view-full"><a href="{% url 'profile:index' %}">Profile</a></li>
                     {% endif %}
                     <li><a href="{% url 'core.logout' %}">Log out</a></li>

File bviewer/core/tests/test_models.py

 
         user = User.objects.get(username='Test')
         need = [
+            'core.user_holder',
             'core.change_gallery',
-            'core.user_gallery',
             'core.add_album',
             'core.change_album',
             'core.delete_album',

File bviewer/profile/admin.py

 # -*- coding: utf-8 -*-
 import os
 from collections import Counter
+
 from django.contrib.admin import AdminSite, ModelAdmin
-from django.contrib.auth.admin import UserAdmin
+from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.template.loader import render_to_string
 from django.utils.encoding import smart_text
 
-from bviewer.core.admin import GalleryForm
 from bviewer.core.controllers import AlbumController
 from bviewer.core.files.storage import ImageStorage
 from bviewer.core.models import Album, Image, Gallery, Video
 from bviewer.profile.actions import bulk_time_update, update_time_from_exif
-from bviewer.profile.forms import AdminAlbumForm
+from bviewer.profile.forms import AdminGalleryForm, AdminAlbumForm
 
 
 class ProfileSite(AdminSite):
 
     def has_permission(self, request):
         user = request.user
-        return user.is_active and request.user.has_perm('core.user_gallery')
+        return user.is_active and request.user.has_perm('core.user_holder')
 
 
 profile = ProfileSite()
         return self.admin_site.has_permission(request)
 
 
+class ProfileUserAdmin(ProfileModelAdmin):
+    list_select_related = True
+
+
+profile.register(User, ProfileUserAdmin)
+
+
+class ProfileGalleryAdmin(ProfileModelAdmin):
+    list_select_related = True
+    form = AdminGalleryForm
+
+    list_display = ('url', 'title', 'top_album', )
+    list_filter = ()
+
+    readonly_fields = ('cache_info', )
+
+    def cache_info(self, user):
+        storage = ImageStorage(user)
+        images_size = storage.cache_size() / 2 ** 20
+        storage = ImageStorage(user, archive_cache=True)
+        archive_size = storage.cache_size() / 2 ** 20
+        return 'Images size: {0} MB, archives size: {1} MB'.format(images_size, archive_size)
+
+    def has_add_permission(self, request):
+        return False
+
+    def queryset(self, request):
+        return super(ProfileGalleryAdmin, self).queryset(request).filter(user=request.user)
+
+    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
+        """
+        Show in drop down menu only user albums
+        """
+        if db_field.name == 'top_album':
+            kwargs['queryset'] = Album.objects.filter(gallery__user=request.user)
+        return super(ProfileGalleryAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
+
+
+profile.register(Gallery, ProfileGalleryAdmin)
+
+
 class ProfileAlbumAdmin(ProfileModelAdmin):
     list_select_related = True
     form = AdminAlbumForm
     search_fields = ('title', 'description',)
 
     readonly_fields = ('images', 'pre_cache', 'thumbnails',)
-    fields = ('parent', 'title', 'visibility', 'album_sorting', 'allow_archiving',
+    fields = ('gallery', 'parent', 'title', 'visibility', 'album_sorting', 'allow_archiving',
               'images', 'pre_cache', 'description', 'time', 'thumbnails', )
 
     def images(self, obj):
     thumbnails.short_description = 'Album thumbnail'
 
     def queryset(self, request):
-        return super(ProfileAlbumAdmin, self).queryset(request).filter(user=request.user)
+        return super(ProfileAlbumAdmin, self).queryset(request).filter(gallery__user=request.user)
 
     def save_model(self, request, obj, form, change):
         obj.user = Gallery.objects.get(pk=request.user.pk)
             controller.set_archiving(obj.allow_archiving)
         super(ProfileAlbumAdmin, self).save_model(request, obj, form, change)
 
-    def get_form(self, request, obj=None, **kwargs):
-        # Add default parent Welcome album
-        user = Gallery.objects.get(pk=request.user.pk)
-        data = request.GET.copy()
-        data['parent'] = user.top_album_id
-        request.GET = data
-        # Add default user
-        data = request.POST.copy()
-        data['user'] = user.id
-        request.POST = data
-        return super(ProfileAlbumAdmin, self).get_form(request, obj=None, **kwargs)
+    # def get_form(self, request, obj=None, **kwargs):
+    # # Add default parent Welcome album
+    # user = Gallery.objects.get(pk=request.user.pk)
+    #     data = request.GET.copy()
+    #     data['parent'] = user.top_album_id
+    #     request.GET = data
+    #     # Add default user
+    #     data = request.POST.copy()
+    #     data['user'] = user.id
+    #     request.POST = data
+    #     return super(ProfileAlbumAdmin, self).get_form(request, obj=None, **kwargs)
 
     def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
         """
         Show in drop down menu only user albums
         """
         if db_field.name == 'parent':
-            kwargs['queryset'] = Album.objects.filter(user=request.user)
+            kwargs['queryset'] = Album.objects.filter(gallery__user=request.user)
         return super(ProfileAlbumAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
 
     class Media(object):
     image_thumbnail_popup.allow_tags = True
 
     def queryset(self, request):
-        return super(ProfileImageAdmin, self).queryset(request).filter(album__user=request.user)
+        return super(ProfileImageAdmin, self).queryset(request).filter(album__gallery__user=request.user)
 
     def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
         """
         Show in drop down menu only user albums
         """
         if db_field.name == 'album':
-            kwargs['queryset'] = Album.objects.filter(user=request.user)
+            kwargs['queryset'] = Album.objects.filter(gallery__user=request.user)
         return super(ProfileImageAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
 
     class Media:
         css = {
             'all': ('profile/css/profile.css',)
         }
-        js = ('my_code.js',)
 
 
 profile.register(Image, ProfileImageAdmin)
         return obj.album.title
 
     def queryset(self, request):
-        return super(ProfileVideoAdmin, self).queryset(request).filter(album__user=request.user)
+        return super(ProfileVideoAdmin, self).queryset(request).filter(album__gallery__user=request.user)
 
     def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
         """
         Show in drop down menu only user albums
         """
         if db_field.name == 'album':
-            kwargs['queryset'] = Album.objects.filter(user=request.user)
+            kwargs['queryset'] = Album.objects.filter(gallery__user=request.user)
         return super(ProfileVideoAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
 
 
-profile.register(Video, ProfileVideoAdmin)
-
-
-class ProfileUserAdmin(ProfileModelAdmin, UserAdmin):
-    list_select_related = True
-
-    list_display = ('username', 'url', 'email', 'top_album', 'is_staff', )
-    list_filter = ()
-
-    fieldsets = (
-        ('Account info', {'fields': ('username', 'password', )}),
-        ('Personal info', {'fields': ('email', 'first_name', 'last_name', )}),
-        ('Viewer info', {'fields': ('url', 'top_album', 'cache_size', 'cache_archive_size', 'cache_info', )}),
-        ('Additional info', {'fields': ('about_title', 'about_text', )}),
-        ('Important dates', {'fields': ('last_login', 'date_joined', )}),
-    )
-    readonly_fields = ('last_login', 'date_joined', 'cache_info', )
-
-    form = GalleryForm
-
-    def cache_info(self, user):
-        storage = ImageStorage(user)
-        images_size = storage.cache_size() / 2 ** 20
-        storage = ImageStorage(user, archive_cache=True)
-        archive_size = storage.cache_size() / 2 ** 20
-        return 'Images size: {0} MB, archives size: {1} MB'.format(images_size, archive_size)
-
-    def has_add_permission(self, request):
-        return False
-
-    def queryset(self, request):
-        return super(ProfileUserAdmin, self).queryset(request).filter(id=request.user.id)
-
-    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
-        """
-        Show in drop down menu only user albums
-        """
-        if db_field.name == 'top_album':
-            kwargs['queryset'] = Album.objects.filter(user=request.user)
-        return super(ProfileUserAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
-
-
-profile.register(Gallery, ProfileUserAdmin)
+profile.register(Video, ProfileVideoAdmin)

File bviewer/profile/forms.py

 from django.core.exceptions import ValidationError
 from django.forms import Form, ModelForm, ChoiceField, CharField, MultipleHiddenInput
 
-from bviewer.core.models import Album, Video
+from bviewer.core.models import Gallery, Album, Video
+from bviewer.core.utils import RaisingRange
 
 
 class BulkTimeUpdateForm(Form):
         return timedelta(**kwargs)
 
 
+class AdminGalleryForm(ModelForm):
+    """
+    Set for UserAdmin Gallery model except User.
+    Add choice field for cache size filed.
+    """
+
+    def __init__(self, *args, **kwargs):
+        super(AdminGalleryForm, self).__init__(*args, **kwargs)
+        self._set_choice('cache_size',
+                         cache_max=Gallery.CACHE_SIZE_MAX,
+                         cache_min=Gallery.CACHE_SIZE_MIN,
+                         base=16
+        )
+        self._set_choice('cache_archive_size',
+                         cache_max=Gallery.CACHE_ARCHIVE_SIZE_MAX,
+                         cache_min=Gallery.CACHE_ARCHIVE_SIZE_MIN,
+                         base=64
+        )
+
+    def _set_choice(self, field_name, cache_max, cache_min, base):
+        raising = RaisingRange(cache_max, start=cache_min, base=base)
+        choice = [(i, '%s MB' % i) for i in raising]
+        self.fields[field_name] = ChoiceField(choices=choice)
+
+    class Meta(object):
+        model = Gallery
+
+
 class AdminAlbumForm(ModelForm):
     def clean_title(self):
         title = self.cleaned_data['title']

File bviewer/profile/views.py

 
 
 @login_required
-@permission_required('core.user_gallery')
+@permission_required('core.user_holder')
 def images_view(request, uid):
     gallery = get_gallery(request)
     if not gallery:
 
 
 @login_required
-@permission_required('core.user_gallery')
+@permission_required('core.user_holder')
 def album_pre_cache(request, uid):
     gallery = get_gallery(request)
     if not gallery:
 
 
 @login_required
-@permission_required('core.user_gallery')
+@permission_required('core.user_holder')
 def download_image(request):
     if request.GET.get('p', None):
         path = request.GET['p']