Guest User

Untitled

a guest
Jun 14th, 2018
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. class UserForm(forms.ModelForm):
  2. password1 = forms.CharField(label='Senha')
  3. password2 = forms.CharField(label='Confirmar Senha')
  4.  
  5. def clean_password2(self):
  6. password1 = self.cleaned_data.get("password1")
  7. password2 = self.cleaned_data.get("password2")
  8. if password1 and password2 and password1 != password2:
  9. raise forms.ValidationError('Senhas não conferem')
  10. return password2
  11.  
  12. def save(self, commit=True):
  13. user = super(UserForm, self).save(commit=False)
  14. user.set_password(self.cleaned_data['password1'])
  15. if commit:
  16. user.save()
  17. return user
  18.  
  19. class Meta:
  20. model = User
  21. fields = ['first_name', 'last_name', 'email']
  22.  
  23. class UserAdminCreationForm(UserCreationForm):
  24. class Meta:
  25. model = UserAdmin
  26. fields = ['first_name', 'last_name', 'email', 'active', 'is_staff', 'is_admin']
  27.  
  28. class UserAdminForm(forms.ModelForm):
  29. class Meta:
  30. model = UserAdmin
  31. fields = ['first_name', 'last_name', 'email']
  32.  
  33. from django.shortcuts import render, redirect
  34. from .forms import UserForm
  35. from django.http import HttpResponse
  36. from django.contrib.auth import authenticate, login, logout, update_session_auth_hash
  37. from django.contrib.auth.forms import PasswordChangeForm
  38. from django.contrib import messages
  39.  
  40. def add_user(request):
  41. if request.method == 'POST':
  42. form = UserForm(request.POST)
  43. if form.is_valid():
  44. u = form.save()
  45. u.set_password(u.password)
  46. u.save()
  47. return HttpResponse('Usuario Cadastrado!')
  48. else:
  49. form = UserForm()
  50. return render(request, 'auth/registro.html', {'form': form})
  51.  
  52. def user_login(request):
  53. if request.method == 'POST':
  54. email = request.POST.get('email')
  55. password = request.POST.get('password')
  56.  
  57. user = authenticate(email=email, password=password)
  58.  
  59. if user:
  60. login(request, user)
  61. return redirect(request.GET.get('next', 'financas:home'))
  62. else:
  63. messages.error(request, 'Usuário ou senha inválidos')
  64. return render(request, 'auth/login.html')
Add Comment
Please, Sign In to add comment