]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/forms.py
ALteration of the schema to a single content model. As a bonus there is a complete...
[osqa.git] / forum / forms.py
index c157aa44254792a64217620e65bdb00144d31158..93a4d15b9c586f1bea71975c66687db0b8cb6b4d 100644 (file)
@@ -4,12 +4,12 @@ from django import forms
 from models import *
 from const import *
 from django.utils.translation import ugettext as _
-from django.contrib.auth.models import User
+from forum.models import User
 from django.contrib.contenttypes.models import ContentType
 from django.utils.safestring import mark_safe
 from forum.utils.forms import NextUrlField, UserNameField, SetPasswordForm
-from recaptcha_django import ReCaptchaField
 from django.conf import settings
+from django.contrib.contenttypes.models import ContentType
 import logging
 
 class TitleField(forms.CharField):
@@ -65,11 +65,11 @@ class TagNamesField(forms.CharField):
         list_temp = []
         if len(list) > 5:
             raise forms.ValidationError(_('please use 5 tags or less'))
+
+        tagname_re = re.compile(r'[a-z0-9]+')
         for tag in list:
             if len(tag) > 20:
                 raise forms.ValidationError(_('tags must be shorter than 20 characters'))
-            #take tag regex from settings
-            tagname_re = re.compile(r'[a-z0-9]+')
             if not tagname_re.match(tag):
                 raise forms.ValidationError(_('please use following characters in tags: letters \'a-z\', numbers, and characters \'.-_#\''))
             # only keep one same tag
@@ -114,9 +114,6 @@ class ModerateUserForm(forms.ModelForm):
         model = User
         fields = ('is_approved',)
 
-class NotARobotForm(forms.Form):
-    recaptcha = ReCaptchaField()
-
 class FeedbackForm(forms.Form):
     name = forms.CharField(label=_('Your name:'), required=False)
     email = forms.EmailField(label=_('Email (not shared with anyone):'), required=False)
@@ -129,27 +126,16 @@ class AskForm(forms.Form):
     tags   = TagNamesField()
     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}))
 
 class AnswerForm(forms.Form):
     text   = EditorField()
     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()
-    def __init__(self, question, user, *args, **kwargs):
+
+    def __init__(self, question, *args, **kwargs):
         super(AnswerForm, self).__init__(*args, **kwargs)
-        self.fields['email_notify'].widget.attrs['id'] = 'question-subscribe-updates';
+
         if question.wiki and settings.WIKI_ON:
             self.fields['wiki'].initial = True
-        if user.is_authenticated():
-            if user in question.followed_by.all():
-                self.fields['email_notify'].initial = True
-                return
-        self.fields['email_notify'].initial = False
 
 
 class CloseForm(forms.Form):
