Skip to content

Instantly share code, notes, and snippets.

@rhenter
Last active July 3, 2019 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhenter/13862c36a3be0a3fca355d51929e6446 to your computer and use it in GitHub Desktop.
Save rhenter/13862c36a3be0a3fca355d51929e6446 to your computer and use it in GitHub Desktop.
Criação de Modelo de Usuário Customizado
from django import forms
from django.contrib.auth import get_user_model, password_validation
User = get_user_model()
class RegisterForm(forms.ModelForm):
password2 = forms.CharField(
label="Confirmacao",
widget=forms.PasswordInput(attrs={'class': 'form-control'}),
help_text=_("Entre com a mesma senha acima para verificação.")
)
class Meta:
model = User
fields = [
'first_name',
'last_name',
'email',
'username',
]
def clean_password(self):
password1 = self.cleaned_data['password']
password_validation.validate_password(password1)
return password1
def clean_password2(self):
password1 = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
"As 2 senhas não conferem.",
code='password_mismatch'
)
return password2
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
role = models.BooleanField(default=False)
def __str__(self):
return self.username
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from .models import User
from .forms import UserForm
class UserCreateView(CreateView):
form_class = RegisterForm
template_name = "../templates/register.html"
model = User
success_url = reverse_lazy('qnow_client:quotation_client')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['origin'] = origin
context['active_page_client_provider'] = active'
def form_valid(self, form):
password = form.cleaned_data["password"]
obj = form.save()
obj.set_password(password)
obj.save()
return super().form_valid(form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment