diff --git a/core/__pycache__/forms.cpython-311.pyc b/core/__pycache__/forms.cpython-311.pyc index ba213a6..5638d92 100644 Binary files a/core/__pycache__/forms.cpython-311.pyc and b/core/__pycache__/forms.cpython-311.pyc differ diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index 00c55ae..c979782 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ diff --git a/core/forms.py b/core/forms.py index a294740..5970473 100644 --- a/core/forms.py +++ b/core/forms.py @@ -1,6 +1,34 @@ from django import forms -from .models import Truck, Shipment, Bid +from .models import Truck, Shipment, Bid, Profile from django.utils.translation import gettext_lazy as _ +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + +class UserRegistrationForm(UserCreationForm): + email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'})) + confirm_email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}), label=_("Confirm Email")) + role = forms.ChoiceField(choices=Profile.ROLE_CHOICES, widget=forms.Select(attrs={'class': 'form-select'})) + phone_number = forms.CharField(max_length=20, required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '+1234567890'})) + + class Meta(UserCreationForm.Meta): + model = User + fields = UserCreationForm.Meta.fields + ('email',) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + for field in self.fields.values(): + if not isinstance(field.widget, (forms.CheckboxInput, forms.Select)): + field.widget.attrs.update({'class': 'form-control'}) + + def clean(self): + cleaned_data = super().clean() + email = cleaned_data.get("email") + confirm_email = cleaned_data.get("confirm_email") + + if email and confirm_email and email != confirm_email: + self.add_error('confirm_email', _("The two email fields didn't match.")) + + return cleaned_data class TruckForm(forms.ModelForm): class Meta: diff --git a/core/templates/registration/register.html b/core/templates/registration/register.html index b5eab21..6e0ea86 100644 --- a/core/templates/registration/register.html +++ b/core/templates/registration/register.html @@ -9,24 +9,34 @@
{% trans "Already have an account?" %} {% trans "Login" %}
diff --git a/core/views.py b/core/views.py index 32e746d..256a0f9 100644 --- a/core/views.py +++ b/core/views.py @@ -1,10 +1,9 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib.auth import login, authenticate -from django.contrib.auth.forms import UserCreationForm from django.utils import timezone from .models import Profile, Truck, Shipment, Bid, Message -from .forms import TruckForm, ShipmentForm, BidForm +from .forms import TruckForm, ShipmentForm, BidForm, UserRegistrationForm from django.contrib import messages from django.utils.translation import gettext as _ from django.db.models import Q @@ -19,19 +18,20 @@ def home(request): def register(request): if request.method == 'POST': - form = UserCreationForm(request.POST) - role = request.POST.get('role') - phone = request.POST.get('phone_number') + form = UserRegistrationForm(request.POST) if form.is_valid(): user = form.save() profile = user.profile - profile.role = role - profile.phone_number = phone + profile.role = form.cleaned_data.get('role') + profile.phone_number = form.cleaned_data.get('phone_number') profile.save() login(request, user) + messages.success(request, _("Registration successful. Welcome!")) return redirect('dashboard') + else: + messages.error(request, _("Please correct the errors below.")) else: - form = UserCreationForm() + form = UserRegistrationForm() return render(request, 'registration/register.html', {'form': form}) @login_required @@ -85,24 +85,19 @@ def post_shipment(request): return redirect('dashboard') if request.method == 'POST': - description = request.POST.get('description') - weight = request.POST.get('weight') - origin = request.POST.get('origin') - destination = request.POST.get('destination') - delivery_date = request.POST.get('delivery_date') - - Shipment.objects.create( - shipper=request.user, - description=description, - weight=weight, - origin=origin, - destination=destination, - delivery_date=delivery_date - ) - messages.success(request, _("Shipment posted successfully!")) - return redirect('dashboard') + form = ShipmentForm(request.POST) + if form.is_valid(): + shipment = form.save(commit=False) + shipment.shipper = request.user + shipment.save() + messages.success(request, _("Shipment posted successfully!")) + return redirect('dashboard') + else: + messages.error(request, _("Please correct the errors in the form.")) + else: + form = ShipmentForm() - return render(request, 'core/post_shipment.html') + return render(request, 'core/post_shipment.html', {'form': form}) @login_required def marketplace(request): @@ -164,4 +159,4 @@ def accept_bid(request, bid_id): bid.shipment.save() messages.success(request, _("Bid accepted! Shipment is now in progress.")) - return redirect('shipment_detail', shipment_id=bid.shipment.id) + return redirect('shipment_detail', shipment_id=bid.shipment.id) \ No newline at end of file