Last active
July 3, 2019 22:34
-
-
Save rhenter/13862c36a3be0a3fca355d51929e6446 to your computer and use it in GitHub Desktop.
Criação de Modelo de Usuário Customizado
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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