From 7b14a2037436b24da70203fac3ee3ee1e48b114b Mon Sep 17 00:00:00 2001 From: Tomasz Zielinski Date: Tue, 27 Dec 2011 18:19:21 +0100 Subject: Updated writers views to use Post wherever possible; Sql VIEW update --- askbot/forms.py | 2 +- askbot/models/__init__.py | 34 +++++-- askbot/models/content.py | 10 +- askbot/models/post.py | 46 +++++++++ askbot/models/post_view.sql | 4 +- askbot/skins/default/templates/answer_edit.html | 2 +- askbot/skins/default/templates/question.html | 34 +++---- askbot/skins/default/templates/question_edit.html | 2 +- askbot/views/readers.py | 7 +- askbot/views/writers.py | 119 ++++++++++------------ 10 files changed, 151 insertions(+), 109 deletions(-) diff --git a/askbot/forms.py b/askbot/forms.py index f1b43f3c..a0e5540a 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -754,7 +754,7 @@ class RevisionForm(forms.Form): def __init__(self, post, latest_revision, *args, **kwargs): super(RevisionForm, self).__init__(*args, **kwargs) - revisions = post.revisions.all().values_list( + revisions = post.revisions().values_list( 'revision', 'author__username', 'revised_at', 'summary') date_format = '%c' self.fields['revision'].choices = [ diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index f4bf165c..7d251815 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -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, Question)) + assert isinstance(question, Post) and question.post_type=='question' self.assert_can_edit_post(question) def user_assert_can_edit_answer(self, answer = None): - assert(isinstance(answer, Answer)) + assert isinstance(answer, Post) and answer.post_type=='answer' self.assert_can_edit_post(answer) @@ -956,7 +956,7 @@ def user_post_comment( award_badges_signal.send(None, event = 'post_comment', actor = self, - context_object = comment, + context_object = comment.self_comment, timestamp = timestamp ) return comment @@ -1289,13 +1289,17 @@ def user_post_question( # because they set some attributes for that instance and expect them to be changed also for question.author return question -def user_edit_comment(self, comment = None, body_text = None): +def user_edit_comment(self, comment_post=None, body_text = None): """apply edit to a comment, the method does not change the comments timestamp and no signals are sent """ - self.assert_can_edit_comment(comment) - comment.comment = body_text - comment.parse_and_save(author = self) + self.assert_can_edit_comment(comment_post) + comment_post.self_comment.comment = body_text + comment_post.self_comment.parse_and_save(author = self) + + # HACK: We have to update this `comment_post` instance to reflect comment changes + comment_post.text = comment_post.self_comment.comment + comment_post.html = comment_post.self_comment.html @auto_now_timestamp def user_edit_question( @@ -1403,13 +1407,21 @@ def user_post_answer( self.assert_can_post_answer() - if not isinstance(question, Question): + if not isinstance(question, Post) or not 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') if timestamp is None: timestamp = datetime.datetime.now() - answer = Answer.objects.create_new( +# answer = Answer.objects.create_new( +# thread = question.thread, +# author = self, +# text = body_text, +# added_at = timestamp, +# email_notify = follow, +# wiki = wiki +# ) + answer_post = Post.objects.create_new_answer( thread = question.thread, author = self, text = body_text, @@ -1420,9 +1432,9 @@ def user_post_answer( award_badges_signal.send(None, event = 'post_answer', actor = self, - context_object = answer + context_object = answer_post.self_answer ) - return answer + return answer_post def user_visit_question(self, question = None, timestamp = None): """create a QuestionView record diff --git a/askbot/models/content.py b/askbot/models/content.py index d877de7e..30abd50c 100644 --- a/askbot/models/content.py +++ b/askbot/models/content.py @@ -721,8 +721,12 @@ class Content(models.Model): if latest_revision.tagnames != tags: self.thread.update_tags(tagnames = tags, user = edited_by, timestamp = edited_at) + self.thread.title = title + self.thread.tagnames = tags + self.thread.save() + # Create a new revision - self.add_revision( + self.add_revision( # has to be called AFTER updating the thread, otherwise it doesn't see new tags and the new title author = edited_by, text = text, revised_at = edited_at, @@ -732,10 +736,6 @@ class Content(models.Model): self.parse_and_save(author = edited_by) - self.thread.title = title - self.thread.tagnames = tags - self.thread.save() - self.thread.set_last_activity(last_activity_at=edited_at, last_activity_by=edited_by) def apply_edit(self, *kargs, **kwargs): diff --git a/askbot/models/post.py b/askbot/models/post.py index 1526e448..d86d7fbb 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -14,6 +14,11 @@ class PostManager(models.Manager): def get_answers(self): return self.filter(post_type='answer') + def create_new_answer(self, *args, **kwargs): + from askbot.models import Answer + answer = Answer.objects.create_new(*args, **kwargs) + return self.get(self_answer=answer) + class Post(content.Content): post_type = models.CharField(max_length=255) @@ -87,6 +92,47 @@ class Post(content.Content): order_number += 1 return int(order_number/const.ANSWERS_PAGE_SIZE) + 1 + def revisions(self): + if self.self_question: + return self.self_question.revisions.all() + elif self.self_answer: + return self.self_answer.revisions.all() + raise NotImplementedError + + def get_latest_revision(self): + return self.revisions().order_by('-revised_at')[0] + + def apply_edit(self, *kargs, **kwargs): + post = (self.self_answer or self.self_question) + if not post: + raise NotImplementedError + return post.apply_edit(*kargs, **kwargs) + + def add_comment(self, *args, **kwargs): + if self.self_question: + comment = self.self_question.add_comment(*args, **kwargs) + elif self.self_answer: + comment = self.self_answer.add_comment(*args, **kwargs) + else: + raise NotImplementedError + return self.__class__.objects.get(self_comment=comment) + + def get_comments(self, *args, **kwargs): + if self.self_question: + comments = self.self_question.get_comments(*args, **kwargs) + elif self.self_answer: + comments = self.self_answer.get_comments(*args, **kwargs) + else: + raise NotImplementedError + # TODO: Convert `comments` to Post-s (there are lots of dependencies, though) + return comments + + def accepted(self): + if self.is_answer(): + return self.question.thread.accepted_answer == self + raise NotImplementedError + + for field in Post._meta.fields: if isinstance(field, models.ForeignKey): diff --git a/askbot/models/post_view.sql b/askbot/models/post_view.sql index 2abac4a9..c55b4027 100644 --- a/askbot/models/post_view.sql +++ b/askbot/models/post_view.sql @@ -172,7 +172,7 @@ SELECT NULL AS last_edited_by_id, comment.html, - NULL AS text, + comment.comment AS text, '' AS summary, @@ -235,7 +235,7 @@ SELECT NULL AS last_edited_by_id, comment.html, - NULL AS text, + comment.comment AS text, '' AS summary, diff --git a/askbot/skins/default/templates/answer_edit.html b/askbot/skins/default/templates/answer_edit.html index bbf3edef..83c952c8 100644 --- a/askbot/skins/default/templates/answer_edit.html +++ b/askbot/skins/default/templates/answer_edit.html @@ -10,7 +10,7 @@ {% trans %}Edit answer{% endtrans %} [{% trans %}back{% endtrans %}]
-
{% csrf_token %} + {% csrf_token %}
{% if revision_form.revision.errors %}{{ revision_form.revision.errors.as_ul() }}{% endif %}
diff --git a/askbot/skins/default/templates/question.html b/askbot/skins/default/templates/question.html index 5e2c7a33..518ebf29 100644 --- a/askbot/skins/default/templates/question.html +++ b/askbot/skins/default/templates/question.html @@ -159,28 +159,20 @@
{# ==== START: question/answer_vote_buttons.html ==== #} {{ macros.post_vote_buttons(post = answer.self_answer, visitor_vote = user_answer_votes[answer.self_answer_id]) }} - {% if request.user == question.author or (request.user.is_authenticated() and (request.user.is_moderator() or request.user.is_administrator())) %} - {% trans %}mark this answer as correct (click again to undo){% endtrans %} - {% else %} - {% if answer.accepted() %} - {% trans question_author=question.author.username %}{{question_author}} has selected this answer as correct{% endtrans %} - {% endif %} - {% endif %} + title="{% trans %}mark this answer as correct (click again to undo){% endtrans %}" + {% else %} + alt="{% trans question_author=question.author.username %}{{question_author}} has selected this answer as correct{% endtrans %}" + title="{% trans questsion_author=question.author.username%}{{question_author}} has selected this answer as correct{% endtrans %}" + {% endif %} + /> {# ==== END: question/answer_vote_buttons.html ==== #}
diff --git a/askbot/skins/default/templates/question_edit.html b/askbot/skins/default/templates/question_edit.html index 47873e0e..bc064467 100644 --- a/askbot/skins/default/templates/question_edit.html +++ b/askbot/skins/default/templates/question_edit.html @@ -7,7 +7,7 @@ {% endblock %} {% block content %}
{% trans %}Edit question{% endtrans %} [{% trans %}back{% endtrans %}]
-{% csrf_token %} +{% csrf_token %} {% if revision_form.revision.errors %}{{ revision_form.revision.errors.as_ul() }}{% endif %}
diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 01e618c2..c7fea431 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -583,6 +583,7 @@ def revisions(request, id, object_name=None): revision.summary = _('initial version') else: revision.diff = htmldiff(revisions[i-1].html, revision.html) + data = { 'page_class':'revisions-page', 'active_tab':'questions', @@ -600,9 +601,9 @@ def get_comment(request): and request must be ajax """ id = int(request.GET['id']) - comment = models.Post.objects.get(post_type='comment', id=id) - request.user.assert_can_edit_comment(comment.self_comment) - return {'text': comment.self_comment.comment} + comment = models.Post.objects.get(post_type='comment', self_comment=id) + request.user.assert_can_edit_comment(comment) + return {'text': comment.text} @ajax_only @get_only diff --git a/askbot/views/writers.py b/askbot/views/writers.py index 19ec57d9..dab32d2a 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -344,7 +344,7 @@ def retag_question(request, id): def edit_question(request, id): """edit question view """ - question = get_object_or_404(models.Question, id = id) + question = get_object_or_404(models.Post, self_question=id) latest_revision = question.get_latest_revision() revision_form = None try: @@ -393,6 +393,7 @@ def edit_question(request, id): is_anon_edit = form.cleaned_data['stay_anonymous'] is_wiki = form.cleaned_data.get('wiki', question.wiki) + request.user.edit_question( question = question, title = form.cleaned_data['title'], @@ -430,7 +431,8 @@ def edit_question(request, id): @csrf.csrf_protect @decorators.check_spam('text') def edit_answer(request, id): - answer = get_object_or_404(models.Answer, id=id) + answer = get_object_or_404(models.Post, self_answer=id) + latest_revision = answer.get_latest_revision() try: request.user.assert_can_edit_answer(answer) latest_revision = answer.get_latest_revision() @@ -496,7 +498,7 @@ def answer(request, id):#process a new answer authenticated users post directly """ - question = get_object_or_404(models.Question, id=id) + question = get_object_or_404(models.Post, post_type='question', self_question=id) if request.method == "POST": form = forms.AnswerForm(question, request.user, request.POST) if form.is_valid(): @@ -540,7 +542,7 @@ def __generate_comments_json(obj, user):#non-view generates json data for the po json_comments = [] for comment in comments: - if user != None and user.is_authenticated(): + if user and user.is_authenticated(): try: user.assert_can_delete_comment(comment) #/posts/392845/comments/219852/delete @@ -575,73 +577,62 @@ def __generate_comments_json(obj, user):#non-view generates json data for the po @decorators.check_spam('comment') def post_comments(request):#generic ajax handler to load comments to an object # only support get post comments by ajax now + + post_type = request.REQUEST.get('post_type', '') + if not request.is_ajax() or post_type not in ('question', 'answer'): + raise Http404 # TODO: Shouldn't be 404! More like 400, 403 or sth more specific + user = request.user - if request.is_ajax(): - post_type = request.REQUEST['post_type'] - id = request.REQUEST['post_id'] - if post_type == 'question': - post_model = models.Question - elif post_type == 'answer': - post_model = models.Answer - else: - raise Http404 - obj = get_object_or_404(post_model, id=id) - if request.method == "GET": + id = request.REQUEST['post_id'] + if post_type == 'question': + obj = get_object_or_404(models.Post, self_question=id) + else: #if post_type == 'answer': + obj = get_object_or_404(models.Post, self_answer=id) + + if request.method == "GET": + response = __generate_comments_json(obj, user) + elif request.method == "POST": + try: + if user.is_anonymous(): + msg = _('Sorry, you appear to be logged out and ' + 'cannot post comments. Please ' + 'sign in.') % \ + {'sign_in_url': url_utils.get_login_url()} + raise exceptions.PermissionDenied(msg) + user.post_comment(parent_post=obj, body_text=request.POST.get('comment')) response = __generate_comments_json(obj, user) - elif request.method == "POST": - try: - if user.is_anonymous(): - msg = _('Sorry, you appear to be logged out and ' - 'cannot post comments. Please ' - 'sign in.') % \ - {'sign_in_url': url_utils.get_login_url()} - raise exceptions.PermissionDenied(msg) - user.post_comment( - parent_post = obj, - body_text = request.POST.get('comment') - ) - response = __generate_comments_json(obj, user) - except exceptions.PermissionDenied, e: - response = HttpResponseForbidden( - unicode(e), - mimetype="application/json" - ) - return response - else: - raise Http404 + except exceptions.PermissionDenied, e: + response = HttpResponseForbidden(unicode(e), mimetype="application/json") + + return response @decorators.ajax_only @decorators.check_spam('comment') def edit_comment(request): - if request.user.is_authenticated(): - comment_id = int(request.POST['comment_id']) - comment = models.Comment.objects.get(id = comment_id) - - request.user.edit_comment( - comment = comment, - body_text = request.POST['comment'] - ) - - is_deletable = template_filters.can_delete_comment(comment.user, comment) - is_editable = template_filters.can_edit_comment(comment.user, comment) - - return {'id' : comment.id, - 'object_id': comment.content_object.id, - 'comment_age': diff_date(comment.added_at), - 'html': comment.html, - 'user_display_name': comment.user.username, - 'user_url': comment.user.get_profile_url(), - 'user_id': comment.user.id, - 'is_deletable': is_deletable, - 'is_editable': is_editable, - 'score': comment.score, - 'voted': comment.is_upvoted_by(request.user), - } - else: - raise exceptions.PermissionDenied( - _('Sorry, anonymous users cannot edit comments') - ) + if request.user.is_anonymous(): + raise exceptions.PermissionDenied(_('Sorry, anonymous users cannot edit comments')) + + comment_id = int(request.POST['comment_id']) + comment_post = models.Post.objects.get(self_comment=comment_id) + + request.user.edit_comment(comment_post=comment_post, body_text = request.POST['comment']) + + is_deletable = template_filters.can_delete_comment(comment_post.author, comment_post) + is_editable = template_filters.can_edit_comment(comment_post.author, comment_post) + + return {'id' : comment_post.self_comment.id, + 'object_id': comment_post.self_comment.content_object.id, + 'comment_age': diff_date(comment_post.added_at), + 'html': comment_post.html, + 'user_display_name': comment_post.author.username, + 'user_url': comment_post.author.get_profile_url(), + 'user_id': comment_post.author.id, + 'is_deletable': is_deletable, + 'is_editable': is_editable, + 'score': comment_post.score, + 'voted': comment_post.self_comment.is_upvoted_by(request.user), + } def delete_comment(request): """ajax handler to delete comment -- cgit v1.2.3-1-g7c22