Commits

b7w committed 5f4ac54

Fourth part of model refactor. Refactor admin sites, fix bugs.

Comments (0)

Files changed (4)

bviewer/core/models.py

     from urllib.request import urlopen
     from urllib.error import URLError
 
-from django.contrib.auth.models import User, Permission
+from django.contrib.auth.models import User
 from django.contrib.sites.models import Site
 from django.core.exceptions import ValidationError
 from django.core.validators import MaxValueValidator, MinValueValidator
 from django.db import models
-from django.db.models.query_utils import Q
 from django.db.models.signals import post_save
 from django.utils import timezone
 from django.utils.encoding import smart_text
 
 def add_top_album(sender, instance, created, **kwargs):
     if created:
-        gal = Album(gallery=instance, title='Welcome', description='Edit main album to change it')
-        gal.save()
-        instance.top_album = gal
-        perms = Permission.objects.filter(
-            Q(codename='change_gallery') |
-            Q(codename='user_gallery') |
-            Q(codename='add_album') |
-            Q(codename='change_album') |
-            Q(codename='delete_album') |
-            Q(codename='add_image') |
-            Q(codename='change_image') |
-            Q(codename='delete_image') |
-            Q(codename='add_video') |
-            Q(codename='change_video') |
-            Q(codename='delete_video') |
-            Q(codename='add_slideshow') |
-            Q(codename='change_slideshow') |
-            Q(codename='delete_slideshow')
-        )
-        instance.user.user_permissions = list(perms)
+        instance.top_album = Album.objects \
+            .create(gallery=instance, title='Welcome', description='Edit main album to change it')
         instance.save()
 
 

bviewer/core/tests/data.py

 
     def update_to_gallery(self, name):
         user = User.objects.get(username=name)
-        perm = Permission.objects.get(codename='user_gallery')
+        perm = Permission.objects.get(codename='user_holder')
         user.user_permissions.add(perm)
         user.save()
 

bviewer/core/tests/test_models.py

         self.assertIsNotNone(Gallery.objects.get(user__username='Test').top_album)
         self.assertTrue(Album.objects.filter(gallery__user__username='Test').exists())
 
-        user = User.objects.get(username='Test')
-        need = [
-            'core.user_holder',
-            'core.change_gallery',
-            'core.add_album',
-            'core.change_album',
-            'core.delete_album',
-            'core.add_image',
-            'core.change_image',
-            'core.delete_image',
-            'core.add_video',
-            'core.change_video',
-            'core.delete_video',
-            'slideshow.add_slideshow',
-            'slideshow.change_slideshow',
-            'slideshow.delete_slideshow',
-        ]
-        self.assertEqual(set(need), user.get_all_permissions())
-
     def assertHttpOk(self, url):
         """
         Open http connection, send HEAD and assert 200 status

bviewer/profile/admin.py

 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
         return self.admin_site.has_permission(request)
 
 
-class ProfileUserAdmin(ProfileModelAdmin):
+class ProfileUserAdmin(UserAdmin):
     list_select_related = True
 
+    list_display = ('username', 'email')
+
+    extra_fieldsets = (
+        ('Account info', {'fields': ('username', 'password', )}),
+        # ('Personal info', {'fields': ('email', 'first_name', 'last_name', )}),
+    )
+    fieldsets = (extra_fieldsets[0], UserAdmin.fieldsets[1], UserAdmin.fieldsets[3])
+    readonly_fields = ('is_active', 'is_staff', 'last_login', 'date_joined', )
+
+    def queryset(self, request):
+        return super(ProfileUserAdmin, self).queryset(request).filter(id=request.user.id)
+
 
 profile.register(User, ProfileUserAdmin)