diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2013-04-23 15:09:06 -0700 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2013-04-23 15:09:06 -0700 |
commit | b2c21e00e4155480d18f00c9e44e4a9e96ad1604 (patch) | |
tree | b5775039dc45a92ec449171788d4095ab9b3d658 | |
parent | 909b1786a1b0d42e7cb39cf6b18fffe2bc020652 (diff) | |
parent | 81af591ee3a3517caa2b70a85f526084385d2474 (diff) | |
download | askbot-b2c21e00e4155480d18f00c9e44e4a9e96ad1604.tar.gz askbot-b2c21e00e4155480d18f00c9e44e4a9e96ad1604.tar.bz2 askbot-b2c21e00e4155480d18f00c9e44e4a9e96ad1604.zip |
Merge pull request #120 from dedeler/customize-answer-view
answer view is more customizable now
-rw-r--r-- | askbot/forms.py | 16 | ||||
-rw-r--r-- | askbot/views/writers.py | 37 |
2 files changed, 28 insertions, 25 deletions
diff --git a/askbot/forms.py b/askbot/forms.py index e214e908..1eaf3cbe 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -1,6 +1,7 @@ """Forms, custom form fields and related utility functions used in AskBot""" import re +import datetime from django import forms from askbot import const from askbot.const import message_keys @@ -1131,6 +1132,21 @@ class AnswerForm(PostAsSomeoneForm, PostPrivatelyForm): self.fields['email_notify'].widget.attrs['id'] = \ 'question-subscribe-updates' + #People can override this function to save their additional fields to db + def save(self, question, user): + wiki = self.cleaned_data['wiki'], + text = self.cleaned_data['text'] + follow = self.cleaned_data['email_notify'] + is_private = self.cleaned_data['post_privately'] + + return user.post_answer( + question = question, + body_text = text, + follow = follow, + wiki = wiki, + is_private = is_private, + timestamp = datetime.datetime.now(), + ) class VoteForm(forms.Form): """form used in ajax vote view (only comment_upvote so far) diff --git a/askbot/views/writers.py b/askbot/views/writers.py index ac5dc8ef..bec059ef 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -575,7 +575,7 @@ def edit_answer(request, id): #todo: rename this function to post_new_answer @decorators.check_authorization_to_post(ugettext_lazy('Please log in to answer questions')) @decorators.check_spam('text') -def answer(request, id):#process a new answer +def answer(request, id, form_class=forms.AnswerForm):#process a new answer """view that posts new answer anonymous users post into anonymous storage @@ -586,19 +586,17 @@ def answer(request, id):#process a new answer question = get_object_or_404(models.Post, post_type='question', id=id) if request.method == "POST": - custom_class_path = getattr(settings, 'ASKBOT_NEW_ANSWER_FORM', None) - if custom_class_path: - form_class = load_module(custom_class_path) - else: - form_class = forms.AnswerForm - + #this check prevents backward compatilibility + if form_class == forms.AnswerForm: + custom_class_path = getattr(settings, 'ASKBOT_NEW_ANSWER_FORM', None) + if custom_class_path: + form_class = load_module(custom_class_path) + else: + form_class = forms.AnswerForm + form = form_class(request.POST, user=request.user) if form.is_valid(): - wiki = form.cleaned_data['wiki'] - text = form.cleaned_data['text'] - update_time = datetime.datetime.now() - if request.user.is_authenticated(): drafts = models.DraftAnswer.objects.filter( author=request.user, @@ -606,19 +604,8 @@ def answer(request, id):#process a new answer ) drafts.delete() try: - follow = form.cleaned_data['email_notify'] - is_private = form.cleaned_data['post_privately'] - user = form.get_post_user(request.user) - - answer = user.post_answer( - question = question, - body_text = text, - follow = follow, - wiki = wiki, - is_private = is_private, - timestamp = update_time, - ) + answer = form.save(question, user) signals.new_answer_posted.send(None, answer=answer, @@ -637,8 +624,8 @@ def answer(request, id):#process a new answer request.session.flush() models.AnonymousAnswer.objects.create( question=question, - wiki=wiki, - text=text, + wiki=form.cleaned_data['wiki'], + text=form.cleaned_data['text'], session_key=request.session.session_key, ip_addr=request.META['REMOTE_ADDR'], ) |