def clean(self, *args, **kwargs): import markdown import html2text from agora_site.agora_core.templatetags.string_tags import urlify_markdown from django.template.defaultfilters import truncatewords_html cleaned_data = super(ElectionAdminForm, self).clean() if not self.instance.has_perms('edit_details', self.request.user): raise ImmediateHttpResponse(response=http.HttpForbidden()) cleaned_data['pretty_name'] = clean_html(cleaned_data['pretty_name'], True) cleaned_data['description'] = clean_html(cleaned_data['description']) short_description = cleaned_data['short_description'] short_description = html2text.html2text(short_description[:140]).strip() short_description = markdown.markdown(urlify_markdown(short_description), safe_mode="escape", enable_attributes=False) cleaned_data['short_description'] = truncatewords_html(short_description, 25)[:140] from_date = cleaned_data.get("from_date", None) to_date = cleaned_data.get("to_date", None) if not from_date and not to_date: return cleaned_data if from_date < timezone.now(): raise django_forms.ValidationError(_('Invalid start date, must be ' 'in the future')) if from_date and to_date and ((to_date - from_date) < datetime.timedelta(hours=1)): raise django_forms.ValidationError(_('Voting time must be at least 1 hour')) return cleaned_data
def clean_first_name(self): """ Validates first_name field (which is actually user's full name). If its a FNMT authenticated user, this user cannot change the first name. """ if ( settings.AGORA_REQUEST_SCANNED_ID_ON_REGISTER and self.cleaned_data["first_name"] != self.request.user.first_name ): return self.request.user.first_name if "first_name" not in self.data: return None first_name = clean_html(self.cleaned_data["first_name"]) if "<" in first_name or '"' in first_name: raise django_forms.ValidationError(_(u"Invalid first name.")) profile = self.request.user.get_profile() if ( isinstance(profile.extra, dict) and profile.extra.has_key("fnmt_cert") and self.request.user.first_name != first_name ): raise django_forms.ValidationError(_("FNMT users cannot change their names.")) return first_name
def clean(self): """ Validates that the values entered into the two password fields match. """ if self.request.user.is_anonymous(): raise django_forms.ValidationError(_("You need to be " "authenticated")) self.cleaned_data["biography"] = clean_html(self.cleaned_data["biography"]) return self.cleaned_data
def clean(self, *args, **kwargs): import markdown import html2text from agora_site.agora_core.templatetags.string_tags import urlify_markdown from django.template.defaultfilters import truncatewords_html cleaned_data = super(ElectionAdminForm, self).clean() if not self.instance.has_perms('edit_details', self.request.user): raise ImmediateHttpResponse(response=http.HttpForbidden()) cleaned_data['pretty_name'] = clean_html(cleaned_data['pretty_name'], True) cleaned_data['description'] = clean_html(cleaned_data['description']) short_description = cleaned_data['short_description'] short_description = html2text.html2text( short_description[:140]).strip() short_description = markdown.markdown( urlify_markdown(short_description), safe_mode="escape", enable_attributes=False) cleaned_data['short_description'] = truncatewords_html( short_description, 25)[:140] from_date = cleaned_data.get("from_date", None) to_date = cleaned_data.get("to_date", None) if not from_date and not to_date: return cleaned_data if from_date < timezone.now(): raise django_forms.ValidationError( _('Invalid start date, must be ' 'in the future')) if from_date and to_date and ( (to_date - from_date) < datetime.timedelta(hours=1)): raise django_forms.ValidationError( _('Voting time must be at least 1 hour')) return cleaned_data
def clean(self): """ Validates that the values entered into the two password fields match. """ if self.request.user.is_anonymous(): raise django_forms.ValidationError( _('You need to be ' 'authenticated')) self.cleaned_data['biography'] = clean_html( self.cleaned_data['biography']) return self.cleaned_data
def clean(self, *args, **kwargs): cleaned_data = super(ElectionAdminForm, self).clean() if not self.instance.has_perms('edit_details', self.request.user): raise ImmediateHttpResponse(response=http.HttpForbidden()) cleaned_data['pretty_name'] = clean_html(cleaned_data['pretty_name'], True) cleaned_data['short_description'] = clean_html(cleaned_data['short_description']) cleaned_data['description'] = clean_html(cleaned_data['description']) from_date = cleaned_data.get("from_date", None) to_date = cleaned_data.get("to_date", None) if not from_date and not to_date: return cleaned_data if from_date < timezone.now(): raise django_forms.ValidationError(_('Invalid start date, must be ' 'in the future')) if from_date and to_date and ((to_date - from_date) < datetime.timedelta(hours=1)): raise django_forms.ValidationError(_('Voting time must be at least 1 hour')) return cleaned_data
def clean_first_name(self): ''' Validates first_name field (which is actually user's full name). If its a FNMT authenticated user, this user cannot change the first name. ''' if 'first_name' not in self.data: return None first_name = clean_html(self.cleaned_data['first_name']) if '<' in first_name or '\"' in first_name: raise django_forms.ValidationError(_(u'Invalid first name.')) profile = self.request.user.get_profile() if isinstance(profile.extra, dict) and\ profile.extra.has_key('fnmt_cert') and\ self.request.user.first_name != first_name: raise django_forms.ValidationError(_('FNMT users cannot change their names.')) return first_name
def save(self): translation.activate(self.target_user.get_profile().lang_code) context = get_base_email_context(self.request) context['to'] = self.target_user context['from'] = self.request.user context['comment'] = clean_html(self.cleaned_data['comment']) datatuples = [ (_('Message from %s') % self.request.user.get_profile().get_fullname(), render_to_string('agora_core/emails/user_mail.txt', context), render_to_string('agora_core/emails/user_mail.html', context), None, [self.target_user.email]) ] translation.deactivate() send_mass_html_mail(datatuples) return None
def clean_comment(self): """ If COMMENTS_ALLOW_PROFANITIES is False, check that the comment doesn't contain anything in PROFANITIES_LIST. """ if not self.request.user.is_authenticated(): raise django_forms.ValidationError(_("You must be authenticated to post a comment")) comment = clean_html(self.cleaned_data["comment"]) if settings.COMMENTS_ALLOW_PROFANITIES == False: bad_words = [w for w in settings.PROFANITIES_LIST if w in comment.lower()] if bad_words: plural = len(bad_words) > 1 raise django_forms.ValidationError(ungettext( "Watch your mouth! The word %s is not allowed here.", "Watch your mouth! The words %s are not allowed here.", plural) % \ get_text_list(['"%s%s%s"' % (i[0], '-'*(len(i)-2), i[-1]) for i in bad_words], 'and')) return comment
def clean_first_name(self): ''' Validates first_name field (which is actually user's full name). If its a FNMT authenticated user, this user cannot change the first name. ''' if 'first_name' not in self.data: return None first_name = clean_html(self.cleaned_data['first_name']) if '<' in first_name or '\"' in first_name: raise django_forms.ValidationError(_(u'Invalid first name.')) profile = self.request.user.get_profile() if isinstance(profile.extra, dict) and\ profile.extra.has_key('fnmt_cert') and\ self.request.user.first_name != first_name: raise django_forms.ValidationError( _('FNMT users cannot change their names.')) return first_name
def save(self): translation.activate(self.target_user.get_profile().lang_code) context = get_base_email_context(self.request) context['to'] = self.target_user context['from'] = self.request.user context['comment'] = clean_html(self.cleaned_data['comment']) datatuples= [( _('Message from %s') % self.request.user.get_profile().get_fullname(), render_to_string('agora_core/emails/user_mail.txt', context), render_to_string('agora_core/emails/user_mail.html', context), None, [self.target_user.email]) ] translation.deactivate() send_mass_html_mail(datatuples) return None
def save(self): translation.activate(self.target_user.get_profile().lang_code) context = get_base_email_context(self.request) context["to"] = self.target_user context["from"] = self.request.user context["comment"] = clean_html(self.cleaned_data["comment"]) datatuples = [ ( _("Message from %s") % self.request.user.get_profile().get_fullname(), render_to_string("agora_core/emails/user_mail.txt", context), render_to_string("agora_core/emails/user_mail.html", context), None, [self.target_user.email], ) ] translation.deactivate() send_mass_html_mail(datatuples) return None
def clean_comment(self): """ If COMMENTS_ALLOW_PROFANITIES is False, check that the comment doesn't contain anything in PROFANITIES_LIST. """ if not self.request.user.is_authenticated(): raise django_forms.ValidationError( _("You must be authenticated to post a comment")) comment = clean_html(self.cleaned_data["comment"]) if settings.COMMENTS_ALLOW_PROFANITIES == False: bad_words = [ w for w in settings.PROFANITIES_LIST if w in comment.lower() ] if bad_words: plural = len(bad_words) > 1 raise django_forms.ValidationError(ungettext( "Watch your mouth! The word %s is not allowed here.", "Watch your mouth! The words %s are not allowed here.", plural) % \ get_text_list(['"%s%s%s"' % (i[0], '-'*(len(i)-2), i[-1]) for i in bad_words], 'and')) return comment
def clean_short_description(self): return clean_html(self.cleaned_data['short_description'])