diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-07-30 12:37:58 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-07-30 12:37:58 -0400 |
commit | c1a7d617eb0c96ae97f6b8516f1b64e1e45d9d7d (patch) | |
tree | 237d78ab31ae2ba6dca9bad5b23c5e88e32e4554 /askbot/forms.py | |
parent | 9a489d9acbe38b403750acd292990460083c6850 (diff) | |
parent | 2aac3317ab03ebe1f27796b647ce87679d202c46 (diff) | |
download | askbot-c1a7d617eb0c96ae97f6b8516f1b64e1e45d9d7d.tar.gz askbot-c1a7d617eb0c96ae97f6b8516f1b64e1e45d9d7d.tar.bz2 askbot-c1a7d617eb0c96ae97f6b8516f1b64e1e45d9d7d.zip |
merged with the tag-editor branch
Diffstat (limited to 'askbot/forms.py')
-rw-r--r-- | askbot/forms.py | 99 |
1 files changed, 79 insertions, 20 deletions
diff --git a/askbot/forms.py b/askbot/forms.py index fe65cd02..2cdf1c5d 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -4,6 +4,7 @@ import re from django import forms from askbot import const from askbot.const import message_keys +from django.forms.util import ErrorList from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ungettext_lazy, string_concat from django.utils.text import get_text_list @@ -769,7 +770,80 @@ class DraftAnswerForm(forms.Form): text = forms.CharField(required=False) -class AskForm(PostPrivatelyForm): +class PostAsSomeoneForm(forms.Form): + post_author_username = forms.CharField( + initial=_('User name:'), + help_text=_( + 'Enter name to post on behalf of someone else. ' + 'Can create new accounts.' + ), + required=False, + widget=forms.TextInput(attrs={'class': 'tipped-input'}) + ) + post_author_email = forms.CharField( + initial=_('Email address:'), + required=False, + widget=forms.TextInput(attrs={'class': 'tipped-input'}) + ) + + def get_post_user(self, user): + """returns user on whose behalf the post or a revision + is being made + """ + username = self.cleaned_data['post_author_username'] + email= self.cleaned_data['post_author_email'] + if user.is_administrator() and username and email: + post_user = user.get_or_create_fake_user(username, email) + else: + post_user = user + return post_user + + def clean_post_author_username(self): + """if value is the same as initial, it is reset to + empty string + todo: maybe better to have field where initial value is invalid, + then we would not have to have two almost identical clean functions? + """ + username = self.cleaned_data.get('post_author_username', '') + initial_username = unicode(self.fields['post_author_username'].initial) + if username == initial_username: + self.cleaned_data['post_author_username'] = '' + return self.cleaned_data['post_author_username'] + + def clean_post_author_email(self): + """if value is the same as initial, it is reset to + empty string""" + email = self.cleaned_data.get('post_author_email', '') + initial_email = unicode(self.fields['post_author_email'].initial) + if email == initial_email: + email = '' + if email != '': + email = forms.EmailField().clean(email) + self.cleaned_data['post_author_email'] = email + return email + + def clean(self): + """requires email address if user name is given""" + username = self.cleaned_data.get('post_author_username', '') + email = self.cleaned_data.get('post_author_email', '') + if username == '' and email: + username_errors = self._errors.get( + 'post_author_username', + ErrorList() + ) + username_errors.append(_('User name is required with the email')) + self._errors['post_author_username'] = username_errors + raise forms.ValidationError('missing user name') + elif email == '' and username: + email_errors = self._errors.get('post_author_email', ErrorList()) + email_errors.append(_('Email is required if user name is added')) + self._errors['post_author_email'] = email_errors + raise forms.ValidationError('missing email') + + return self.cleaned_data + + +class AskForm(PostAsSomeoneForm, PostPrivatelyForm): """the form used to askbot questions field ask_anonymously is shown to the user if the if ALLOW_ASK_ANONYMOUSLY live setting is True @@ -794,14 +868,6 @@ class AskForm(PostPrivatelyForm): required=False, max_length=255, widget=forms.TextInput(attrs={'size': 40, 'class': 'openid-input'}) ) - user = forms.CharField( - required=False, max_length=255, - widget=forms.TextInput(attrs={'size': 35}) - ) - email = forms.CharField( - required=False, max_length=255, - widget=forms.TextInput(attrs={'size': 35}) - ) def __init__(self, *args, **kwargs): super(AskForm, self).__init__(*args, **kwargs) @@ -904,21 +970,13 @@ class AskByEmailForm(forms.Form): return self.cleaned_data['subject'] -class AnswerForm(PostPrivatelyForm): +class AnswerForm(PostAsSomeoneForm, PostPrivatelyForm): text = AnswerEditorField() wiki = WikiField() openid = forms.CharField( required=False, max_length=255, widget=forms.TextInput(attrs={'size': 40, 'class': 'openid-input'}) ) - user = forms.CharField( - required=False, max_length=255, - widget=forms.TextInput(attrs={'size': 35}) - ) - email = forms.CharField( - required=False, max_length=255, - widget=forms.TextInput(attrs={'size': 35}) - ) email_notify = EmailNotifyField(initial=False) def __init__(self, *args, **kwargs): @@ -989,7 +1047,7 @@ class RevisionForm(forms.Form): self.fields['revision'].initial = latest_revision.revision -class EditQuestionForm(PostPrivatelyForm): +class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): title = TitleField() text = QuestionEditorField() tags = TagNamesField() @@ -1099,6 +1157,7 @@ class EditQuestionForm(PostPrivatelyForm): field edit_anonymously. It relies on correct cleaning if the "reveal_identity" field """ + super(EditQuestionForm, self).clean() reveal_identity = self.cleaned_data.get('reveal_identity', False) stay_anonymous = False if reveal_identity is False and self.can_stay_anonymous(): @@ -1107,7 +1166,7 @@ class EditQuestionForm(PostPrivatelyForm): return self.cleaned_data -class EditAnswerForm(PostPrivatelyForm): +class EditAnswerForm(PostAsSomeoneForm, PostPrivatelyForm): text = AnswerEditorField() summary = SummaryField() wiki = WikiField() |