From a19232b3e567e59a125d1bc7f3708617a37f1c7c Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Wed, 27 Jan 2016 16:33:34 +0100 Subject: [PATCH] fix #3663: changed and updated sort keys --- .../migrations/0009_auto_20160127_1019.py | 37 +++++++++++++++++++ src/catalogue/models/tag.py | 2 +- src/catalogue/views.py | 17 ++++----- 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/catalogue/migrations/0009_auto_20160127_1019.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 index 000000000..eebb58bb1 --- /dev/null +++ b/src/catalogue/migrations/0009_auto_20160127_1019.py @@ -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), + ] diff --git a/src/catalogue/models/tag.py b/src/catalogue/models/tag.py index 4a7be0247..57935f8fb 100644 --- a/src/catalogue/models/tag.py +++ b/src/catalogue/models/tag.py @@ -219,7 +219,7 @@ class Tag(TagBase): 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. diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 297339ebe..92fe162b1 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -34,8 +34,8 @@ staff_required = user_passes_test(lambda user: user.is_staff) 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()) @@ -163,11 +163,9 @@ def tagged_object_list(request, tags='', gallery=False): 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: - 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( @@ -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: - 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: - 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: - 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) -- 2.20.1