--- /dev/null
+# -*- 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),
+ ]
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.
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())
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(
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)