from datetime import datetime from django.core.mail import send_mail from django import forms from django.db import transaction from django.contrib.auth.forms import ( UserCreationForm, UserChangeForm, ) from django.contrib.auth.forms import PasswordResetForm from course.models import Program from .models import User, Student, Parent, RELATION_SHIP, LEVEL class StaffAddForm(UserCreationForm): username = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Username", required=False ) first_name = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="First Name", ) last_name = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Last Name", ) address = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Address", ) phone = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Mobile No.", ) email = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Email", ) password1 = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "password", "class": "form-control", } ), label="Password", required=False ) password2 = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "password", "class": "form-control", } ), label="Password Confirmation", required=False ) class Meta(UserCreationForm.Meta): model = User @transaction.atomic() def save(self, commit=True): user = super().save(commit=False) user.is_lecturer = True user.first_name = self.cleaned_data.get("first_name") user.last_name = self.cleaned_data.get("last_name") user.phone = self.cleaned_data.get("phone") user.address = self.cleaned_data.get("address") user.email = self.cleaned_data.get("email") # Generate a username based on first and last name and registration date registration_date = datetime.now().strftime('%Y%m%d%H%M') generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}" # Check if the generated username already exists, and regenerate if needed while User.objects.filter(username=generated_username).exists(): registration_date = datetime.now().strftime('%Y%m%d%H%M') generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(" ", "") user.username = generated_username generated_password = User.objects.make_random_password() user.set_password(generated_password) if commit: user.save() # Send email with the generated credentials send_mail( 'Your account credentials', f'Your username: {generated_username}\nYour password: {generated_password}', 'from@example.com', [user.email], fail_silently=False, ) return user class StudentAddForm(UserCreationForm): username = forms.CharField( max_length=30, widget=forms.TextInput( attrs={"type": "text", "class": "form-control", "id": "username_id"} ), label="Username", required=False ) address = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Address", ) phone = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Mobile No.", ) first_name = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="First name", ) last_name = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Last name", ) level = forms.CharField( widget=forms.Select( choices=LEVEL, attrs={ "class": "browser-default custom-select form-control", }, ), ) program = forms.ModelChoiceField( queryset=Program.objects.all(), widget=forms.Select( attrs={"class": "browser-default custom-select form-control"} ), label="Program", ) email = forms.EmailField( widget=forms.TextInput( attrs={ "type": "email", "class": "form-control", } ), label="Email Address", ) password1 = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "password", "class": "form-control", } ), label="Password", required=False ) password2 = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "password", "class": "form-control", } ), label="Password Confirmation", required=False ) # def validate_email(self): # email = self.cleaned_data['email'] # if User.objects.filter(email__iexact=email, is_active=True).exists(): # raise forms.ValidationError("Email has taken, try another email address. ") class Meta(UserCreationForm.Meta): model = User @transaction.atomic() def save(self, commit=True): user = super().save(commit=False) user.is_lecturer = True user.first_name = self.cleaned_data.get("first_name") user.last_name = self.cleaned_data.get("last_name") user.phone = self.cleaned_data.get("phone") user.address = self.cleaned_data.get("address") user.email = self.cleaned_data.get("email") # Generate a username based on first and last name and registration date registration_date = datetime.now().strftime('%Y%m%d%H%M') generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}" # Check if the generated username already exists, and regenerate if needed while User.objects.filter(username=generated_username).exists(): registration_date = datetime.now().strftime('%Y%m%d%H%M') generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(" ", "") user.username = generated_username generated_password = User.objects.make_random_password() user.set_password(generated_password) if commit: user.save() # Send email with the generated credentials send_mail( 'Your account credentials', f'Your username: {generated_username}\nYour password: {generated_password}', 'from@example.com', [user.email], fail_silently=False, ) return user class ProfileUpdateForm(UserChangeForm): email = forms.EmailField( widget=forms.TextInput( attrs={ "type": "email", "class": "form-control", } ), label="Email Address", ) first_name = forms.CharField( widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="First Name", ) last_name = forms.CharField( widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Last Name", ) phone = forms.CharField( widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Phone No.", ) address = forms.CharField( widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Address / city", ) class Meta: model = User fields = ["email", "phone", "address", "picture", "first_name", "last_name"] class EmailValidationOnForgotPassword(PasswordResetForm): def clean_email(self): email = self.cleaned_data["email"] if not User.objects.filter(email__iexact=email, is_active=True).exists(): msg = "There is no user registered with the specified E-mail address. " self.add_error("email", msg) return email class ParentAddForm(UserCreationForm): username = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Username", ) address = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Address", ) phone = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Mobile No.", ) first_name = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="First name", ) last_name = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "text", "class": "form-control", } ), label="Last name", ) email = forms.EmailField( widget=forms.TextInput( attrs={ "type": "email", "class": "form-control", } ), label="Email Address", ) student = forms.ModelChoiceField( queryset=Student.objects.all(), widget=forms.Select( attrs={"class": "browser-default custom-select form-control"} ), label="Student", ) relation_ship = forms.CharField( widget=forms.Select( choices=RELATION_SHIP, attrs={ "class": "browser-default custom-select form-control", }, ), ) password1 = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "password", "class": "form-control", } ), label="Password", ) password2 = forms.CharField( max_length=30, widget=forms.TextInput( attrs={ "type": "password", "class": "form-control", } ), label="Password Confirmation", ) # def validate_email(self): # email = self.cleaned_data['email'] # if User.objects.filter(email__iexact=email, is_active=True).exists(): # raise forms.ValidationError("Email has taken, try another email address. ") class Meta(UserCreationForm.Meta): model = User @transaction.atomic() def save(self): user = super().save(commit=False) user.is_parent = True user.first_name = self.cleaned_data.get("first_name") user.last_name = self.cleaned_data.get("last_name") user.address = self.cleaned_data.get("address") user.phone = self.cleaned_data.get("phone") user.email = self.cleaned_data.get("email") user.save() parent = Parent.objects.create( user=user, student=self.cleaned_data.get("student"), relation_ship=self.cleaned_data.get("relation_ship"), ) parent.save() return user