Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Users no long are allowed to change their username, and the email add…

…ress is now a required field.
  • Loading branch information...
commit e70af1088b01f059a5a989ef4640e45999af007b 1 parent 9265259
hrcerqueira hrcerqueira authored

Showing 3 changed files with 414 additions and 414 deletions. Show diff stats Hide diff stats

  1. +318 318 forum/forms.py
  2. +1 1  forum/views.py
  3. +95 95 templates/user_edit.html
636 forum/forms.py
... ... @@ -1,318 +1,318 @@
1   -import re
2   -from datetime import date
3   -from django import forms
4   -from models import *
5   -from const import *
6   -from django.utils.translation import ugettext as _
7   -from django_authopenid.forms import NextUrlField, UserNameField
8   -import settings
9   -
10   -class TitleField(forms.CharField):
11   - def __init__(self, *args, **kwargs):
12   - super(TitleField, self).__init__(*args, **kwargs)
13   - self.required = True
14   - self.widget = forms.TextInput(attrs={'size' : 70, 'autocomplete' : 'off'})
15   - self.max_length = 255
16   - self.label = _('title')
17   - self.help_text = _('please enter a descriptive title for your question')
18   - self.initial = ''
19   -
20   - def clean(self, value):
21   - if len(value) < 10:
22   - raise forms.ValidationError(_('title must be > 10 characters'))
23   -
24   - return value
25   -
26   -class EditorField(forms.CharField):
27   - def __init__(self, *args, **kwargs):
28   - super(EditorField, self).__init__(*args, **kwargs)
29   - self.required = True
30   - self.widget = forms.Textarea(attrs={'id':'editor'})
31   - self.label = _('content')
32   - self.help_text = u''
33   - self.initial = ''
34   -
35   - def clean(self, value):
36   - if len(value) < 10:
37   - raise forms.ValidationError(_('question content must be > 10 characters'))
38   -
39   - return value
40   -
41   -class TagNamesField(forms.CharField):
42   - def __init__(self, *args, **kwargs):
43   - super(TagNamesField, self).__init__(*args, **kwargs)
44   - self.required = True
45   - self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
46   - self.max_length = 255
47   - self.label = _('tags')
48   - #self.help_text = _('please use space to separate tags (this enables autocomplete feature)')
49   - self.help_text = _('Tags are short keywords, with no spaces within. Up to five tags can be used.')
50   - self.initial = ''
51   -
52   - def clean(self, value):
53   - value = super(TagNamesField, self).clean(value)
54   - data = value.strip()
55   - if len(data) < 1:
56   - raise forms.ValidationError(_('tags are required'))
57   -
58   - split_re = re.compile(r'[ ,]+')
59   - list = split_re.split(data)
60   - list_temp = []
61   - if len(list) > 5:
62   - raise forms.ValidationError(_('please use 5 tags or less'))
63   - for tag in list:
64   - if len(tag) > 20:
65   - raise forms.ValidationError(_('tags must be shorter than 20 characters'))
66   - #take tag regex from settings
67   - tagname_re = re.compile(r'[a-z0-9]+')
68   - if not tagname_re.match(tag):
69   - raise forms.ValidationError(_('please use following characters in tags: letters \'a-z\', numbers, and characters \'.-_#\''))
70   - # only keep one same tag
71   - if tag not in list_temp and len(tag.strip()) > 0:
72   - list_temp.append(tag)
73   - return u' '.join(list_temp)
74   -
75   -class WikiField(forms.BooleanField):
76   - def __init__(self, *args, **kwargs):
77   - super(WikiField, self).__init__(*args, **kwargs)
78   - self.required = False
79   - self.label = _('community wiki')
80   - self.help_text = _('if you choose community wiki option, the question and answer do not generate points and name of author will not be shown')
81   - def clean(self,value):
82   - return value and settings.WIKI_ON
83   -
84   -class EmailNotifyField(forms.BooleanField):
85   - def __init__(self, *args, **kwargs):
86   - super(EmailNotifyField, self).__init__(*args, **kwargs)
87   - self.required = False
88   - self.widget.attrs['class'] = 'nomargin'
89   -
90   -class SummaryField(forms.CharField):
91   - def __init__(self, *args, **kwargs):
92   - super(SummaryField, self).__init__(*args, **kwargs)
93   - self.required = False
94   - self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
95   - self.max_length = 300
96   - self.label = _('update summary:')
97   - self.help_text = _('enter a brief summary of your revision (e.g. fixed spelling, grammar, improved style, this field is optional)')
98   -
99   -class ModerateUserForm(forms.ModelForm):
100   - is_approved = forms.BooleanField(label=_("Automatically accept user's contributions for the email updates"),
101   - required=False)
102   -
103   - def clean_is_approved(self):
104   - if 'is_approved' not in self.cleaned_data:
105   - self.cleaned_data['is_approved'] = False
106   - return self.cleaned_data['is_approved']
107   -
108   - class Meta:
109   - model = User
110   - fields = ('is_approved',)
111   -
112   -class FeedbackForm(forms.Form):
113   - name = forms.CharField(label=_('Your name:'), required=False)
114   - email = forms.EmailField(label=_('Email (not shared with anyone):'), required=False)
115   - message = forms.CharField(label=_('Your message:'), max_length=800,widget=forms.Textarea(attrs={'cols':60}))
116   - next = NextUrlField()
117   -
118   -class AskForm(forms.Form):
119   - title = TitleField()
120   - text = EditorField()
121   - tags = TagNamesField()
122   - wiki = WikiField()
123   -
124   - openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
125   - user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
126   - email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
127   -
128   -class AnswerForm(forms.Form):
129   - text = EditorField()
130   - wiki = WikiField()
131   - openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
132   - user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
133   - email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
134   - email_notify = EmailNotifyField()
135   - def __init__(self, question, user, *args, **kwargs):
136   - super(AnswerForm, self).__init__(*args, **kwargs)
137   - self.fields['email_notify'].widget.attrs['id'] = 'question-subscribe-updates';
138   - if question.wiki and settings.WIKI_ON:
139   - self.fields['wiki'].initial = True
140   - if user.is_authenticated():
141   - if user in question.followed_by.all():
142   - self.fields['email_notify'].initial = True
143   - return
144   - self.fields['email_notify'].initial = False
145   -
146   -
147   -class CloseForm(forms.Form):
148   - reason = forms.ChoiceField(choices=CLOSE_REASONS)
149   -
150   -class RetagQuestionForm(forms.Form):
151   - tags = TagNamesField()
152   - # initialize the default values
153   - def __init__(self, question, *args, **kwargs):
154   - super(RetagQuestionForm, self).__init__(*args, **kwargs)
155   - self.fields['tags'].initial = question.tagnames
156   -
157   -class RevisionForm(forms.Form):
158   - """
159   - Lists revisions of a Question or Answer
160   - """
161   - revision = forms.ChoiceField(widget=forms.Select(attrs={'style' : 'width:520px'}))
162   -
163   - def __init__(self, post, latest_revision, *args, **kwargs):
164   - super(RevisionForm, self).__init__(*args, **kwargs)
165   - revisions = post.revisions.all().values_list(
166   - 'revision', 'author__username', 'revised_at', 'summary')
167   - date_format = '%c'
168   - self.fields['revision'].choices = [
169   - (r[0], u'%s - %s (%s) %s' % (r[0], r[1], r[2].strftime(date_format), r[3]))
170   - for r in revisions]
171   - self.fields['revision'].initial = latest_revision.revision
172   -
173   -class EditQuestionForm(forms.Form):
174   - title = TitleField()
175   - text = EditorField()
176   - tags = TagNamesField()
177   - summary = SummaryField()
178   -
179   - def __init__(self, question, revision, *args, **kwargs):
180   - super(EditQuestionForm, self).__init__(*args, **kwargs)
181   - self.fields['title'].initial = revision.title
182   - self.fields['text'].initial = revision.text
183   - self.fields['tags'].initial = revision.tagnames
184   - # Once wiki mode is enabled, it can't be disabled
185   - if not question.wiki:
186   - self.fields['wiki'] = WikiField()
187   -
188   -class EditAnswerForm(forms.Form):
189   - text = EditorField()
190   - summary = SummaryField()
191   -
192   - def __init__(self, answer, revision, *args, **kwargs):
193   - super(EditAnswerForm, self).__init__(*args, **kwargs)
194   - self.fields['text'].initial = revision.text
195   -
196   -class EditUserForm(forms.Form):
197   - email = forms.EmailField(label=u'Email', help_text=_('this email does not have to be linked to gravatar'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
198   - username = UserNameField(label=_('Screen name'))
199   - realname = forms.CharField(label=_('Real name'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
200   - website = forms.URLField(label=_('Website'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
201   - city = forms.CharField(label=_('Location'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
202   - birthday = forms.DateField(label=_('Date of birth'), help_text=_('will not be shown, used to calculate age, format: YYYY-MM-DD'), required=False, widget=forms.TextInput(attrs={'size' : 35}))
203   - about = forms.CharField(label=_('Profile'), required=False, widget=forms.Textarea(attrs={'cols' : 60}))
204   -
205   - def __init__(self, user, *args, **kwargs):
206   - super(EditUserForm, self).__init__(*args, **kwargs)
207   - self.fields['username'].initial = user.username
208   - self.fields['username'].user_instance = user
209   - self.fields['email'].initial = user.email
210   - self.fields['realname'].initial = user.real_name
211   - self.fields['website'].initial = user.website
212   - self.fields['city'].initial = user.location
213   -
214   - if user.date_of_birth is not None:
215   - self.fields['birthday'].initial = user.date_of_birth
216   - else:
217   - self.fields['birthday'].initial = '1990-01-01'
218   - self.fields['about'].initial = user.about
219   - self.user = user
220   -
221   - def clean_email(self):
222   - """For security reason one unique email in database"""
223   - if self.user.email != self.cleaned_data['email']:
224   - #todo dry it, there is a similar thing in openidauth
225   - if settings.EMAIL_UNIQUE == True:
226   - if 'email' in self.cleaned_data:
227   - try:
228   - user = User.objects.get(email = self.cleaned_data['email'])
229   - except User.DoesNotExist:
230   - return self.cleaned_data['email']
231   - except User.MultipleObjectsReturned:
232   - raise forms.ValidationError(_('this email has already been registered, please use another one'))
233   - raise forms.ValidationError(_('this email has already been registered, please use another one'))
234   - return self.cleaned_data['email']
235   -
236   -class TagFilterSelectionForm(forms.ModelForm):
237   - tag_filter_setting = forms.ChoiceField(choices=TAG_EMAIL_FILTER_CHOICES, #imported from forum/const.py
238   - initial='ignored',
239   - label=_('Choose email tag filter'),
240   - widget=forms.RadioSelect)
241   - class Meta:
242   - model = User
243   - fields = ('tag_filter_setting',)
244   -
245   - def save(self):
246   - before = self.instance.tag_filter_setting
247   - super(TagFilterSelectionForm, self).save()
248   - after = self.instance.tag_filter_setting #User.objects.get(pk=self.instance.id).tag_filter_setting
249   - if before != after:
250   - return True
251   - return False
252   -
253   -class EditUserEmailFeedsForm(forms.Form):
254   - WN = (('w',_('weekly')),('n',_('no email')))
255   - DWN = (('d',_('daily')),('w',_('weekly')),('n',_('no email')))
256   - FORM_TO_MODEL_MAP = {
257   - 'all_questions':'q_all',
258   - 'asked_by_me':'q_ask',
259   - 'answered_by_me':'q_ans',
260   - 'individually_selected':'q_sel',
261   - }
262   - NO_EMAIL_INITIAL = {
263   - 'all_questions':'n',
264   - 'asked_by_me':'n',
265   - 'answered_by_me':'n',
266   - 'individually_selected':'n',
267   - }
268   - asked_by_me = forms.ChoiceField(choices=DWN,initial='w',
269   - widget=forms.RadioSelect,
270   - label=_('Asked by me'))
271   - answered_by_me = forms.ChoiceField(choices=DWN,initial='w',
272   - widget=forms.RadioSelect,
273   - label=_('Answered by me'))
274   - individually_selected = forms.ChoiceField(choices=DWN,initial='w',
275   - widget=forms.RadioSelect,
276   - label=_('Individually selected'))
277   - all_questions = forms.ChoiceField(choices=DWN,initial='w',
278   - widget=forms.RadioSelect,
279   - label=_('Entire forum (tag filtered)'),)
280   -
281   - def set_initial_values(self,user=None):
282   - KEY_MAP = dict([(v,k) for k,v in self.FORM_TO_MODEL_MAP.iteritems()])
283   - if user != None:
284   - settings = EmailFeedSetting.objects.filter(subscriber=user)
285   - initial_values = {}
286   - for setting in settings:
287   - feed_type = setting.feed_type
288   - form_field = KEY_MAP[feed_type]
289   - frequency = setting.frequency
290   - initial_values[form_field] = frequency
291   - self.initial = initial_values
292   - return self
293   -
294   - def reset(self):
295   - self.cleaned_data['all_questions'] = 'n'
296   - self.cleaned_data['asked_by_me'] = 'n'
297   - self.cleaned_data['answered_by_me'] = 'n'
298   - self.cleaned_data['individually_selected'] = 'n'
299   - self.initial = self.NO_EMAIL_INITIAL
300   - return self
301   -
302   - def save(self,user):
303   - changed = False
304   - for form_field, feed_type in self.FORM_TO_MODEL_MAP.items():
305   - s, created = EmailFeedSetting.objects.get_or_create(subscriber=user,\
306   - feed_type=feed_type)
307   - new_value = self.cleaned_data[form_field]
308   - if s.frequency != new_value:
309   - s.frequency = self.cleaned_data[form_field]
310   - s.save()
311   - changed = True
312   - else:
313   - if created:
314   - s.save()
315   - if form_field == 'individually_selected':
316   - feed_type = ContentType.objects.get_for_model(Question)
317   - user.followed_questions.clear()
318   - return changed
  1 +import re
  2 +from datetime import date
  3 +from django import forms
  4 +from models import *
  5 +from const import *
  6 +from django.utils.translation import ugettext as _
  7 +from django_authopenid.forms import NextUrlField, UserNameField
  8 +import settings
  9 +
  10 +class TitleField(forms.CharField):
  11 + def __init__(self, *args, **kwargs):
  12 + super(TitleField, self).__init__(*args, **kwargs)
  13 + self.required = True
  14 + self.widget = forms.TextInput(attrs={'size' : 70, 'autocomplete' : 'off'})
  15 + self.max_length = 255
  16 + self.label = _('title')
  17 + self.help_text = _('please enter a descriptive title for your question')
  18 + self.initial = ''
  19 +
  20 + def clean(self, value):
  21 + if len(value) < 10:
  22 + raise forms.ValidationError(_('title must be > 10 characters'))
  23 +
  24 + return value
  25 +
  26 +class EditorField(forms.CharField):
  27 + def __init__(self, *args, **kwargs):
  28 + super(EditorField, self).__init__(*args, **kwargs)
  29 + self.required = True
  30 + self.widget = forms.Textarea(attrs={'id':'editor'})
  31 + self.label = _('content')
  32 + self.help_text = u''
  33 + self.initial = ''
  34 +
  35 + def clean(self, value):
  36 + if len(value) < 10:
  37 + raise forms.ValidationError(_('question content must be > 10 characters'))
  38 +
  39 + return value
  40 +
  41 +class TagNamesField(forms.CharField):
  42 + def __init__(self, *args, **kwargs):
  43 + super(TagNamesField, self).__init__(*args, **kwargs)
  44 + self.required = True
  45 + self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
  46 + self.max_length = 255
  47 + self.label = _('tags')
  48 + #self.help_text = _('please use space to separate tags (this enables autocomplete feature)')
  49 + self.help_text = _('Tags are short keywords, with no spaces within. Up to five tags can be used.')
  50 + self.initial = ''
  51 +
  52 + def clean(self, value):
  53 + value = super(TagNamesField, self).clean(value)
  54 + data = value.strip()
  55 + if len(data) < 1:
  56 + raise forms.ValidationError(_('tags are required'))
  57 +
  58 + split_re = re.compile(r'[ ,]+')
  59 + list = split_re.split(data)
  60 + list_temp = []
  61 + if len(list) > 5:
  62 + raise forms.ValidationError(_('please use 5 tags or less'))
  63 + for tag in list:
  64 + if len(tag) > 20:
  65 + raise forms.ValidationError(_('tags must be shorter than 20 characters'))
  66 + #take tag regex from settings
  67 + tagname_re = re.compile(r'[a-z0-9]+')
  68 + if not tagname_re.match(tag):
  69 + raise forms.ValidationError(_('please use following characters in tags: letters \'a-z\', numbers, and characters \'.-_#\''))
  70 + # only keep one same tag
  71 + if tag not in list_temp and len(tag.strip()) > 0:
  72 + list_temp.append(tag)
  73 + return u' '.join(list_temp)
  74 +
  75 +class WikiField(forms.BooleanField):
  76 + def __init__(self, *args, **kwargs):
  77 + super(WikiField, self).__init__(*args, **kwargs)
  78 + self.required = False
  79 + self.label = _('community wiki')
  80 + self.help_text = _('if you choose community wiki option, the question and answer do not generate points and name of author will not be shown')
  81 + def clean(self,value):
  82 + return value and settings.WIKI_ON
  83 +
  84 +class EmailNotifyField(forms.BooleanField):
  85 + def __init__(self, *args, **kwargs):
  86 + super(EmailNotifyField, self).__init__(*args, **kwargs)
  87 + self.required = False
  88 + self.widget.attrs['class'] = 'nomargin'
  89 +
  90 +class SummaryField(forms.CharField):
  91 + def __init__(self, *args, **kwargs):
  92 + super(SummaryField, self).__init__(*args, **kwargs)
  93 + self.required = False
  94 + self.widget = forms.TextInput(attrs={'size' : 50, 'autocomplete' : 'off'})
  95 + self.max_length = 300
  96 + self.label = _('update summary:')
  97 + self.help_text = _('enter a brief summary of your revision (e.g. fixed spelling, grammar, improved style, this field is optional)')
  98 +
  99 +class ModerateUserForm(forms.ModelForm):
  100 + is_approved = forms.BooleanField(label=_("Automatically accept user's contributions for the email updates"),
  101 + required=False)
  102 +
  103 + def clean_is_approved(self):
  104 + if 'is_approved' not in self.cleaned_data:
  105 + self.cleaned_data['is_approved'] = False
  106 + return self.cleaned_data['is_approved']
  107 +
  108 + class Meta:
  109 + model = User
  110 + fields = ('is_approved',)
  111 +
  112 +class FeedbackForm(forms.Form):
  113 + name = forms.CharField(label=_('Your name:'), required=False)
  114 + email = forms.EmailField(label=_('Email (not shared with anyone):'), required=False)
  115 + message = forms.CharField(label=_('Your message:'), max_length=800,widget=forms.Textarea(attrs={'cols':60}))
  116 + next = NextUrlField()
  117 +
  118 +class AskForm(forms.Form):
  119 + title = TitleField()
  120 + text = EditorField()
  121 + tags = TagNamesField()
  122 + wiki = WikiField()
  123 +
  124 + openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
  125 + user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  126 + email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  127 +
  128 +class AnswerForm(forms.Form):
  129 + text = EditorField()
  130 + wiki = WikiField()
  131 + openid = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 40, 'class':'openid-input'}))
  132 + user = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  133 + email = forms.CharField(required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  134 + email_notify = EmailNotifyField()
  135 + def __init__(self, question, user, *args, **kwargs):
  136 + super(AnswerForm, self).__init__(*args, **kwargs)
  137 + self.fields['email_notify'].widget.attrs['id'] = 'question-subscribe-updates';
  138 + if question.wiki and settings.WIKI_ON:
  139 + self.fields['wiki'].initial = True
  140 + if user.is_authenticated():
  141 + if user in question.followed_by.all():
  142 + self.fields['email_notify'].initial = True
  143 + return
  144 + self.fields['email_notify'].initial = False
  145 +
  146 +
  147 +class CloseForm(forms.Form):
  148 + reason = forms.ChoiceField(choices=CLOSE_REASONS)
  149 +
  150 +class RetagQuestionForm(forms.Form):
  151 + tags = TagNamesField()
  152 + # initialize the default values
  153 + def __init__(self, question, *args, **kwargs):
  154 + super(RetagQuestionForm, self).__init__(*args, **kwargs)
  155 + self.fields['tags'].initial = question.tagnames
  156 +
  157 +class RevisionForm(forms.Form):
  158 + """
  159 + Lists revisions of a Question or Answer
  160 + """
  161 + revision = forms.ChoiceField(widget=forms.Select(attrs={'style' : 'width:520px'}))
  162 +
  163 + def __init__(self, post, latest_revision, *args, **kwargs):
  164 + super(RevisionForm, self).__init__(*args, **kwargs)
  165 + revisions = post.revisions.all().values_list(
  166 + 'revision', 'author__username', 'revised_at', 'summary')
  167 + date_format = '%c'
  168 + self.fields['revision'].choices = [
  169 + (r[0], u'%s - %s (%s) %s' % (r[0], r[1], r[2].strftime(date_format), r[3]))
  170 + for r in revisions]
  171 + self.fields['revision'].initial = latest_revision.revision
  172 +
  173 +class EditQuestionForm(forms.Form):
  174 + title = TitleField()
  175 + text = EditorField()
  176 + tags = TagNamesField()
  177 + summary = SummaryField()
  178 +
  179 + def __init__(self, question, revision, *args, **kwargs):
  180 + super(EditQuestionForm, self).__init__(*args, **kwargs)
  181 + self.fields['title'].initial = revision.title
  182 + self.fields['text'].initial = revision.text
  183 + self.fields['tags'].initial = revision.tagnames
  184 + # Once wiki mode is enabled, it can't be disabled
  185 + if not question.wiki:
  186 + self.fields['wiki'] = WikiField()
  187 +
  188 +class EditAnswerForm(forms.Form):
  189 + text = EditorField()
  190 + summary = SummaryField()
  191 +
  192 + def __init__(self, answer, revision, *args, **kwargs):
  193 + super(EditAnswerForm, self).__init__(*args, **kwargs)
  194 + self.fields['text'].initial = revision.text
  195 +
  196 +class EditUserForm(forms.Form):
  197 + 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}))
  198 + #username = UserNameField(label=_('Screen name'))
  199 + realname = forms.CharField(label=_('Real name'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  200 + website = forms.URLField(label=_('Website'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  201 + city = forms.CharField(label=_('Location'), required=False, max_length=255, widget=forms.TextInput(attrs={'size' : 35}))
  202 + birthday = forms.DateField(label=_('Date of birth'), help_text=_('will not be shown, used to calculate age, format: YYYY-MM-DD'), required=False, widget=forms.TextInput(attrs={'size' : 35}))
  203 + about = forms.CharField(label=_('Profile'), required=False, widget=forms.Textarea(attrs={'cols' : 60}))
  204 +
  205 + def __init__(self, user, *args, **kwargs):
  206 + super(EditUserForm, self).__init__(*args, **kwargs)
  207 + #self.fields['username'].initial = user.username
  208 + #self.fields['username'].user_instance = user
  209 + self.fields['email'].initial = user.email
  210 + self.fields['realname'].initial = user.real_name
  211 + self.fields['website'].initial = user.website
  212 + self.fields['city'].initial = user.location
  213 +
  214 + if user.date_of_birth is not None:
  215 + self.fields['birthday'].initial = user.date_of_birth
  216 + else:
  217 + self.fields['birthday'].initial = '1990-01-01'
  218 + self.fields['about'].initial = user.about
  219 + self.user = user
  220 +
  221 + def clean_email(self):
  222 + """For security reason one unique email in database"""
  223 + if self.user.email != self.cleaned_data['email']:
  224 + #todo dry it, there is a similar thing in openidauth
  225 + if settings.EMAIL_UNIQUE == True:
  226 + if 'email' in self.cleaned_data:
  227 + try:
  228 + user = User.objects.get(email = self.cleaned_data['email'])
  229 + except User.DoesNotExist:
  230 + return self.cleaned_data['email']
  231 + except User.MultipleObjectsReturned:
  232 + raise forms.ValidationError(_('this email has already been registered, please use another one'))
  233 + raise forms.ValidationError(_('this email has already been registered, please use another one'))
  234 + return self.cleaned_data['email']
  235 +
  236 +class TagFilterSelectionForm(forms.ModelForm):
  237 + tag_filter_setting = forms.ChoiceField(choices=TAG_EMAIL_FILTER_CHOICES, #imported from forum/const.py
  238 + initial='ignored',
  239 + label=_('Choose email tag filter'),
  240 + widget=forms.RadioSelect)
  241 + class Meta:
  242 + model = User
  243 + fields = ('tag_filter_setting',)
  244 +
  245 + def save(self):
  246 + before = self.instance.tag_filter_setting
  247 + super(TagFilterSelectionForm, self).save()
  248 + after = self.instance.tag_filter_setting #User.objects.get(pk=self.instance.id).tag_filter_setting
  249 + if before != after:
  250 + return True
  251 + return False
  252 +
  253 +class EditUserEmailFeedsForm(forms.Form):
  254 + WN = (('w',_('weekly')),('n',_('no email')))
  255 + DWN = (('d',_('daily')),('w',_('weekly')),('n',_('no email')))
  256 + FORM_TO_MODEL_MAP = {
  257 + 'all_questions':'q_all',
  258 + 'asked_by_me':'q_ask',
  259 + 'answered_by_me':'q_ans',
  260 + 'individually_selected':'q_sel',
  261 + }
  262 + NO_EMAIL_INITIAL = {
  263 + 'all_questions':'n',
  264 + 'asked_by_me':'n',
  265 + 'answered_by_me':'n',
  266 + 'individually_selected':'n',
  267 + }
  268 + asked_by_me = forms.ChoiceField(choices=DWN,initial='w',
  269 + widget=forms.RadioSelect,
  270 + label=_('Asked by me'))
  271 + answered_by_me = forms.ChoiceField(choices=DWN,initial='w',
  272 + widget=forms.RadioSelect,
  273 + label=_('Answered by me'))
  274 + individually_selected = forms.ChoiceField(choices=DWN,initial='w',
  275 + widget=forms.RadioSelect,
  276 + label=_('Individually selected'))
  277 + all_questions = forms.ChoiceField(choices=DWN,initial='w',
  278 + widget=forms.RadioSelect,
  279 + label=_('Entire forum (tag filtered)'),)
  280 +
  281 + def set_initial_values(self,user=None):
  282 + KEY_MAP = dict([(v,k) for k,v in self.FORM_TO_MODEL_MAP.iteritems()])
  283 + if user != None:
  284 + settings = EmailFeedSetting.objects.filter(subscriber=user)
  285 + initial_values = {}
  286 + for setting in settings:
  287 + feed_type = setting.feed_type
  288 + form_field = KEY_MAP[feed_type]
  289 + frequency = setting.frequency
  290 + initial_values[form_field] = frequency
  291 + self.initial = initial_values
  292 + return self
  293 +
  294 + def reset(self):
  295 + self.cleaned_data['all_questions'] = 'n'
  296 + self.cleaned_data['asked_by_me'] = 'n'
  297 + self.cleaned_data['answered_by_me'] = 'n'
  298 + self.cleaned_data['individually_selected'] = 'n'
  299 + self.initial = self.NO_EMAIL_INITIAL
  300 + return self
  301 +
  302 + def save(self,user):
  303 + changed = False
  304 + for form_field, feed_type in self.FORM_TO_MODEL_MAP.items():
  305 + s, created = EmailFeedSetting.objects.get_or_create(subscriber=user,\
  306 + feed_type=feed_type)
  307 + new_value = self.cleaned_data[form_field]
  308 + if s.frequency != new_value:
  309 + s.frequency = self.cleaned_data[form_field]
  310 + s.save()
  311 + changed = True
  312 + else:
  313 + if created:
  314 + s.save()
  315 + if form_field == 'individually_selected':
  316 + feed_type = ContentType.objects.get_for_model(Question)
  317 + user.followed_questions.clear()
  318 + return changed
2  forum/views.py
@@ -1235,7 +1235,7 @@ def edit_user(request, id):
1235 1235 from django_authopenid.views import set_new_email
1236 1236 set_new_email(user, new_email)
1237 1237
1238   - user.username = sanitize_html(form.cleaned_data['username'])
  1238 + #user.username = sanitize_html(form.cleaned_data['username'])
1239 1239 user.real_name = sanitize_html(form.cleaned_data['realname'])
1240 1240 user.website = sanitize_html(form.cleaned_data['website'])
1241 1241 user.location = sanitize_html(form.cleaned_data['city'])
190 templates/user_edit.html
... ... @@ -1,95 +1,95 @@
1   -{% extends "base_content.html" %}
2   -<!-- user_edit.html -->
3   -{% load extra_tags %}
4   -{% load humanize %}
5   -{% load i18n %}
6   -{% block title %}{% spaceless %}{% trans "Edit user profile" %}{% endspaceless %}{% endblock %}
7   -{% block forejs %}
8   - <script type="text/javascript">
9   - $().ready(function(){
10   - $("#nav_profile").attr('className',"on");
11   - $("#cancel").bind('click', function(){history.go(-1);})
12   - });
13   - </script>
14   - {% block userjs %}
15   - {% endblock %}
16   -{% endblock %}
17   -{% block content %}
18   -<div id="main-bar" class="headNormal">
19   - {{ request.user.username }} - {% trans "edit profile" %}
20   -</div>
21   -<div id="main-body" style="width:100%;padding-top:10px">
22   - <form name="" action="{% url edit_user request.user.id %}" method="post">
23   - <div id="left" style="float:left;width:180px">
24   - {% if request.user.email %}
25   - {% gravatar request.user 128 %}
26   - {% else %}
27   - <img src="{% href "/content/images/nophoto.png" %}">
28   - {% endif %}
29   - <div style="padding:20px 0 0 20px;font-weight:bold;font-size:150%">
30   - <a href="http://www.gravatar.com/" target="_blank"
31   - title="gravatar {% trans "image associated with your email address" %}">{% blocktrans %}avatar, see {{gravatar_faq_url}}{% endblocktrans %}</a>
32   - </div>
33   - </div>
34   -
35   - <div id="askform" style="float:right;width:750px;text-align:left;">
36   - <h2>{% trans "Registered user" %}</h2>
37   - <table class="user-details">
38   - <tr>
39   - <th width="100px"></th>
40   - <th></th>
41   - </tr>
42   - <tr style="height:35px">
43   - <td>{{ form.username.label_tag }}:</td>
44   - <td>{{ form.username }} <span class="form-error"></span> {{ form.username.errors }} </td>
45   - </tr>
46   -
47   - <tr style="height:35px">
48   - <td>{{ form.email.label_tag }}:</td>
49   - <td>{{ form.email }} <span class="form-error"></span> {{ form.email.errors }} </td>
50   - </tr>
51   - <tr style="height:35px">
52   - <td></td>
53   - <td class="title-desc">{{ form.email.help_text }}</td>
54   - </tr>
55   - <tr style="height:35px">
56   - <td>{{ form.realname.label_tag }}:</td>
57   - <td>{{ form.realname }} <span class="form-error"></span> {{ form.realname.errors }} </td>
58   - </tr>
59   - <tr style="height:35px">
60   - <td>{{ form.website.label_tag }}:</td>
61   - <td>{{ form.website }} <span class="form-error"></span> {{ form.website.errors }} </td>
62   - </tr>
63   - <tr style="height:35px">
64   - <td>{{ form.city.label_tag }}:</td>
65   - <td>{{ form.city }} <span class="form-error"></span> {{ form.city.errors }} </td>
66   - </tr>
67   - <tr style="height:35px">
68   - <td>{{ form.birthday.label_tag }}:</td>
69   - <td>{{ form.birthday }} <span class="form-error"></span> {{ form.birthday.errors }} </td>
70   - </tr>
71   - <tr style="height:35px">
72   - <td></td>
73   - <td class="title-desc">{{ form.birthday.help_text }}</td>
74   - </tr>
75   - <tr style="height:10px">
76   - <td colspan="2">
77   - </td>
78   - </tr>
79   - <tr>
80   - <td style="vertical-align:top">{{ form.about.label_tag }}:</td>
81   - <td>{{ form.about }} <span class="form-error"></span> {{ form.about.errors }} </td>
82   - </tr>
83   -
84   - </table>
85   - <div style="margin:30px 0 60px 0">
86   - <input type="submit" value="{% trans "Update" %}" class="submit" >
87   - <input id="cancel" type="button" value="{% trans "Cancel" %}" class="submit" >
88   -
89   - </div>
90   - </div>
91   - </form>
92   -
93   -</div>
94   -{% endblock %}
95   -<!-- end user_edit.html -->
  1 +{% extends "base_content.html" %}
  2 +<!-- user_edit.html -->
  3 +{% load extra_tags %}
  4 +{% load humanize %}
  5 +{% load i18n %}
  6 +{% block title %}{% spaceless %}{% trans "Edit user profile" %}{% endspaceless %}{% endblock %}
  7 +{% block forejs %}
  8 + <script type="text/javascript">
  9 + $().ready(function(){
  10 + $("#nav_profile").attr('className',"on");
  11 + $("#cancel").bind('click', function(){history.go(-1);})
  12 + });
  13 + </script>
  14 + {% block userjs %}
  15 + {% endblock %}
  16 +{% endblock %}
  17 +{% block content %}
  18 +<div id="main-bar" class="headNormal">
  19 + {{ request.user.username }} - {% trans "edit profile" %}
  20 +</div>
  21 +<div id="main-body" style="width:100%;padding-top:10px">
  22 + <form name="" action="{% url edit_user request.user.id %}" method="post">
  23 + <div id="left" style="float:left;width:180px">
  24 + {% if request.user.email %}
  25 + {% gravatar request.user 128 %}
  26 + {% else %}
  27 + <img src="{% href "/content/images/nophoto.png" %}">
  28 + {% endif %}
  29 + <div style="padding:20px 0 0 20px;font-weight:bold;font-size:150%">
  30 + <a href="http://www.gravatar.com/" target="_blank"
  31 + title="gravatar {% trans "image associated with your email address" %}">{% blocktrans %}avatar, see {{gravatar_faq_url}}{% endblocktrans %}</a>
  32 + </div>
  33 + </div>
  34 +
  35 + <div id="askform" style="float:right;width:750px;text-align:left;">
  36 + <h2>{% trans "Registered user" %}</h2>
  37 + <table class="user-details">
  38 + <tr>
  39 + <th width="100px"></th>
  40 + <th></th>
  41 + </tr>
  42 + <tr style="height:35px">
  43 + <td>{% trans "Screen Name" %}:</td>
  44 + <td>{{ request.user.username }} <span class="form-error"></span> {{ form.username.errors }} </td>
  45 + </tr>
  46 +
  47 + <tr style="height:35px">
  48 + <td>{{ form.email.label_tag }}:</td>
  49 + <td>{{ form.email }} <span class="form-error"></span> {{ form.email.errors }} </td>
  50 + </tr>
  51 + <tr style="height:35px">
  52 + <td></td>
  53 + <td class="title-desc">{{ form.email.help_text }}</td>
  54 + </tr>
  55 + <tr style="height:35px">
  56 + <td>{{ form.realname.label_tag }}:</td>
  57 + <td>{{ form.realname }} <span class="form-error"></span> {{ form.realname.errors }} </td>
  58 + </tr>
  59 + <tr style="height:35px">
  60 + <td>{{ form.website.label_tag }}:</td>
  61 + <td>{{ form.website }} <span class="form-error"></span> {{ form.website.errors }} </td>
  62 + </tr>
  63 + <tr style="height:35px">
  64 + <td>{{ form.city.label_tag }}:</td>
  65 + <td>{{ form.city }} <span class="form-error"></span> {{ form.city.errors }} </td>
  66 + </tr>
  67 + <tr style="height:35px">
  68 + <td>{{ form.birthday.label_tag }}:</td>
  69 + <td>{{ form.birthday }} <span class="form-error"></span> {{ form.birthday.errors }} </td>
  70 + </tr>
  71 + <tr style="height:35px">
  72 + <td></td>
  73 + <td class="title-desc">{{ form.birthday.help_text }}</td>
  74 + </tr>
  75 + <tr style="height:10px">
  76 + <td colspan="2">
  77 + </td>
  78 + </tr>
  79 + <tr>
  80 + <td style="vertical-align:top">{{ form.about.label_tag }}:</td>
  81 + <td>{{ form.about }} <span class="form-error"></span> {{ form.about.errors }} </td>
  82 + </tr>
  83 +
  84 + </table>
  85 + <div style="margin:30px 0 60px 0">
  86 + <input type="submit" value="{% trans "Update" %}" class="submit" >
  87 + <input id="cancel" type="button" value="{% trans "Cancel" %}" class="submit" >
  88 +
  89 + </div>
  90 + </div>
  91 + </form>
  92 +
  93 +</div>
  94 +{% endblock %}
  95 +<!-- end user_edit.html -->

0 comments on commit e70af10

Please sign in to comment.
Something went wrong with that request. Please try again.