3 from south.db import db
4 from south.v2 import DataMigration
5 from django.db import connection, models
6 qn = connection.ops.quote_name
8 class Migration(DataMigration):
10 def forwards(self, orm):
12 model_table = qn(model._meta.db_table)
13 for tag in orm.Tag.objects.exclude(user=None):
15 SELECT COUNT(%(model_pk)s) -- count books
16 FROM %(model)s, %(tagged_item)s -- from books x tagged
18 %(tagged_item)s.tag_id=%(tag_id)s -- get only the shelf
19 AND %(model_pk)s = %(tagged_item)s.object_id -- get only books on the shelf
20 GROUP BY %(tagged_item)s.tag_id""" % {
21 'model_pk': '%s.%s' % (model_table, qn(model._meta.pk.column)),
23 'tagged_item': qn(orm.TagRelation._meta.db_table),
27 cursor = connection.cursor()
29 book_count = (cursor.fetchone() or (0,))[0]
31 tag.book_count = book_count
34 def backwards(self, orm):
35 "Write your backwards methods here."
39 'Meta': {'object_name': 'Group'},
40 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
41 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
42 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'blank': 'True'})
45 'Meta': {'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
46 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
47 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
48 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
49 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
52 'Meta': {'object_name': 'User'},
53 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
54 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
55 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
56 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'blank': 'True'}),
57 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
58 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
59 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
60 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
61 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
62 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
63 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
64 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'blank': 'True'}),
65 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
68 'Meta': {'object_name': 'Book'},
69 '_short_html': ('django.db.models.fields.TextField', [], {}),
70 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
71 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
72 'extra_info': ('catalogue.fields.JSONField', [], {}),
73 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}),
74 'html_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}),
75 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
76 'mp3_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}),
77 'odt_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}),
78 'ogg_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}),
79 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['catalogue.Book']"}),
80 'parent_number': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
81 'pdf_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}),
82 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}),
83 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}),
84 'txt_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}),
85 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}),
86 'xml_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'})
88 'catalogue.bookstub': {
89 'Meta': {'object_name': 'BookStub'},
90 'author': ('django.db.models.fields.CharField', [], {'max_length': '120'}),
91 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
92 'pd': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
93 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}),
94 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}),
95 'translator': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
96 'translator_death': ('django.db.models.fields.TextField', [], {'blank': 'True'})
98 'catalogue.fragment': {
99 'Meta': {'object_name': 'Fragment'},
100 '_short_html': ('django.db.models.fields.TextField', [], {}),
101 'anchor': ('django.db.models.fields.CharField', [], {'max_length': '120'}),
102 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'fragments'", 'to': "orm['catalogue.Book']"}),
103 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
104 'short_text': ('django.db.models.fields.TextField', [], {}),
105 'text': ('django.db.models.fields.TextField', [], {})
108 'Meta': {'object_name': 'Tag'},
109 'book_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
110 'category': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}),
111 'death': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
112 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
113 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}),
114 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
115 'main_page': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True', 'blank': 'True'}),
116 'name': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}),
117 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}),
118 'sort_key': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}),
119 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
120 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'})
122 'catalogue.tagrelation': {
123 'Meta': {'unique_together': "(('tag', 'content_type', 'object_id'),)", 'object_name': 'TagRelation', 'db_table': "'catalogue_tag_relation'"},
124 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
125 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
126 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
127 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'items'", 'to': "orm['catalogue.Tag']"})
129 'contenttypes.contenttype': {
130 'Meta': {'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
131 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
132 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
133 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
134 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
138 complete_apps = ['catalogue']