fix #3663: changed and updated sort keys
authorJan Szejko <j-sz@o2.pl>
Wed, 27 Jan 2016 15:33:34 +0000 (16:33 +0100)
committerJan Szejko <j-sz@o2.pl>
Wed, 27 Jan 2016 15:33:34 +0000 (16:33 +0100)
src/catalogue/migrations/0009_auto_20160127_1019.py [new file with mode: 0644]
src/catalogue/models/tag.py
src/catalogue/views.py

diff --git a/src/catalogue/migrations/0009_auto_20160127_1019.py b/src/catalogue/migrations/0009_auto_20160127_1019.py
new file mode 100644 (file)
index 0000000..eebb58b
--- /dev/null
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+from sortify import sortify
+
+
+def update_sort_keys(apps, schema_editor):
+    Tag = apps.get_model('catalogue', 'Tag')
+    Book = apps.get_model('catalogue', 'Book')
+    Picture = apps.get_model('picture', 'Picture')
+    ContentType = apps.get_model('contenttypes', 'ContentType')
+    for author in Tag.objects.filter(category='author'):
+        name_parts = author.name.split()
+        sort_key = ' '.join([name_parts[-1]] + name_parts[:-1])
+        author.sort_key = sortify(sort_key.lower())
+        author.save()
+    for model in Book, Picture:
+        ct = ContentType.objects.get_for_model(model)
+        for obj in model.objects.all():
+            authors = Tag.objects.filter(category='author', items__content_type=ct, items__object_id=obj.id)
+            author = authors[0]
+            obj.sort_key_author = author.sort_key
+            obj.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0008_auto_20151221_1225'),
+        ('picture', '0007_auto_20160125_1709'),
+    ]
+
+    operations = [
+        migrations.RunPython(update_sort_keys),
+    ]
index 4a7be02..57935f8 100644 (file)
@@ -219,7 +219,7 @@ class Tag(TagBase):
                 lang = getattr(tag_name, 'lang', settings.LANGUAGE_CODE)
                 tag_sort_key = tag_name
                 if category == 'author':
                 lang = getattr(tag_name, 'lang', settings.LANGUAGE_CODE)
                 tag_sort_key = tag_name
                 if category == 'author':
-                    tag_sort_key = tag_name.last_name
+                    tag_sort_key = ' '.join((tag_name.last_name,) + tag_name.first_names)
                     tag_name = tag_name.readable()
                 if lang == settings.LANGUAGE_CODE:
                     # Allow creating new tag, if it's in default language.
                     tag_name = tag_name.readable()
                 if lang == settings.LANGUAGE_CODE:
                     # Allow creating new tag, if it's in default language.
index 297339e..92fe162 100644 (file)
@@ -34,8 +34,8 @@ staff_required = user_passes_test(lambda user: user.is_staff)
 
 
 def catalogue(request, as_json=False):
 
 
 def catalogue(request, as_json=False):
-    books = models.Book.objects.filter(parent=None)
-    pictures = Picture.objects.all()
+    books = models.Book.objects.filter(parent=None).order_by('sort_key_author', 'sort_key')
+    pictures = Picture.objects.order_by('sort_key_author', 'sort_key')
     collections = models.Collection.objects.all()
     return render(request, 'catalogue/catalogue.html', locals())
 
     collections = models.Collection.objects.all()
     return render(request, 'catalogue/catalogue.html', locals())
 
@@ -163,11 +163,9 @@ def tagged_object_list(request, tags='', gallery=False):
                 raise Http404
             else:
                 if tags:
                 raise Http404
             else:
                 if tags:
-                    objects = Picture.tagged.with_all(tags).order_by(
-                        'sort_key_author', 'title')
+                    objects = Picture.tagged.with_all(tags).order_by('sort_key_author', 'sort_key')
                 else:
                 else:
-                    objects = Picture.objects.all().order_by(
-                        'sort_key_author', 'title')
+                    objects = Picture.objects.all().order_by('sort_key_author', 'sort_key')
             areas = PictureArea.objects.filter(picture__in=objects)
             categories = split_tags(
                 models.Tag.objects.usage_for_queryset(
             areas = PictureArea.objects.filter(picture__in=objects)
             categories = split_tags(
                 models.Tag.objects.usage_for_queryset(
@@ -183,16 +181,15 @@ def tagged_object_list(request, tags='', gallery=False):
             else:
                 all_books = models.Book.objects.filter(parent=None)
             if shelf_is_set:
             else:
                 all_books = models.Book.objects.filter(parent=None)
             if shelf_is_set:
-                objects = all_books.order_by('sort_key_author', 'title')
+                objects = all_books.order_by('sort_key_author', 'sort_key')
                 related_book_tags = models.Tag.objects.usage_for_queryset(
                     objects, counts=True).exclude(
                     category='set').exclude(pk__in=tags_pks)
             else:
                 if tags:
                 related_book_tags = models.Tag.objects.usage_for_queryset(
                     objects, counts=True).exclude(
                     category='set').exclude(pk__in=tags_pks)
             else:
                 if tags:
-                    objects = models.Book.tagged_top_level(tags).order_by(
-                        'sort_key_author', 'title')
+                    objects = models.Book.tagged_top_level(tags).order_by('sort_key_author', 'sort_key')
                 else:
                 else:
-                    objects = all_books.order_by('sort_key_author', 'title')
+                    objects = all_books.order_by('sort_key_author', 'sort_key')
                 # WTF: was outside if, overwriting value assigned if shelf_is_set
                 related_book_tags = get_top_level_related_tags(tags)
 
                 # WTF: was outside if, overwriting value assigned if shelf_is_set
                 related_book_tags = get_top_level_related_tags(tags)