Commits

b7w committed 98c87e7

Fix bug integrity error duplicate key value violates unique constraint (title, user). Add validation for title.

  • Participants
  • Parent commits a9fdeb9

Comments (0)

Files changed (3)

bviewer/profile/admin.py

 # -*- coding: utf-8 -*-
 from collections import Counter
 import os
-
 from django.contrib.admin import AdminSite, ModelAdmin
 from django.contrib.auth.admin import UserAdmin
 from django.core.urlresolvers import reverse
 from bviewer.core.admin import ProxyUserForm
 from bviewer.core.files.storage import ImageStorage
 from bviewer.core.models import Gallery, Image, ProxyUser, Video
+from bviewer.profile.forms import AdminGalleryForm
 
 
 class ProfileSite(AdminSite):
 
 class ProfileGalleryAdmin(ProfileModelAdmin):
     list_select_related = True
+    form = AdminGalleryForm
 
     list_display = ('title', 'parent', 'visibility', 'images', 'time',)
     list_filter = ('parent__title', 'time', )
             obj.thumbnail_id = thumbnail_id
         else:
             obj.thumbnail = None
-        obj.save()
+        super(ProfileGalleryAdmin, self).save_model(request, obj, form, change)
 
     def add_view(self, request, form_url='', extra_context=None):
         # Add default parent Welcome gallery
+        user = ProxyUser.objects.get(pk=request.user.pk)
         data = request.GET.copy()
-        data['parent'] = ProxyUser.objects.get(pk=request.user.pk).top_gallery_id
+        data['parent'] = user.top_gallery_id
         request.GET = data
+        # Add default user
+        data = request.POST.copy()
+        data['user'] = user.id
+        request.POST = data
         return super(ProfileGalleryAdmin, self).add_view(request, form_url, extra_context)
 
     def formfield_for_foreignkey(self, db_field, request=None, **kwargs):

bviewer/profile/forms.py

 # -*- coding: utf-8 -*-
-
+from django.core.exceptions import ValidationError
 from django.forms import ModelForm
 
 from bviewer.core.models import Gallery, Video
 
 
+class AdminGalleryForm(ModelForm):
+    def clean_title(self):
+        title = self.cleaned_data['title']
+        user_id = self.data['user']
+        if Gallery.objects.filter(title=title, user_id=user_id).count() > 0:
+            raise ValidationError('Title must be unique')
+        return title
+
+    class Meta(object):
+        model = Gallery
+
+
 class GalleryForm(ModelForm):
     class Meta(object):
         model = Gallery

docs/releases/notes.rst

 | **v1.0.8 dev**
 | Add some small features
 
+* Fix bug admin gallery title unique integrity error
+* Fix bug not safe HTML_EXTRA
 * Add disk cache info in user profile
-* Fix bug with not safe HTML_EXTRA
 
 
 | **v1.0.7** - *26.01.1014*