From 9e6221283bde3f3d4b4115cd725fb4145996d53c Mon Sep 17 00:00:00 2001 From: Tomasz Zielinski Date: Thu, 29 Dec 2011 14:17:37 +0100 Subject: Further Users views migration; cleanup before major migration to Post --- askbot/models/__init__.py | 14 +- askbot/models/base.py | 31 -- askbot/models/meta.py | 21 +- askbot/models/question.py | 2 +- askbot/models/tag.py | 7 +- .../templates/user_profile/user_recent.html | 34 +- .../default/templates/user_profile/user_stats.html | 2 +- askbot/tests/misc_tests.py | 47 -- askbot/views/commands.py | 116 ++--- askbot/views/users.py | 543 ++++++--------------- 10 files changed, 265 insertions(+), 552 deletions(-) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 7d251815..7fff58fd 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -312,7 +312,7 @@ def _assert_user_can( raise django_exceptions.PermissionDenied(error_message) def user_assert_can_unaccept_best_answer(self, answer = None): - assert(isinstance(answer, Answer)) + assert getattr(answer, 'post_type', '') == 'answer' blocked_error_message = _( 'Sorry, you cannot accept or unaccept best answers ' 'because your account is blocked' @@ -368,7 +368,7 @@ def user_assert_can_unaccept_best_answer(self, answer = None): raise django_exceptions.PermissionDenied(error_message) def user_assert_can_accept_best_answer(self, answer = None): - assert(isinstance(answer, Answer)) + assert getattr(answer, 'post_type', '') == 'answer' self.assert_can_unaccept_best_answer(answer) def user_assert_can_vote_for_post( @@ -601,12 +601,12 @@ def user_assert_can_edit_post(self, post = None): def user_assert_can_edit_question(self, question = None): - assert isinstance(question, Post) and question.post_type=='question' + assert getattr(question, 'post_type', '') == 'question' self.assert_can_edit_post(question) def user_assert_can_edit_answer(self, answer = None): - assert isinstance(answer, Post) and answer.post_type=='answer' + assert getattr(answer, 'post_type', '') == 'answer' self.assert_can_edit_post(answer) @@ -953,10 +953,12 @@ def user_post_comment( comment = body_text, added_at = timestamp, ) + if hasattr(comment, 'self_comment'): # Handle Post-s + comment = comment.self_comment award_badges_signal.send(None, event = 'post_comment', actor = self, - context_object = comment.self_comment, + context_object = comment, timestamp = timestamp ) return comment @@ -1407,7 +1409,7 @@ def user_post_answer( self.assert_can_post_answer() - if not isinstance(question, Post) or not question.post_type == 'question': + if getattr(question, 'post_type', '') != 'question': raise TypeError('question argument must be provided') if body_text is None: raise ValueError('Body text is required to post answer') diff --git a/askbot/models/base.py b/askbot/models/base.py index 5ac2ac7a..121b0182 100644 --- a/askbot/models/base.py +++ b/askbot/models/base.py @@ -5,8 +5,6 @@ import logging from django.db import models from django.utils.html import strip_tags from django.contrib.auth.models import User -from django.contrib.contenttypes import generic -from django.contrib.contenttypes.models import ContentType from django.contrib.sitemaps import ping_google #todo: maybe merge askbot.utils.markup and forum.utils.html @@ -188,35 +186,6 @@ class BaseQuerySetManager(models.Manager): except AttributeError: return getattr(self.get_query_set(), attr, *args) -class UserContent(models.Model): - user = models.ForeignKey(User, related_name='%(class)ss') - - class Meta: - abstract = True - app_label = 'askbot' - - -class MetaContent(models.Model): - """ - Base class for Vote and Comment - """ - content_type = models.ForeignKey(ContentType) - object_id = models.PositiveIntegerField() - content_object = generic.GenericForeignKey('content_type', 'object_id') - - class Meta: - abstract = True - app_label = 'askbot' - -class DeletableContent(models.Model): - deleted = models.BooleanField(default=False) - deleted_at = models.DateTimeField(null=True, blank=True) - deleted_by = models.ForeignKey(User, null=True, blank=True, related_name='deleted_%(class)ss') - - class Meta: - abstract = True - app_label = 'askbot' - class AnonymousContent(models.Model): """ diff --git a/askbot/models/meta.py b/askbot/models/meta.py index 0a4fb349..6fc1e70d 100644 --- a/askbot/models/meta.py +++ b/askbot/models/meta.py @@ -1,4 +1,5 @@ import datetime +from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from django.db import models from django.utils import html as html_utils @@ -29,21 +30,26 @@ class VoteManager(models.Manager): return 0 -class Vote(base.MetaContent, base.UserContent): +class Vote(base.UserContent): VOTE_UP = +1 VOTE_DOWN = -1 VOTE_CHOICES = ( (VOTE_UP, u'Up'), (VOTE_DOWN, u'Down'), ) + user = models.ForeignKey('auth.User', related_name='votes') + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') vote = models.SmallIntegerField(choices=VOTE_CHOICES) voted_at = models.DateTimeField(default=datetime.datetime.now) objects = VoteManager() - class Meta(base.MetaContent.Meta): + class Meta: unique_together = ('content_type', 'object_id', 'user') + app_label = 'askbot' db_table = u'vote' def __unicode__(self): @@ -86,7 +92,7 @@ class Vote(base.MetaContent, base.UserContent): #todo: move this class to content -class Comment(base.MetaContent, base.UserContent): +class Comment(base.UserContent): post_type = 'comment' comment = models.CharField(max_length = const.COMMENT_HARD_MAX_LENGTH) added_at = models.DateTimeField(default = datetime.datetime.now) @@ -94,13 +100,20 @@ class Comment(base.MetaContent, base.UserContent): score = models.IntegerField(default = 0) offensive_flag_count = models.IntegerField(default = 0) + user = models.ForeignKey('auth.User', related_name='comments') + + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') + _urlize = True _use_markdown = True _escape_html = True is_anonymous = False #comments are never anonymous - may change - class Meta(base.MetaContent.Meta): + class Meta: ordering = ('-added_at',) + app_label = 'askbot' db_table = u'comment' #these two are methods diff --git a/askbot/models/question.py b/askbot/models/question.py index d0a30fcc..e9b732f6 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -919,7 +919,7 @@ class QuestionView(models.Model): class FavoriteQuestion(models.Model): """A favorite Question of a User.""" thread = models.ForeignKey(Thread) - user = models.ForeignKey(User, related_name='unused_user_favorite_questions') + user = models.ForeignKey(User, related_name='user_favorite_questions') added_at = models.DateTimeField(default=datetime.datetime.now) class Meta: diff --git a/askbot/models/tag.py b/askbot/models/tag.py index 7bd49d12..349b409b 100644 --- a/askbot/models/tag.py +++ b/askbot/models/tag.py @@ -124,9 +124,14 @@ class Tag(DeletableContent): # Denormalised data used_count = models.PositiveIntegerField(default=0) + deleted = models.BooleanField(default=False) + deleted_at = models.DateTimeField(null=True, blank=True) + deleted_by = models.ForeignKey(User, null=True, blank=True, related_name='deleted_tags') + objects = TagManager() - class Meta(DeletableContent.Meta): + class Meta: + app_label = 'askbot' db_table = u'tag' ordering = ('-used_count', 'name') diff --git a/askbot/skins/default/templates/user_profile/user_recent.html b/askbot/skins/default/templates/user_profile/user_recent.html index 84d10357..09689419 100644 --- a/askbot/skins/default/templates/user_profile/user_recent.html +++ b/askbot/skins/default/templates/user_profile/user_recent.html @@ -13,25 +13,23 @@
{% if act.is_badge %} -  {% trans name=act.badge.name %}{{name}}{% endtrans %} - {% if act.related_object_type == 'question' %}{# question #} - {% for question in questions %}{# could also create a new dict #} - {% if question.question_id == act.obj %} - ({% trans %}source{% endtrans %}) - {% endif %} - {% endfor %} - {% elif act.related_object_type == 'answer' %}{# answer #} - {% for answer in answers %}{# could also create a new dict #} - {% if answer.answer_id == act.obj %} - ({% trans %}source{% endtrans %}) - {% endif %} - {% endfor %} - {% endif %} + +  {% trans name=act.badge.name %}{{name}}{% endtrans %} + + {% if act.content_object.post_type == 'question' %} + {% set question=act.content_object %} + ({% trans %}source{% endtrans %}) + {% elif act.content_object.post_type == 'answer' %} + {% set answer=act.content_object %} + ({% trans %}source{% endtrans %}) + {% endif %} {% else %} - {{ act.title|escape }} - {% if act.summary %}{{ act.summary|escape }}{% endif %} + {{ act.title|escape }} + {% if act.summary %}{{ act.summary|escape }}{% endif %} {% endif %}
diff --git a/askbot/skins/default/templates/user_profile/user_stats.html b/askbot/skins/default/templates/user_profile/user_stats.html index 0691dbad..95619560 100644 --- a/askbot/skins/default/templates/user_profile/user_stats.html +++ b/askbot/skins/default/templates/user_profile/user_stats.html @@ -114,7 +114,7 @@