From 3db03e8e6e4b70f82f1c046fdeeb4c94f70b61ea Mon Sep 17 00:00:00 2001 From: Shubham Garg Date: Wed, 4 Dec 2024 01:26:19 -0500 Subject: [PATCH] (#238) Profile picture (#237) * profile picture * requirements fix * adding navbar image * edits * edit * edit * gitignore * fix --- .gitignore | 2 +- src/accounts/admin.py | 1 + src/accounts/forms.py | 46 +++++++- .../0009_customuser_profile_image_url.py | 23 ++++ src/accounts/models.py | 7 ++ src/accounts/templates/profile_base.html | 95 ++++++++++------ src/accounts/templates/register.html | 9 +- src/accounts/views.py | 105 ++++++++++++++++-- src/public_service_finder/settings.py | 16 ++- src/public_service_finder/templates/base.html | 3 - .../templates/partials/_navbar.html | 11 +- src/requirements.txt | 25 ++--- 12 files changed, 274 insertions(+), 69 deletions(-) create mode 100644 src/accounts/migrations/0009_customuser_profile_image_url.py diff --git a/.gitignore b/.gitignore index 100fe18..d4f5e41 100644 --- a/.gitignore +++ b/.gitignore @@ -144,4 +144,4 @@ GitHub.sublime-settings .env ps_env .aider* -src/staticfiles \ No newline at end of file +src/staticfiles diff --git a/src/accounts/admin.py b/src/accounts/admin.py index b742668..4da6aab 100644 --- a/src/accounts/admin.py +++ b/src/accounts/admin.py @@ -17,6 +17,7 @@ class CustomUserAdmin(UserAdmin): "last_name", "user_type", "is_staff", + "profile_image_url", # Display profile image URL ) # Fields to filter by in the list view diff --git a/src/accounts/forms.py b/src/accounts/forms.py index b500b30..ab115ac 100644 --- a/src/accounts/forms.py +++ b/src/accounts/forms.py @@ -80,6 +80,15 @@ class UserRegisterForm(UserCreationForm): } ), ) + profile_image = forms.ImageField( + required=False, + widget=forms.ClearableFileInput( + attrs={ + "class": "w-full p-2 rounded bg-gray-700 text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500 h-12 border border-gray-600", + } + ), + help_text="Optional. Upload a profile image.", + ) class Meta: model = CustomUser @@ -91,6 +100,7 @@ class Meta: "password1", "password2", "user_type", + "profile_image", # Add this field ] @@ -199,9 +209,25 @@ def get_invalid_login_error(self): class ServiceSeekerForm(forms.ModelForm): + profile_image = forms.ImageField( + required=False, + widget=forms.ClearableFileInput( + attrs={ + "class": "w-full p-2 rounded bg-gray-700 text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500 h-12 border border-gray-600", + } + ), + help_text="Upload a new profile image or leave blank to keep the current one.", + ) + remove_profile_image = forms.BooleanField( + required=False, + initial=False, + label="Remove profile image", + help_text="Check this box to remove your current profile image.", + ) + class Meta: model = CustomUser - fields = ["username", "email", "first_name", "last_name"] + fields = ["username", "email", "first_name", "last_name", "profile_image"] widgets = { "username": forms.TextInput( attrs={ @@ -235,9 +261,25 @@ class Meta: # Note: We're maintaining two different forms (even though they're the same) for seekers and providers to future-proof our code. # We *might* add additional fields for service providers soon. class ServiceProviderForm(forms.ModelForm): + profile_image = forms.ImageField( + required=False, + widget=forms.ClearableFileInput( + attrs={ + "class": "w-full p-2 rounded bg-gray-700 text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500 h-12 border border-gray-600", + } + ), + help_text="Upload a new profile image or leave blank to keep the current one.", + ) + remove_profile_image = forms.BooleanField( + required=False, + initial=False, + label="Remove profile image", + help_text="Check this box to remove your current profile image.", + ) + class Meta: model = CustomUser - fields = ["username", "email", "first_name", "last_name"] + fields = ["username", "email", "first_name", "last_name", "profile_image"] widgets = { "username": forms.TextInput( attrs={ diff --git a/src/accounts/migrations/0009_customuser_profile_image_url.py b/src/accounts/migrations/0009_customuser_profile_image_url.py new file mode 100644 index 0000000..d13127c --- /dev/null +++ b/src/accounts/migrations/0009_customuser_profile_image_url.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.1 on 2024-12-03 23:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("accounts", "0008_alter_customuser_first_name_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="customuser", + name="profile_image_url", + field=models.URLField( + blank=True, + help_text="URL to the user's profile image stored in S3.", + max_length=500, + null=True, + ), + ), + ] diff --git a/src/accounts/models.py b/src/accounts/models.py index 9a24ebb..6483e34 100644 --- a/src/accounts/models.py +++ b/src/accounts/models.py @@ -16,5 +16,12 @@ class CustomUser(AbstractUser): max_length=20, choices=USER_TYPE_CHOICES, default="user" ) + profile_image_url = models.URLField( + max_length=500, + blank=True, + null=True, + help_text="URL to the user's profile image stored in S3.", + ) + def __str__(self): return f"{self.first_name} {self.last_name} ({self.username})" diff --git a/src/accounts/templates/profile_base.html b/src/accounts/templates/profile_base.html index 3184643..1eca2a0 100644 --- a/src/accounts/templates/profile_base.html +++ b/src/accounts/templates/profile_base.html @@ -21,9 +21,14 @@

Profile

-
- {{ profile.username|first }} -
+ {% if profile.profile_image_url %} + Profile Image + {% else %} +
+ {{ profile.username|first }} +
+ {% endif %}

Profile Information

Username: {{ profile.username }}

@@ -47,7 +52,7 @@

Profile Information

border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700"> Forum Posts - + {{ user_posts|length }} @@ -58,7 +63,7 @@

Profile Information

Bookmarks {{ bookmarks|length }} - +
@@ -162,7 +167,7 @@

My Reviews

My Forum Posts

{{ user_posts|length }} posts - +
{% if user_posts %}
@@ -175,7 +180,8 @@

My Forum Posts

{{ post.title }}

- {{ post.content|truncatewords:30 }}

+ {{ post.content|truncatewords:30 }} +

{{ post.category.name }} {{ post.created_at|date:"M d, Y" }} @@ -199,41 +205,48 @@

My Forum Posts

-
- +