From 8b40ebe282f4e24db725a7d63af28314adf3c29a Mon Sep 17 00:00:00 2001 From: Tomasz Zielinski Date: Fri, 9 Dec 2011 15:34:53 +0100 Subject: Tickets 104, 107: Added the unmanaged Post model along with the underlying VIEW SQL --- askbot/models/__init__.py | 3 +- askbot/models/post.py | 33 +++++++++++++- askbot/models/post_view.sql | 107 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 askbot/models/post_view.sql diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 3ba9825e..f4bf165c 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -29,7 +29,7 @@ from askbot.models.answer import Answer, AnonymousAnswer from askbot.models.tag import Tag, MarkedTag from askbot.models.meta import Vote, Comment from askbot.models.user import EmailFeedSetting, ActivityAuditStatus, Activity -from askbot.models.post import PostRevision +from askbot.models.post import Post, PostRevision from askbot.models import signals from askbot.models.badges import award_badges_signal, get_badge, init_badges #from user import AuthKeyUserAssociation @@ -2717,6 +2717,7 @@ __all__ = [ 'Answer', 'AnonymousAnswer', + 'Post', 'PostRevision', 'Tag', diff --git a/askbot/models/post.py b/askbot/models/post.py index fd8fc292..eaa2cb8d 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -3,9 +3,38 @@ from django.core.exceptions import ValidationError from askbot.utils import markup from askbot.utils.html import sanitize_html +from askbot.models import content + + +class Post(content.Content): + post_type = models.CharField(max_length=255) + parent = models.ForeignKey('Post', blank=True, null=True) + self_answer = models.ForeignKey('Answer', blank=True, null=True) + self_question = models.ForeignKey('Question', blank=True, null=True) + thread = models.ForeignKey('Thread') + + class Meta: + app_label = 'askbot' + db_table = 'askbot_post' + managed = False + + def delete(self, *args, **kwargs): + # Redirect the deletion to the relevant Question or Answer instance + # WARNING: This is not called for batch deletions so watch out! + if self.self_answer: + return self.self_answer.delete(*args, **kwargs) + elif self.self_question: + return self.self_question.delete(*args, **kwargs) + raise NotImplementedError + +for field in Post._meta.fields: + if isinstance(field, models.ForeignKey): + # HACK: Patch all foreign keys to not cascade when deleted + # This is required because foreign keys on Post create normal backreferences + # in the destination models, so e.g. deleting User instance would trigger Post instance deletion, + # which is not what should happen. + field.rel.on_delete = models.DO_NOTHING -#class Post(models.Model): -# pass class PostRevisionManager(models.Manager): def create(self, *kargs, **kwargs): diff --git a/askbot/models/post_view.sql b/askbot/models/post_view.sql new file mode 100644 index 00000000..a3487265 --- /dev/null +++ b/askbot/models/post_view.sql @@ -0,0 +1,107 @@ +/* + +SQL for creating a database VIEW for the unmanaged `Post` model + +Tested with: SQLite3 + +*/ + +CREATE VIEW askbot_post AS + +SELECT + answer.id + 1000000 AS id, -- fake unique ID + + /* Some required pseudo-fields */ + "answer" AS post_type, + + joined_question.id AS parent_id, + joined_question.thread_id AS thread_id, + + answer.id AS self_answer_id, + NULL AS self_question_id, + + /* Shared fields from content.Content */ + answer.author_id, + answer.added_at, + + answer.deleted, + answer.deleted_at, + answer.deleted_by_id, + + answer.wiki, + answer.wikified_at, + + answer.locked, + answer.locked_by_id, + answer.locked_at, + + answer.score, + answer.vote_up_count, + answer.vote_down_count, + + answer.comment_count, + answer.offensive_flag_count, + + answer.last_edited_at, + answer.last_edited_by_id, + + answer.html, + answer.text, + + answer.summary, + + answer.is_anonymous + +FROM answer + +INNER JOIN question as joined_question +ON joined_question.id=answer.question_id + +UNION + +SELECT + question.id AS id, -- fake unique ID + + /* Some required pseudo-fields */ + "question" AS post_type, + + NULL AS parent_id, + thread_id, + + NULL AS self_answer_id, + id AS self_question_id, + + /* Shared fields from content.Content */ + author_id, + added_at, + + deleted, + deleted_at, + deleted_by_id, + + wiki, + wikified_at, + + locked, + locked_by_id, + locked_at, + + score, + vote_up_count, + vote_down_count, + + comment_count, + offensive_flag_count, + + last_edited_at, + last_edited_by_id, + + html, + text, + + summary, + + is_anonymous + + +FROM question; \ No newline at end of file -- cgit v1.2.3-1-g7c22