X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/709daa52fd9261caebeb4ba2f0972c6e77882314..9c5d9a4e77a10b4e60d89d3890e49002bd7f3993:/apps/catalogue/migrations/0003_fix_book_count_on_shelves.py diff --git a/apps/catalogue/migrations/0003_fix_book_count_on_shelves.py b/apps/catalogue/migrations/0003_fix_book_count_on_shelves.py index 97f1b0c38..8180311b2 100644 --- a/apps/catalogue/migrations/0003_fix_book_count_on_shelves.py +++ b/apps/catalogue/migrations/0003_fix_book_count_on_shelves.py @@ -2,15 +2,33 @@ import datetime from south.db import db from south.v2 import DataMigration -from django.db import models +from django.db import connection, models +qn = connection.ops.quote_name class Migration(DataMigration): def forwards(self, orm): - for tag in orm['catalogue.Tag'].objects.filter(user__isnull=False): - books = orm['catalogue.Tag'].intermediary_table_model.objects.get_intersection_by_model( - orm['catalogue.Book'], [tag]) - tag.book_count = len(books) + model = orm.Book + model_table = qn(model._meta.db_table) + for tag in orm.Tag.objects.exclude(user=None): + query = """ + SELECT COUNT(%(model_pk)s) -- count books + FROM %(model)s, %(tagged_item)s -- from books x tagged + WHERE + %(tagged_item)s.tag_id=%(tag_id)s -- get only the shelf + AND %(model_pk)s = %(tagged_item)s.object_id -- get only books on the shelf + GROUP BY %(tagged_item)s.tag_id""" % { + 'model_pk': '%s.%s' % (model_table, qn(model._meta.pk.column)), + 'model': model_table, + 'tagged_item': qn(orm.TagRelation._meta.db_table), + 'tag_id': tag.pk, + } + + cursor = connection.cursor() + cursor.execute(query) + book_count = (cursor.fetchone() or (0,))[0] + + tag.book_count = book_count tag.save() def backwards(self, orm):