diff --git a/core/__pycache__/admin.cpython-311.pyc b/core/__pycache__/admin.cpython-311.pyc index 63ef292..8577f3a 100644 Binary files a/core/__pycache__/admin.cpython-311.pyc and b/core/__pycache__/admin.cpython-311.pyc differ diff --git a/core/__pycache__/forms.cpython-311.pyc b/core/__pycache__/forms.cpython-311.pyc index 9dfcf9b..af0d53c 100644 Binary files a/core/__pycache__/forms.cpython-311.pyc and b/core/__pycache__/forms.cpython-311.pyc differ diff --git a/core/__pycache__/models.cpython-311.pyc b/core/__pycache__/models.cpython-311.pyc index 63bd88a..be16afb 100644 Binary files a/core/__pycache__/models.cpython-311.pyc and b/core/__pycache__/models.cpython-311.pyc differ diff --git a/core/admin.py b/core/admin.py index eeb9c21..53798df 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,5 +1,10 @@ from django.contrib import admin -from .models import Profile, Truck, Shipment, Bid, Message, WhatsAppConfig +from .models import Profile, Truck, Shipment, Bid, Message, WhatsAppConfig, Country + +@admin.register(Country) +class CountryAdmin(admin.ModelAdmin): + list_display = ('name', 'code', 'is_default') + list_editable = ('is_default',) @admin.register(Profile) class ProfileAdmin(admin.ModelAdmin): @@ -37,4 +42,4 @@ class WhatsAppConfigAdmin(admin.ModelAdmin): # Only allow one configuration record if self.model.objects.exists(): return False - return super().has_add_permission(request) \ No newline at end of file + return super().has_add_permission(request) diff --git a/core/forms.py b/core/forms.py index cd392a4..e14a845 100644 --- a/core/forms.py +++ b/core/forms.py @@ -1,5 +1,5 @@ from django import forms -from .models import Truck, Shipment, Bid, Profile +from .models import Truck, Shipment, Bid, Profile, Country from django.utils.translation import gettext_lazy as _ from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User @@ -8,8 +8,8 @@ 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'})) - country_code = forms.CharField(max_length=5, required=True, initial='966', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '966'})) - phone_number = forms.CharField(max_length=20, required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '+1234567890'})) + country_code = forms.ChoiceField(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': '123456789'})) class Meta(UserCreationForm.Meta): model = User @@ -17,6 +17,18 @@ class UserRegistrationForm(UserCreationForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + # Dynamic country codes from the database + countries = Country.objects.all() + if countries.exists(): + self.fields['country_code'].choices = [(c.code, str(c)) for c in countries] + default_country = countries.filter(is_default=True).first() + if default_country: + self.fields['country_code'].initial = default_country.code + else: + # Fallback if no countries are defined yet + self.fields['country_code'].choices = [('966', 'Saudi Arabia (+966)')] + self.fields['country_code'].initial = '966' + for field in self.fields.values(): if not isinstance(field.widget, (forms.CheckboxInput, forms.Select)): field.widget.attrs.update({'class': 'form-control'}) @@ -86,4 +98,4 @@ class BidForm(forms.ModelForm): # Only allow bidding with approved trucks self.fields['truck'].queryset = Truck.objects.filter(owner=user, is_approved=True) if not self.fields['truck'].queryset.exists(): - self.fields['truck'].help_text = _("You must have an approved truck to place a bid.") \ No newline at end of file + self.fields['truck'].help_text = _("You must have an approved truck to place a bid.") diff --git a/core/migrations/0008_country.py b/core/migrations/0008_country.py new file mode 100644 index 0000000..4246a80 --- /dev/null +++ b/core/migrations/0008_country.py @@ -0,0 +1,26 @@ +# Generated by Django 5.2.7 on 2026-01-23 12:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0007_profile_country_code'), + ] + + operations = [ + migrations.CreateModel( + name='Country', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100, verbose_name='Country Name')), + ('code', models.CharField(max_length=10, verbose_name='Country Code')), + ('is_default', models.BooleanField(default=False, verbose_name='Is Default')), + ], + options={ + 'verbose_name': 'Country', + 'verbose_name_plural': 'Countries', + }, + ), + ] diff --git a/core/migrations/__pycache__/0008_country.cpython-311.pyc b/core/migrations/__pycache__/0008_country.cpython-311.pyc new file mode 100644 index 0000000..78db181 Binary files /dev/null and b/core/migrations/__pycache__/0008_country.cpython-311.pyc differ diff --git a/core/models.py b/core/models.py index 3eb0964..e063def 100644 --- a/core/models.py +++ b/core/models.py @@ -5,6 +5,18 @@ from django.dispatch import receiver from django.utils.translation import gettext_lazy as _ from django.utils.translation import get_language +class Country(models.Model): + name = models.CharField(_('Country Name'), max_length=100) + code = models.CharField(_('Country Code'), max_length=10) + is_default = models.BooleanField(_('Is Default'), default=False) + + class Meta: + verbose_name = _('Country') + verbose_name_plural = _('Countries') + + def __str__(self): + return f"{self.name} (+{self.code})" + class Profile(models.Model): ROLE_CHOICES = ( ('SHIPPER', _('Shipper (Need Goods Moved)')), @@ -56,15 +68,6 @@ class Truck(models.Model): is_approved = models.BooleanField(_('Is Approved'), default=False) created_at = models.DateTimeField(auto_now_add=True) - @property - def full_phone_number(self): - if not self.phone_number: - return "" - # Remove any existing leading + from country code and phone number - cc = str(self.country_code).replace("+", "").strip() - pn = str(self.phone_number).replace("+", "").strip() - return f"{cc}{pn}" - def __str__(self): return f"{self.display_truck_type} - {self.plate_no}" @@ -111,15 +114,6 @@ class Shipment(models.Model): created_at = models.DateTimeField(auto_now_add=True) - @property - def full_phone_number(self): - if not self.phone_number: - return "" - # Remove any existing leading + from country code and phone number - cc = str(self.country_code).replace("+", "").strip() - pn = str(self.phone_number).replace("+", "").strip() - return f"{cc}{pn}" - def __str__(self): return f"{self.origin} to {self.destination} - {self.status}" @@ -138,15 +132,6 @@ class Bid(models.Model): created_at = models.DateTimeField(auto_now_add=True) - @property - def full_phone_number(self): - if not self.phone_number: - return "" - # Remove any existing leading + from country code and phone number - cc = str(self.country_code).replace("+", "").strip() - pn = str(self.phone_number).replace("+", "").strip() - return f"{cc}{pn}" - def __str__(self): return f"Bid by {self.truck_owner.username} for {self.shipment}" @@ -156,15 +141,6 @@ class Message(models.Model): content = models.TextField() timestamp = models.DateTimeField(auto_now_add=True) - @property - def full_phone_number(self): - if not self.phone_number: - return "" - # Remove any existing leading + from country code and phone number - cc = str(self.country_code).replace("+", "").strip() - pn = str(self.phone_number).replace("+", "").strip() - return f"{cc}{pn}" - def __str__(self): return f"From {self.sender.username} at {self.timestamp}" @@ -177,15 +153,6 @@ class WhatsAppConfig(models.Model): verbose_name = _('WhatsApp Configuration') verbose_name_plural = _('WhatsApp Configuration') - @property - def full_phone_number(self): - if not self.phone_number: - return "" - # Remove any existing leading + from country code and phone number - cc = str(self.country_code).replace("+", "").strip() - pn = str(self.phone_number).replace("+", "").strip() - return f"{cc}{pn}" - def __str__(self): return str(_("WhatsApp Configuration")) @@ -199,4 +166,4 @@ def save_user_profile(sender, instance, **kwargs): if hasattr(instance, 'profile'): instance.profile.save() else: - Profile.objects.create(user=instance) \ No newline at end of file + Profile.objects.create(user=instance) diff --git a/core/templates/registration/register.html b/core/templates/registration/register.html index 099c746..c59665a 100644 --- a/core/templates/registration/register.html +++ b/core/templates/registration/register.html @@ -27,7 +27,7 @@