diff --git a/sahara/db.sqlite3 b/sahara/db.sqlite3 index 4a9c6a5..27c2ebd 100644 Binary files a/sahara/db.sqlite3 and b/sahara/db.sqlite3 differ diff --git a/sahara/main/__pycache__/forms.cpython-312.pyc b/sahara/main/__pycache__/forms.cpython-312.pyc index 9e091ac..801719f 100644 Binary files a/sahara/main/__pycache__/forms.cpython-312.pyc and b/sahara/main/__pycache__/forms.cpython-312.pyc differ diff --git a/sahara/main/__pycache__/views.cpython-312.pyc b/sahara/main/__pycache__/views.cpython-312.pyc index 0b69e7e..d1a1e12 100644 Binary files a/sahara/main/__pycache__/views.cpython-312.pyc and b/sahara/main/__pycache__/views.cpython-312.pyc differ diff --git a/sahara/main/forms.py b/sahara/main/forms.py index acce591..bbd69ca 100644 --- a/sahara/main/forms.py +++ b/sahara/main/forms.py @@ -1,12 +1,8 @@ from django import forms -from django.contrib.auth.forms import AuthenticationForm +from django.core.exceptions import ValidationError from .models import User -from django import forms -from .models import User, Service - class UserRegistrationForm(forms.ModelForm): - # Password and confirm password fields with custom classes password = forms.CharField( widget=forms.PasswordInput(attrs={ 'class': 'form-control', @@ -22,7 +18,8 @@ class UserRegistrationForm(forms.ModelForm): class Meta: model = User - fields = ['email', 'first_name', 'last_name', 'profile', 'price', 'bio', 'service_offered', 'login_profile'] + fields = ['email', 'first_name', 'last_name', 'profile', 'price', + 'bio', 'service_offered', 'login_profile'] widgets = { 'first_name': forms.TextInput(attrs={ 'class': 'form-control', @@ -43,7 +40,7 @@ class UserRegistrationForm(forms.ModelForm): 'bio': forms.Textarea(attrs={ 'class': 'form-control', 'placeholder': 'Enter your bio', - 'rows': 4, # You can customize the rows and columns as needed + 'rows': 4, }), 'price': forms.NumberInput(attrs={ 'class': 'form-control', @@ -57,22 +54,35 @@ class UserRegistrationForm(forms.ModelForm): }), } + def clean_email(self): + email = self.cleaned_data.get('email') + if User.objects.filter(email=email).exists(): + raise ValidationError('Email is already registered.') + return email + def clean(self): cleaned_data = super().clean() password = cleaned_data.get('password') confirm_password = cleaned_data.get('confirm_password') + + # Password validation + if password: + if len(password) < 8: + raise ValidationError('Password must be at least 8 characters long.') + if not any(char.isdigit() for char in password): + raise ValidationError('Password must contain at least one number.') + if not any(char.isupper() for char in password): + raise ValidationError('Password must contain at least one uppercase letter.') # Check if passwords match - if password != confirm_password: - self.add_error('confirm_password', 'Passwords do not match') + if password and confirm_password and password != confirm_password: + raise ValidationError('Passwords do not match.') return cleaned_data - - def clean(self): - cleaned_data = super().clean() - password = cleaned_data.get('password') - confirm_password = cleaned_data.get('confirm_password') - - if password and confirm_password and password != confirm_password: - raise forms.ValidationError("Passwords do not match.") + def save(self, commit=True): + user = super().save(commit=False) + user.set_password(self.cleaned_data['password']) + if commit: + user.save() + return user diff --git a/sahara/main/templates/main/register.html b/sahara/main/templates/main/register.html index 8cbc3d9..007038f 100644 --- a/sahara/main/templates/main/register.html +++ b/sahara/main/templates/main/register.html @@ -1,133 +1,44 @@ + {% extends 'base.html' %} {% block content %} -
-

Register

-
- {% csrf_token %} - -
- - {{ form.first_name }} - {% if form.first_name.errors %} -
- {% for error in form.first_name.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.last_name }} - {% if form.last_name.errors %} -
- {% for error in form.last_name.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.email }} - {% if form.email.errors %} -
- {% for error in form.email.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.password }} - {% if form.password.errors %} -
- {% for error in form.password.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.confirm_password }} - {% if form.confirm_password.errors %} -
- {% for error in form.confirm_password.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.profile }} - {% if form.profile.errors %} -
- {% for error in form.profile.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.price }} - {% if form.price.errors %} -
- {% for error in form.price.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.bio }} - {% if form.bio.errors %} -
- {% for error in form.bio.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.service_offered }} - {% if form.service_offered.errors %} -
- {% for error in form.service_offered.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- - {{ form.login_profile }} - {% if form.login_profile.errors %} -
- {% for error in form.login_profile.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- - -
-

Already have an account? Login here.

+
+
+
+
+
Register
+
+ {% if messages %} + {% for message in messages %} +
+ {{ message }} +
+ {% endfor %} + {% endif %} + +
+ {% csrf_token %} + + {% for field in form %} +
+ + {{ field }} + {% if field.help_text %} + {{ field.help_text }} + {% endif %} + {% for error in field.errors %} +
+ {{ error }} +
+ {% endfor %} +
+ {% endfor %} + + +
+
+
+
+
-{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/sahara/main/views.py b/sahara/main/views.py index 8d054a9..8609f73 100644 --- a/sahara/main/views.py +++ b/sahara/main/views.py @@ -44,28 +44,28 @@ def home(request): def register(request): if request.user.is_authenticated: - messages.warning(request,"You are already logged in. Please Logout first to register ") + messages.warning(request, "You are already logged in.") return redirect('home') - if request.method == 'POST' and request.FILES: - form = UserRegistrationForm(request.POST,request.FILES, instance=request.user) - if form.is_valid(): - user = form.save(commit=False) - user.set_password(form.cleaned_data['password']) - user.save() + if request.method == 'POST': + form = UserRegistrationForm(request.POST, request.FILES) + if form.is_valid(): try: - user.send_verification_email(user) - messages.success(request, 'Registration successful! Please check your email to activate your account.') + user = form.save() + messages.success(request, 'Registration successful! You can now login.') + return redirect('login') except Exception as e: - logger.error(f"Error sending verification email: {e}") - messages.error(request, e) - - return redirect('login') + logger.error(f"Registration error: {str(e)}") + messages.error(request, 'An error occurred during registration. Please try again.') + else: + # Display form errors + for field, errors in form.errors.items(): + for error in errors: + messages.error(request, f"{field}: {error}") else: form = UserRegistrationForm() - messages.error(request, 'email already exists') - return render(request, 'main/register.html', {'form': form}) + return render(request, 'main/register.html', {'form': form}) def activate_account(request, uidb64, token): try: uid = urlsafe_base64_decode(uidb64).decode() diff --git a/sahara/static/profile-images/pic1_boIQ2tD.jpg b/sahara/static/profile-images/pic1_boIQ2tD.jpg new file mode 100644 index 0000000..bfbb2b8 Binary files /dev/null and b/sahara/static/profile-images/pic1_boIQ2tD.jpg differ diff --git a/sahara/templates/base.html b/sahara/templates/base.html index d4d4829..9fbc8a5 100644 --- a/sahara/templates/base.html +++ b/sahara/templates/base.html @@ -62,4 +62,5 @@ \ No newline at end of file + + \ No newline at end of file