Beispiel #1
0
class UserCreationForm(RegistrationFormUniqueEmail):
    username = forms.CharField(widget=forms.TextInput(), )
    email = forms.EmailField()
    password1 = forms.CharField(label=_("Password"),
                                widget=forms.PasswordInput)
    password2 = forms.CharField(
        label=_("Password confirmation"),
        widget=forms.PasswordInput,
        help_text=_("Enter the same password as above, for verification."),
    )
    captcha = ReCaptchaField()
    subscribe_newsletter = forms.BooleanField(required=False)

    class Meta:
        model = get_user_model()
        fields = ("username", "email")

    def clean_username(self):
        username = self.cleaned_data.get("username", "").strip()
        if not is_valid_username(username):
            raise forms.ValidationError(
                "Nome de usuário pode conter apenas letras, números e '_' e não deve ser um documento"
            )
        elif username and User.objects.filter(
                username__iexact=username).exists():
            raise forms.ValidationError(
                "Nome de usuário já existente (escolha um diferente).")
        return username

    def clean_email(self):
        email = self.cleaned_data.get("email", "").strip().lower()
        if email and User.objects.filter(email__iexact=email).exists():
            raise forms.ValidationError(
                f"Usuário com o email {email} já cadastrado.")
        return email
Beispiel #2
0
class UserCreationForm(RegistrationFormUniqueEmail):
    username = forms.CharField(
        widget=forms.TextInput(attrs={"style": "text-transform: lowercase"}), )
    email = forms.EmailField()
    password1 = forms.CharField(label=_("Password"),
                                widget=forms.PasswordInput)
    password2 = forms.CharField(
        label=_("Password confirmation"),
        widget=forms.PasswordInput,
        help_text=_("Enter the same password as above, for verification."),
    )
    captcha = ReCaptchaField()
    subscribe_newsletter = forms.BooleanField(required=False)

    class Meta:
        model = get_user_model()
        fields = ("username", "email")

    def clean_username(self):
        username = self.cleaned_data.get("username", "")
        return username.lower()

    def clean_email(self):
        email = self.cleaned_data.get("email")
        if email:
            if get_user_model().objects.filter(email__iexact=email).exists():
                raise forms.ValidationError(
                    f"Usuário com o email {email} já cadastrado.")
        return email
Beispiel #3
0
class ContactForm(forms.Form):
    name = forms.CharField(required=True, label="Nome")
    email = forms.EmailField(required=True, label="E-mail")
    message = forms.CharField(
        required=True,
        label="Mensagem",
        widget=forms.Textarea(attrs={"class": "materialize-textarea"}),
    )
    captcha = ReCaptchaField()
Beispiel #4
0
class FeedbackForm(forms.Form):
    email = forms.EmailField(required=False)
    message = forms.CharField(widget=forms.Textarea())
    error = forms.CharField(required=False, widget=forms.HiddenInput)
    captcha = ReCaptchaField(label=_(u'captcha'))

    def __init__(self, *args, **kwargs):
        hide_captcha = kwargs.pop('hide_captcha', False)
        super(FeedbackForm, self).__init__(*args, **kwargs)
        if hide_captcha:
            del self.fields['captcha']

    def send(self, request):
        email = self.cleaned_data['email']
        message = self.cleaned_data['message']
        error = self.cleaned_data['error']
        user_agent_data = u'User agent: %s' % request.META.get(
            'HTTP_USER_AGENT')
        timestamp = u'Time: %s' % datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        version = u'Version: %s' % settings.PROJECT_VERSION
        commit = u'Commit: %s' % settings.LAST_COMMIT_GUID
        url = u'URL: %s' % request.META.get('HTTP_REFERER', '')
        user = u'Logged in: %s' % (request.user.is_authenticated()
                                   and request.user or u'not logged in')
        message = u'%s\n\n%s\n%s\n%s\n%s\n%s\n%s' % (
            message, user_agent_data, timestamp, version, commit, url, user)
        if error in ['404', '500']:
            message += u'\nPage type: %s' % error
            feedback_emails = [settings.FEEDBACK_ERROR_EMAIL]
        else:
            feedback_emails = settings.FEEDBACK_EMAILS
        headers = {'Reply-To': email} if email else None
        bcc = getattr(settings, 'EMAIL_BCC_LIST', [])
        if email:
            subject = '%s (from %s)' % (settings.FEEDBACK_SUBJECT, email)
        else:
            subject = settings.FEEDBACK_SUBJECT
        EmailMessage(subject, message, email, \
                         feedback_emails, headers=headers, bcc=bcc).send()

        if email:
            headers = {'Reply-To': settings.FEEDBACK_RESPONSE_EMAIL}
            body = render_to_string(settings.FEEDBACK_RESPONSE_TEMPLATE, {})
            email = EmailMessage(settings.FEEDBACK_RESPONSE_SUBJECT, body, \
                         settings.FEEDBACK_RESPONSE_EMAIL, [email], headers=headers, bcc=bcc)
            email.content_subtype = 'html'
            email.send()

    def get_errors(self):
        from django.utils.encoding import force_unicode
        output = {}
        for key, value in self.errors.items():
            output[key] = '/n'.join([force_unicode(i) for i in value])
        return output
Beispiel #5
0
class TokenApiManagementForm(forms.Form):
    captcha = ReCaptchaField()