Skip to content

Commit

Permalink
feature/aahq-41-admin-page-settings-for-usersuser-profiles-and-events
Browse files Browse the repository at this point in the history
  • Loading branch information
aahnik committed Jan 7, 2024
1 parent 48e08f9 commit 400e8fc
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/haps/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ class EventAdmin(admin.ModelAdmin):
@admin.register(EventRegistration)
class EventRegistrationAdmin(admin.ModelAdmin):
search_fields = ["event", "user"]
list_display = ["user", "event"]
list_display = ["event", "user_name", "user_whatsapp", "user", "user_profile_link"]
list_filter = ["event__name"]
11 changes: 11 additions & 0 deletions src/haps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from utils.images import upload_image_to
from utils.slugs import generate_unique_slug
from ckeditor_uploader.fields import RichTextUploadingField
from users.models import UserProfile
from django.utils.html import format_html

User = get_user_model()

Expand Down Expand Up @@ -43,5 +45,14 @@ class EventRegistration(models.Model):
datetime = models.DateTimeField(auto_now_add=True, null=True, blank=True)
# regno = models.CharField(unique=True)

def user_name(self):
return self.user.full_name()

def user_whatsapp(self):
return UserProfile.objects.get(user=self.user).whatsapp_number

def user_profile_link(self):
return self.user.profile_link()

def __str__(self) -> str:
return self.user.__str__() + "%" + self.event.__str__()
2 changes: 1 addition & 1 deletion src/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TempleWebUserAdmin(UserAdmin):
form = TempleWebAdminUserChangeForm
add_form = TempleWebUserCreationForm

list_display = ["full_name", "email", "date_joined", "is_staff"]
list_display = ["full_name", "email", "date_joined", "is_staff", "profile_link"]
list_filter = ["is_staff"]

fieldsets = [
Expand Down
7 changes: 6 additions & 1 deletion src/users/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from utils.images import upload_image_to

from django.utils.html import format_html
from .managers import TempleWebUserManager


Expand All @@ -20,6 +20,11 @@ def __str__(self):
def full_name(self):
return self.get_full_name()

def profile_link(self):
return format_html(
f'<a href="/users/profile/{self.email}" target="_blank">View User Profile</a>'
)


class UserProfile(models.Model):
user = models.OneToOneField(TempleWebUser, on_delete=models.CASCADE)
Expand Down
4 changes: 2 additions & 2 deletions src/users/templates/users/profile.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "commons.html" %}

<!-- title -->
{% block title %} Profile {% endblock title %}
{% block title %} {{user.full_name}} {% endblock title %}

<!-- content -->

Expand All @@ -14,7 +14,7 @@
<img class="w-24 h-24 mb-3 rounded-full shadow-lg" {% if user_profile.profile_pic %}
src="{{user_profile.profile_pic.url}}" {% else %} src="" {% endif %} alt="User Profile Image" />
<h5 class="mb-1 text-xl font-medium text-gray-900 dark:text-white">
{{user.first_name}} {{user.last_name}}
{{user.full_name}}
</h5>
<span class="text-sm text-gray-500 dark:text-gray-400">{{user.email}}</span>
<form enctype="multipart/form-data" method="post">
Expand Down
1 change: 1 addition & 0 deletions src/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
path("me/", views.me_view, name="me_view"),
path("register/", views.register, name="register"),
path("login/", views.login_view, name="login"),
path("profile/<str:email>", views.user_profile, name="profile"),
path("logout", views.logout_view, name="logout"),
]
23 changes: 22 additions & 1 deletion src/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from django.core.exceptions import ValidationError
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required
from utils.adirect import adirect

User = get_user_model()

Expand Down Expand Up @@ -53,7 +55,7 @@ def me_view(request: HttpRequest, edit: str = "view"):
form.save()
return redirect("users:me_view")
else:
user_profile, truth = UserProfile.objects.get_or_create(user=request.user)
# user_profile, truth = UserProfile.objects.get_or_create(user=request.user)
form = UserProfileForm(instance=user_profile)
return render(
request,
Expand Down Expand Up @@ -95,3 +97,22 @@ def register(request: HttpRequest):

def forgot_password(request: HttpRequest):
pass


@permission_required("is_superuser", login_url="/users/login")
def user_profile(request: HttpRequest, email: str):
user = User.objects.get(email=email)
user_profile, t = UserProfile.objects.get_or_create(user=user)

if request.method == "POST":
form = UserProfileForm(request.POST, request.FILES, instance=user_profile)
if form.is_valid():
form.save()
return redirect("users:profile", email=email)
else:
form = UserProfileForm(instance=user_profile)
return render(
request,
"users/profile.html",
{"form": form, "user": user, "user_profile": user_profile},
)

0 comments on commit 400e8fc

Please sign in to comment.