/geoxp/latlong/forms.py

https://gitlab.com/geoxperience/geoxp · Python · 52 lines · 35 code · 10 blank · 7 comment · 5 complexity · e61a66f08828a2ee331a5aa56ee142c9 MD5 · raw file

  1. # coding:utf-8
  2. from django import forms
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.contrib.auth.models import User
  5. class UploadFileForm(forms.Form):
  6. ''' Represents an upload file form to receive the .csv file as input '''
  7. attrs = {
  8. 'class': 'button',
  9. }
  10. file = forms.FileField(label='', widget=forms.FileInput(attrs=attrs))
  11. class LoginForm(forms.Form):
  12. ''' Represents a login form that receives user login info '''
  13. username = forms.CharField(label='Usuário', max_length=50)
  14. password = forms.CharField(
  15. widget=forms.PasswordInput, label='Senha', max_length=20)
  16. class RegistrationForm(forms.Form):
  17. ''' Represents a registration form that will receive user sign up info '''
  18. email = forms.EmailField(widget=forms.TextInput(
  19. attrs=dict(required=True, max_length=30)), label=_("E-mail:"))
  20. username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=50)), label=_(
  21. u"Usuário:"), error_messages={'invalid': _("Este campo aceita apenas letras, números e underline.")})
  22. password1 = forms.CharField(widget=forms.PasswordInput(
  23. attrs=dict(required=True, max_length=20, render_value=False)), label=_("Senha:"))
  24. password2 = forms.CharField(widget=forms.PasswordInput(
  25. attrs=dict(required=True, max_length=20, render_value=False)), label=_("Confirmar senha:"))
  26. def clean_username(self):
  27. ''' Checks if username already exists in database '''
  28. try:
  29. user = User.objects.get(
  30. username__iexact=self.cleaned_data['username'])
  31. except User.DoesNotExist:
  32. return self.cleaned_data['username']
  33. raise forms.ValidationError(
  34. _(u'Usuário já existe'))
  35. def clean(self):
  36. ''' Compares the password inputs and returns an error if they
  37. do not match '''
  38. if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
  39. if self.cleaned_data['password1'] != self.cleaned_data['password2']:
  40. raise forms.ValidationError(
  41. _(u'As duas senhas não são iguais.'))
  42. return self.cleaned_data