@@ -168,15 +154,17 @@ class RevisionForm(forms.Form):
     """
     revision = forms.ChoiceField(widget=forms.Select(attrs={'style' : 'width:520px'}))
 
-    def __init__(self, post, latest_revision, *args, **kwargs):
+    def __init__(self, post, *args, **kwargs):
         super(RevisionForm, self).__init__(*args, **kwargs)
+
         revisions = post.revisions.all().values_list(
             'revision', 'author__username', 'revised_at', 'summary')
         date_format = '%c'
         self.fields['revision'].choices = [
             (r[0], u'%s - %s (%s) %s' % (r[0], r[1], r[2].strftime(date_format), r[3]))
             for r in revisions]
-        self.fields['revision'].initial = latest_revision.revision
+
+        self.fields['revision'].initial = post.active_revision.revision
 
 class EditQuestionForm(forms.Form):
     title  = TitleField()
@@ -184,11 +172,16 @@ class EditQuestionForm(forms.Form):
     tags   = TagNamesField()
     summary = SummaryField()
 
-    def __init__(self, question, revision, *args, **kwargs):
+    def __init__(self, question, revision=None, *args, **kwargs):
         super(EditQuestionForm, self).__init__(*args, **kwargs)
+
+        if revision is None:
+            revision = question.active_revision
+
         self.fields['title'].initial = revision.title
-        self.fields['text'].initial = revision.text
+        self.fields['text'].initial = revision.body
         self.fields['tags'].initial = revision.tagnames
+            
         # Once wiki mode is enabled, it can't be disabled
         if not question.wiki:
             self.fields['wiki'] = WikiField()
@@ -197,9 +190,13 @@ class EditAnswerForm(forms.Form):
     text = EditorField()
     summary = SummaryField()
 
-    def __init__(self, answer, revision, *args, **kwargs):
+    def __init__(self, answer, revision=None, *args, **kwargs):
         super(EditAnswerForm, self).__init__(*args, **kwargs)
-        self.fields['text'].initial = revision.text
+
+        if revision is None:
+            revision = answer.active_revision
+
+        self.fields['text'].initial = revision.body
 
 class EditUserForm(forms.Form):
     email = forms.EmailField(label=u'Email', help_text=_('this email does not have to be linked to gravatar'), required=True, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
@@ -244,116 +241,23 @@ class EditUserForm(forms.Form):
                     raise forms.ValidationError(_('this email has already been registered, please use another one'))
         return self.cleaned_data['email']
 
-class TagFilterSelectionForm(forms.ModelForm):
-    tag_filter_setting = forms.ChoiceField(choices=TAG_EMAIL_FILTER_CHOICES, #imported from forum/const.py
-                                            initial='ignored',
-                                            label=_('Choose email tag filter'),
-                                            widget=forms.RadioSelect)
-    class Meta:
-        model = User
-        fields = ('tag_filter_setting',)
-
-    def save(self):
-        before = self.instance.tag_filter_setting
-        super(TagFilterSelectionForm, self).save()
-        after = self.instance.tag_filter_setting #User.objects.get(pk=self.instance.id).tag_filter_setting
-        if before != after:
-            return True
-        return False
-
-
-class ChangePasswordForm(SetPasswordForm):
-    """ change password form """
-    oldpw = forms.CharField(widget=forms.PasswordInput(attrs={'class':'required'}),
-                label=mark_safe(_('Current password')))
-
-    def __init__(self, data=None, user=None, *args, **kwargs):
-        if user is None:
-            raise TypeError("Keyword argument 'user' must be supplied")
-        super(ChangePasswordForm, self).__init__(data, *args, **kwargs)
-        self.user = user
 
-    def clean_oldpw(self):
-        """ test old password """
-        if not self.user.check_password(self.cleaned_data['oldpw']):
-            raise forms.ValidationError(_("Old password is incorrect. \
-                    Please enter the correct password."))
-        return self.cleaned_data['oldpw']
-
-class EditUserEmailFeedsForm(forms.Form):
-    WN = (('w',_('weekly')),('n',_('no email')))
-    DWN = (('d',_('daily')),('w',_('weekly')),('n',_('no email')))
-    FORM_TO_MODEL_MAP = {
-                'all_questions':'q_all',
-                'asked_by_me':'q_ask',
-                'answered_by_me':'q_ans',
-                'individually_selected':'q_sel',
-                }
-    NO_EMAIL_INITIAL = {
-                'all_questions':'n',
-                'asked_by_me':'n',
-                'answered_by_me':'n',
-                'individually_selected':'n',
-                }
-    asked_by_me = forms.ChoiceField(choices=DWN,initial='w',
-                            widget=forms.RadioSelect,
-                            label=_('Asked by me'))
-    answered_by_me = forms.ChoiceField(choices=DWN,initial='w',
-                            widget=forms.RadioSelect,
-                            label=_('Answered by me'))
-    individually_selected = forms.ChoiceField(choices=DWN,initial='w',
-                            widget=forms.RadioSelect,
-                            label=_('Individually selected'))
-    all_questions = forms.ChoiceField(choices=DWN,initial='w',
-                            widget=forms.RadioSelect,
-                            label=_('Entire forum (tag filtered)'),)
-
-    def set_initial_values(self,user=None):
-        KEY_MAP = dict([(v,k) for k,v in self.FORM_TO_MODEL_MAP.iteritems()])
-        if user != None:
-            settings = EmailFeedSetting.objects.filter(subscriber=user)
-            initial_values = {}
-            for setting in settings:
-                feed_type = setting.feed_type
-                form_field = KEY_MAP[feed_type]
-                frequency = setting.frequency
-                initial_values[form_field] = frequency
-            self.initial = initial_values
-        return self
-
-    def reset(self):
-        self.cleaned_data['all_questions'] = 'n'
-        self.cleaned_data['asked_by_me'] = 'n'
-        self.cleaned_data['answered_by_me'] = 'n'
-        self.cleaned_data['individually_selected'] = 'n'
-        self.initial = self.NO_EMAIL_INITIAL
-        return self
-
-    def save(self,user,save_unbound=False):
-        """
-            with save_unbound==True will bypass form validation and save initial values
-        """
-        changed = False
-        for form_field, feed_type in self.FORM_TO_MODEL_MAP.items():
-            s, created = EmailFeedSetting.objects.get_or_create(subscriber=user,\
-                                                    feed_type=feed_type)
-            if save_unbound:
-                #just save initial values instead
-                if form_field in self.initial:
-                    new_value = self.initial[form_field]
-                else:
-                    new_value = self.fields[form_field].initial
-            else:
-                new_value = self.cleaned_data[form_field]
-            if s.frequency != new_value:
-                s.frequency = new_value
-                s.save()
-                changed = True
-            else:
-                if created:
-                    s.save()
-            if form_field == 'individually_selected':
-                feed_type = ContentType.objects.get_for_model(Question)
-                user.followed_questions.clear()
-        return changed
+class SubscriptionSettingsForm(forms.Form):
+    member_joins = forms.ChoiceField(widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)
+    new_question = forms.ChoiceField(widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)
+    new_question_watched_tags = forms.ChoiceField(widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)
+    subscribed_questions = forms.ChoiceField(widget=forms.RadioSelect, choices=NOTIFICATION_CHOICES)
+
+    all_questions = forms.BooleanField(required=False, initial=False)
+    all_questions_watched_tags = forms.BooleanField(required=False, initial=False)
+    questions_asked = forms.BooleanField(required=False, initial=False)
+    questions_answered = forms.BooleanField(required=False, initial=False)
+    questions_commented = forms.BooleanField(required=False, initial=False)
+    questions_viewed = forms.BooleanField(required=False, initial=False)
+
+    notify_answers = forms.BooleanField(required=False, initial=False)
+    notify_reply_to_comments = forms.BooleanField(required=False, initial=False)
+    notify_comments_own_post = forms.BooleanField(required=False, initial=False)
+    notify_comments = forms.BooleanField(required=False, initial=False)
+    notify_accepted = forms.BooleanField(required=False, initial=False)