def clean(self): data = super(GetUserForm, self).clean() email = data.get('email') if not email or len(email) > 250: raise forms.ValidationError(_("Enter e-mail address."), code='empty_email') try: validate_email(email) except forms.ValidationError: raise forms.ValidationError(_("Entered e-mail is invalid."), code='invalid_email') try: User = get_user_model() user = User.objects.get_by_email(data['email']) self.user_cache = user except User.DoesNotExist: raise forms.ValidationError(_("No user with this e-mail exists."), code='not_found') self.confirm_allowed(user) return data
def clean(self): data = super(MovePostsForm, self).clean() new_thread_url = data.get('new_thread_url') try: if not new_thread_url: raise Http404() resolution = resolve(urlparse(new_thread_url).path) if not 'thread_id' in resolution.kwargs: raise Http404() queryset = Thread.objects.select_related('forum') self.new_thread = queryset.get(id=resolution.kwargs['thread_id']) add_acl(self.user, self.new_thread.forum) add_acl(self.user, self.new_thread) allow_see_forum(self.user, self.new_thread.forum) allow_browse_forum(self.user, self.new_thread.forum) allow_see_thread(self.user, self.new_thread) except (Http404, Thread.DoesNotExist): message = _("You have to enter valid link to thread.") raise forms.ValidationError(message) if self.thread == self.new_thread: message = _("New thread is same as current one.") raise forms.ValidationError(message) if self.new_thread.forum.special_role: message = _("You can't move posts to special threads.") raise forms.ValidationError(message) return data
def validate_post(self, post): post_len = len(post) if not post_len: raise forms.ValidationError(_("Enter message.")) if post_len < settings.post_length_min: message = ungettext( "Posted message should be at least %(limit)s character long.", "Posted message should be at least %(limit)s characters long.", settings.post_length_min) message = message % {'limit': settings.post_length_min} raise forms.ValidationError(message) if settings.post_length_max and post_len > settings.post_length_max: message = ungettext( "Posted message can't be longer than %(limit)s character.", "Posted message can't be longer than %(limit)s characters.", settings.post_length_max, ) message = message % {'limit': settings.post_length_max} raise forms.ValidationError(message) self.parsing_result = common_flavour(post, self.post_instance.poster) self.post_instance.original = self.parsing_result['original_text'] self.post_instance.parsed = self.parsing_result['parsed_text']
def clean(self): data = super(ChangeEmailPasswordForm, self).clean() current_password = data.get('current_password') new_email = data.get('new_email') new_password = data.get('new_password') if not data.get('current_password'): message = _("You have to enter your current password.") raise forms.ValidationError(message) if not self.user.check_password(current_password): raise forms.ValidationError(_("Entered password is invalid.")) if not (new_email or new_password): message = _("You have to enter new e-mail or password.") raise forms.ValidationError(message) if new_email: if new_email.lower() == self.user.email.lower(): message = _("New e-mail is same as current one.") raise forms.ValidationError(message) validate_email(new_email) if new_password: validate_password(new_password) return data
def confirm_allowed(self, user): self.confirm_user_not_banned(user) username_format = {'user': user.username} if not user.requires_activation: message = _("%(user)s, your account is already active.") raise forms.ValidationError(message % username_format) if user.requires_activation_by_admin: message = _("%(user)s, only administrator may activate " "your account.") raise forms.ValidationError(message % username_format)
def confirm_can_be_activated(self, user): username_format = {'user': user.username} if not user.requires_activation: message = _("%(user)s, your account is already active.") raise forms.ValidationError(message % username_format, code='already_active') if user.requires_activation_by_admin: message = _("%(user)s, only administrator may activate " "your account.") raise forms.ValidationError(message % username_format, code='inactive_admin')
def recaptcha_test(request): r = requests.post('https://www.google.com/recaptcha/api/siteverify', data={ 'secret': settings.recaptcha_secret_key, 'response': request.data.get('captcha'), 'remoteip': request.user_ip }) if r.status_code == 200: response_json = r.json() if not response_json.get('success'): raise forms.ValidationError(_("Please try again.")) else: raise forms.ValidationError(_("Failed to contact reCAPTCHA API."))
def clean(self): data = super(ChangeUsernameForm, self).clean() username = data.get('username') if not username: raise forms.ValidationError(_("Enter new username.")) if username == self.user.username: message = _("New username is same as current one.") raise forms.ValidationError(message) validate_username(username, exclude=self.user) return data
def clean_new_email(self): data = self.cleaned_data['new_email'] if not data: message = _("You have to enter new e-mail address.") raise forms.ValidationError(message) if data.lower() == self.user.email.lower(): message = _("New e-mail is same as current one.") raise forms.ValidationError(message) validate_email(data) return data
def clean(self): cleaned_data = super(ThreadParticipantsForm, self).clean() if not cleaned_data.get('users'): raise forms.ValidationError( _("You have to specify message recipients.")) clean_usernames = [] for name in cleaned_data['users'].split(','): clean_name = name.strip().lower() if clean_name == self.user.slug: raise forms.ValidationError( _("You can't addres message to yourself.")) if clean_name not in clean_usernames: clean_usernames.append(clean_name) max_participants = self.user.acl['max_private_thread_participants'] if max_participants and len(clean_usernames) > max_participants: message = ungettext( "You can't start private thread " "with more than than %(users)s user.", "You can't start private thread " "with more than than %(users)s users.", max_participants) message = message % {'users': max_participants} raise forms.ValidationError(message) users_qs = get_user_model().objects.filter(slug__in=clean_usernames) for user in users_qs: try: allow_message_user(self.user, user) except PermissionDenied as e: raise forms.ValidationError(unicode(e)) self.users_cache.append(user) if len(self.users_cache) != len(clean_usernames): valid_usernames = [u.slug for u in self.users_cache] invalid_usernames = [] for username in clean_usernames: if username not in valid_usernames: invalid_usernames.append(username) message = _("One or more message recipients could " "not be found: %(usernames)s") formats = {'usernames': ', '.join(invalid_usernames)} raise forms.ValidationError(message % formats) valid_usernames = [u.username for u in self.users_cache] cleaned_data['users'] = ','.join(valid_usernames) return cleaned_data
def clean(self): data = super(EditSignatureForm, self).clean() if len(data.get('signature', '')) > settings.signature_length_max: raise forms.ValidationError(_("Signature is too long.")) return data
def clean_expires_on(self): data = self.cleaned_data['expires_on'] if self.user.acl_['max_ban_length']: max_ban_length = timedelta(days=self.user.acl_['max_ban_length']) if not data or data > (timezone.now() + max_ban_length).date(): message = ungettext( "You can't set bans longer than %(days)s day.", "You can't set bans longer than %(days)s days.", self.user.acl_['max_ban_length']) message = message % {'days': self.user.acl_['max_ban_length']} raise forms.ValidationError(message) elif data and data < timezone.now().date(): raise forms.ValidationError(_("Expiration date is in past.")) return data
def clean_recaptcha(self): if not self.data.get('recaptcha_response_field'): raise forms.ValidationError(_("This field is required.")) api_response = submit_recaptcha(self.data.get('recaptcha_challenge_field'), self.data.get('recaptcha_response_field'), settings.recaptcha_private_api_key, self._misago_real_ip) if api_response.is_valid: self.has_recaptcha = False mark_session_as_passing(self.session) else: raise forms.ValidationError(_("Text from image is incorrect.")) return ''
def clean(self): data = super(GetUserForm, self).clean() credential = data.get('username') if not credential or len(credential) > 250: raise forms.ValidationError(_("You have to fill out form.")) try: User = get_user_model() user = User.objects.get_by_username_or_email(data['username']) self.user_cache = user except User.DoesNotExist: raise forms.ValidationError(_("Invalid username or e-mail.")) self.confirm_allowed(user) return data
def clean(self): data = super(MoveThreadsForm, self).clean() new_forum = data.get('new_forum') if new_forum: if new_forum.is_category: message = _("You can't move threads to category.") raise forms.ValidationError(message) if new_forum.is_redirect: message = _("You can't move threads to redirect.") raise forms.ValidationError(message) if new_forum.pk == self.forum.pk: message = _("New forum is same as current one.") raise forms.ValidationError(message) else: raise forms.ValidationError(_("You have to select forum.")) return data
def qacaptcha_test(request): answer = request.data.get('captcha', '').lower() for predefined_answer in settings.qa_answers.lower().splitlines(): predefined_answer = predefined_answer.strip().lower() if answer == predefined_answer: break else: raise forms.ValidationError(_("Entered answer is incorrect."))
def clean_banned_value(self): data = self.cleaned_data['banned_value'] while '**' in data: data = data.replace('**', '*') if data == '*': raise forms.ValidationError(_("Banned value is too vague.")) return data
def validate_data(self, data): errors = [] if not data.get('title') and not data.get('post'): raise forms.ValidationError(_("Enter thread title and message.")) try: validate_title(data.get('title', '')) except forms.ValidationError as e: errors.append(e) try: self.validate_post(data.get('post', '')) except forms.ValidationError as e: errors.append(e) if errors: raise forms.ValidationError(errors)
def clean(self): data = super(SetNewPasswordForm, self).clean() new_password = data.get('new_password') if not new_password or len(new_password) > 250: raise forms.ValidationError(_("You have to fill out form.")) validate_password(new_password) return data
def clean(self): data = super(MergeThreadsForm, self).clean() merged_thread_title = data.get('merged_thread_title') if merged_thread_title: validate_title(merged_thread_title) else: message = _("You have to enter merged thread title.") raise forms.ValidationError(message) return data
def _validate_post(self, post): post_len = len(post) if not post_len: raise forms.ValidationError(_("Enter message.")) if post_len < settings.post_length_min: message = ungettext( "Posted message should be at least %(limit)s character long.", "Posted message should be at least %(limit)s characters long.", settings.post_length_min) message = message % {'limit': settings.post_length_min} raise forms.ValidationError(message) if settings.post_length_max and post_len > settings.post_length_max: message = ungettext( "Posted message can't be longer than %(limit)s character.", "Posted message can't be longer than %(limit)s characters.", settings.post_length_max) message = message % {'limit': settings.post_length_max} raise forms.ValidationError(message)
def clean_signature(self): data = self.cleaned_data['signature'] length_limit = settings.signature_length_max if len(data) > length_limit: raise forms.ValidationError(ungettext( "Signature can't be longer than %(limit)s character.", "Signature can't be longer than %(limit)s characters.", length_limit) % {'limit': length_limit}) return data
def clean_roles(self): data = self.cleaned_data['roles'] for role in data: if role.special_role == 'authenticated': break else: message = _('All registered members must have "Member" role.') raise forms.ValidationError(message) return data
def clean_qa_answer(self): answer = self.cleaned_data['qa_answer'].lower() for predefined_answer in settings.qa_answers.lower().splitlines(): predefined_answer = predefined_answer.strip().lower() if answer == predefined_answer: self.has_qa_captcha = False mark_session_as_passing(self.session) return self.cleaned_data['qa_answer'] else: raise forms.ValidationError(_("Entered answer is incorrect."))
def __call__(self, data): data_len = len(data) if self.min_choices and self.min_choices > data_len: message = ungettext( 'You have to select at least %(choices)d option.', 'You have to select at least %(choices)d options.', self.min_choices) message = message % {'choices': self.min_choices} raise forms.ValidationError(message) if self.max_choices and self.max_choices < data_len: message = ungettext( 'You cannot select more than %(choices)d option.', 'You cannot select more than %(choices)d options.', self.max_choices) message = message % {'choices': self.max_choices} raise forms.ValidationError(message) return data
def clean(self): data = super(EditSignatureForm, self).clean() length_limit = settings.signature_length_max if len(data) > length_limit: raise forms.ValidationError( ungettext( "Signature can't be longer than %(limit)s character.", "Signature can't be longer than %(limit)s characters.", length_limit) % {'limit': length_limit}) return data
def clean(self): data = super(SplitThreadForm, self).clean() forum = data.get('forum') if forum: if forum.is_category: message = _("You can't start threads in category.") raise forms.ValidationError(message) if forum.is_redirect: message = _("You can't start threads in redirect.") raise forms.ValidationError(message) else: raise forms.ValidationError(_("You have to select forum.")) thread_title = data.get('thread_title') if thread_title: validate_title(thread_title) else: message = _("You have to enter new thread title.") raise forms.ValidationError(message) return data
def clean_name(self): data = self.cleaned_data['name'] self.instance.set_name(data) unique_qs = Rank.objects.filter(slug=self.instance.slug) if self.instance.pk: unique_qs = unique_qs.exclude(pk=self.instance.pk) if unique_qs.exists(): raise forms.ValidationError( _("This name collides with other rank.")) return data
def clean_reason(self): data = self.cleaned_data['reason'] if len(data) > 2000: message = _("Warning reason can't be longer than 2000 characters.") raise forms.ValidationError(message) return data
def confirm_login_allowed(self, user): if not user.is_staff: raise forms.ValidationError( self.error_messages['not_staff'], code='not_staff', )