From 270d49573b9c7ac889a76b9de2dc4aca70470381 Mon Sep 17 00:00:00 2001 From: Tomasz Zielinski Date: Sat, 31 Dec 2011 15:04:30 +0100 Subject: Updated migrations to work around a strange issue with GFkeys<->South interactions Possibly this is due the recurring problem with contenttypes occasionally changing their IDs --- askbot/migrations/0090_postize_q_a_c.py | 20 +++++++------- .../migrations/0092_postize_vote_and_activity.py | 32 +++++++++------------- askbot/migrations/0095_postize_award_and_repute.py | 19 +++---------- 3 files changed, 27 insertions(+), 44 deletions(-) diff --git a/askbot/migrations/0090_postize_q_a_c.py b/askbot/migrations/0090_postize_q_a_c.py index aafaf4dc..42927d85 100644 --- a/askbot/migrations/0090_postize_q_a_c.py +++ b/askbot/migrations/0090_postize_q_a_c.py @@ -1,6 +1,5 @@ # encoding: utf-8 import datetime -from django.contrib.contenttypes import generic from south.db import db from south.v2 import DataMigration from django.db import models @@ -78,16 +77,17 @@ class Migration(DataMigration): is_anonymous=ans.is_anonymous, ) - gfk = generic.GenericForeignKey('content_type', 'object_id') - gfk.contribute_to_class(orm.Comment, 'content_object') - for cm in orm.Comment.objects.all(): - if cm.content_object.post_type == 'question': - thread = orm.Thread.objects.get(id=cm.content_object.thread.id) # conver from Thread to orm.Thread - a subtle difference - parent = orm.Post.objects.get(self_question=cm.content_object) - elif cm.content_object.post_type == 'answer': - thread = orm.Thread.objects.get(id=cm.content_object.question.thread.id) # conver from Thread to orm.Thread - a subtle difference - parent = orm.Post.objects.get(self_answer=cm.content_object) + # Workaround for a strange issue with: http://south.aeracode.org/docs/generics.html + # No need to investigate that as this is as simple as the "proper" way + if (cm.content_type.app_label, cm.content_type.model) == ('askbot', 'question'): + content_object = orm.Question.objects.get(id=cm.object_id) + thread = orm.Thread.objects.get(id=content_object.thread.id) # conver from Thread to orm.Thread - a subtle difference + parent = orm.Post.objects.get(self_question=content_object) + elif (cm.content_type.app_label, cm.content_type.model) == ('askbot', 'answer'): + content_object = orm.Answer.objects.get(id=cm.object_id) + thread = orm.Thread.objects.get(id=content_object.question.thread.id) # conver from Thread to orm.Thread - a subtle difference + parent = orm.Post.objects.get(self_answer=content_object) else: raise ValueError('comment.content_object is neither question nor answer!') diff --git a/askbot/migrations/0092_postize_vote_and_activity.py b/askbot/migrations/0092_postize_vote_and_activity.py index 4e4f3a6c..d292330e 100644 --- a/askbot/migrations/0092_postize_vote_and_activity.py +++ b/askbot/migrations/0092_postize_vote_and_activity.py @@ -1,6 +1,5 @@ # encoding: utf-8 import datetime -from django.contrib.contenttypes import generic from south.db import db from south.v2 import DataMigration from django.db import models @@ -8,17 +7,13 @@ from django.db import models class Migration(DataMigration): def forwards(self, orm): - gfk = generic.GenericForeignKey('content_type', 'object_id') - gfk.contribute_to_class(orm.Vote, 'content_object') - for v in orm.Vote.objects.all(): - # note that isinstance() doesn't work with orm.Model and GFKey! - if getattr(v.content_object, 'post_type', '') == 'question': - v.voted_post = orm.Post.objects.get(self_question__id=v.content_object.id) - elif getattr(v.content_object, 'post_type', '') == 'answer': - v.voted_post = orm.Post.objects.get(self_answer__id=v.content_object.id) - elif getattr(v.content_object, 'post_type', '') == 'comment': - v.voted_post = orm.Post.objects.get(self_comment__id=v.content_object.id) + if (v.content_type.app_label, v.content_type.model) == ('askbot', 'question'): + v.voted_post = orm.Post.objects.get(self_question__id=v.object_id) + elif (v.content_type.app_label, v.content_type.model) == ('askbot', 'answer'): + v.voted_post = orm.Post.objects.get(self_answer__id=v.object_id) + elif (v.content_type.app_label, v.content_type.model) == ('askbot', 'comment'): + v.voted_post = orm.Post.objects.get(self_comment__id=v.object_id) else: raise ValueError('Unknown vote subject!') v.save() @@ -27,16 +22,15 @@ class Migration(DataMigration): for a in orm.Activity.objects.all(): save = False - - # note that isinstance() doesn't work with orm.Model and GFKey! - if getattr(a.content_object, 'post_type', '') == 'question': - a.content_object = orm.Post.objects.get(self_question__id=a.content_object.id) + + if (a.content_type.app_label, a.content_type.model) == ('askbot', 'question'): + a.content_object = orm.Post.objects.get(self_question__id=a.object_id) save = True - elif getattr(a.content_object, 'post_type', '') == 'answer': - a.content_object = orm.Post.objects.get(self_answer__id=a.content_object.id) + elif (a.content_type.app_label, a.content_type.model) == ('askbot', 'answer'): + a.content_object = orm.Post.objects.get(self_answer__id=a.object_id) save = True - elif getattr(a.content_object, 'post_type', '') == 'comment': - a.content_object = orm.Post.objects.get(self_comment__id=a.content_object.id) + elif (a.content_type.app_label, a.content_type.model) == ('askbot', 'comment'): + a.content_object = orm.Post.objects.get(self_comment__id=a.object_id) save = True if a.question: diff --git a/askbot/migrations/0095_postize_award_and_repute.py b/askbot/migrations/0095_postize_award_and_repute.py index 25a31496..3afb459f 100644 --- a/askbot/migrations/0095_postize_award_and_repute.py +++ b/askbot/migrations/0095_postize_award_and_repute.py @@ -1,6 +1,5 @@ # encoding: utf-8 import datetime -from django.contrib.contenttypes import generic from south.db import db from south.v2 import DataMigration from django.db import models @@ -8,10 +7,6 @@ from django.db import models class Migration(DataMigration): def forwards(self, orm): - # Note that objects fetched from GFKeys are not compliant with orm.Model instances! - gfk = generic.GenericForeignKey('content_type', 'object_id') - gfk.contribute_to_class(orm.Award, 'content_object') - ct_question = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='question') ct_answer = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='answer') ct_comment = orm['contenttypes.ContentType'].objects.get(app_label='askbot', model='comment') @@ -20,19 +15,13 @@ class Migration(DataMigration): for aw in orm.Award.objects.all(): ct = aw.content_type - obj = aw.content_object + aw.content_type = ct_post if ct == ct_question: - assert getattr(obj, 'post_type', '') == 'question' - aw.content_type = ct_post - aw.object_id = orm.Post.objects.get(self_question__id=obj.id).id + aw.object_id = orm.Post.objects.get(self_question__id=aw.object_id).id elif ct == ct_answer: - assert getattr(obj, 'post_type', '') == 'answer' - aw.content_type = ct_post - aw.object_id = orm.Post.objects.get(self_answer__id=obj.id).id + aw.object_id = orm.Post.objects.get(self_answer__id=aw.object_id).id elif ct == ct_comment: - assert getattr(obj, 'post_type', '') == 'comment' - aw.content_type = ct_post - aw.object_id = orm.Post.objects.get(self_comment__id=obj.id).id + aw.object_id = orm.Post.objects.get(self_comment__id=aw.object_id).id else: raise ValueError('Unknown award subject!') aw.save() -- cgit v1.2.3-1-g7c22