from django import forms from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ from .models import Profile class UserRegistrationForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput, label=_("Password")) password_confirm = forms.CharField(widget=forms.PasswordInput, label=_("Confirm Password")) role = forms.ChoiceField(choices=Profile.ROLE_CHOICES, label=_("Register as")) phone_number = forms.CharField(max_length=20, label=_("Phone Number")) class Meta: model = User fields = ['username', 'email', 'first_name', 'last_name'] labels = { 'username': _('Username'), 'email': _('Email'), 'first_name': _('First Name'), 'last_name': _('Last Name'), } def clean_password_confirm(self): password = self.cleaned_data.get('password') password_confirm = self.cleaned_data.get('password_confirm') if password and password_confirm and password != password_confirm: raise forms.ValidationError(_("Passwords don't match")) return password_confirm def save(self, commit=True): user = super().save(commit=False) user.set_password(self.cleaned_data['password']) if commit: user.save() Profile.objects.create( user=user, role=self.cleaned_data['role'], phone_number=self.cleaned_data['phone_number'] ) return user