From f3cc2d16796b95575c6972250c171a501eb9fa05 Mon Sep 17 00:00:00 2001 From: Fer-Bar Date: Thu, 23 May 2024 19:40:10 -0400 Subject: [PATCH 1/3] Added picture_url property to return safe url strings --- people/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/people/models.py b/people/models.py index 4534dfe..886eca9 100644 --- a/people/models.py +++ b/people/models.py @@ -145,6 +145,15 @@ class Person(models.Model): help_text=_("Max size allowed: 20Mbs"), ) + @property + def picture_url(self): + """ + Return self.picture.url if self.picture is not None, + 'url' exist and has a value, else, return None. + """ + if self.picture and hasattr(self.picture, "url"): + return self.picture.url + def create_user(self): if self.user_id is not None: return From 6ea8408f26940927a45b37f061c1970a46ce1939 Mon Sep 17 00:00:00 2001 From: Fer-Bar Date: Thu, 23 May 2024 19:42:39 -0400 Subject: [PATCH 2/3] Deleted user object references, refactor the orden fields & added picture_url property --- people/admin.py | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/people/admin.py b/people/admin.py index f63ed2f..8c4d687 100644 --- a/people/admin.py +++ b/people/admin.py @@ -25,34 +25,30 @@ class BasePersonAdmin(ModelAdmin): "user__email", ] autocomplete_fields = [ - "user", "occupation", "city", "nationality", ] def picture_display(self, obj=None): - return mark_safe(f'') + return mark_safe( + f'' + ) - picture_display.short_description = _("See picture") + picture_display.short_description = _("Profile Picture") - def get_readonly_fields(self, request, obj=None): - return ["picture_display",] + readonly_fields = ("picture_display",) def get_fieldsets(self, request, obj=None): if obj is None or not obj.picture: main_fields = [ ("picture",), - ("first_name", "last_name", "gender"), ] else: main_fields = [ ( "picture_display", "picture", - "first_name", - "last_name", - "gender", ), ] @@ -62,7 +58,7 @@ def get_fieldsets(self, request, obj=None): { "fields": main_fields + [ - ("city",), + ("first_name", "last_name", "gender", "city"), ("personal_email", "birthday"), ] }, @@ -74,10 +70,10 @@ def get_fieldsets(self, request, obj=None): ("document_type", "document_id"), ("marital_status",), ("phone_number", "secondary_phone_number"), - ("nationality", ), - ("address", ), - ("neighborhood", ), - ("occupation", ), + ("nationality",), + ("address",), + ("neighborhood",), + ("occupation",), ] }, ), @@ -86,20 +82,17 @@ def get_fieldsets(self, request, obj=None): @admin.register(Person) class PersonAdmin(BasePersonAdmin): - - def get_fieldsets(self, request, obj=None): - fieldsets = super().get_fieldsets(request, obj=obj) - return fieldsets + list_display = ["first_name", "last_name", "list_pets",] + inlines = [ + PetInline, + ] def has_delete_permission( self, request, obj=None ): # This allows to delete only if you're a superuser return request.user.is_superuser and request.user.is_active - inlines = [PetInline,] - list_display = ["first_name", "last_name", "list_pets"] - def list_pets(self, obj): return ", ".join([str(pet) for pet in obj.pets.all()]) - list_pets.short_description = _("Pets") + list_pets.short_description = _("My Pets") From c0f36f307e724a9a7a0e3b5b3e495cc058c3c020 Mon Sep 17 00:00:00 2001 From: Fer-Bar Date: Thu, 23 May 2024 19:54:53 -0400 Subject: [PATCH 3/3] Added picture profile in admin section, declaring the field as a readonly --- pet/admin.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pet/admin.py b/pet/admin.py index 0f87689..7e9b6d9 100644 --- a/pet/admin.py +++ b/pet/admin.py @@ -1,4 +1,6 @@ from django.contrib import admin +from django.utils.safestring import mark_safe +from django.utils.translation import gettext_lazy as _ from pet.filters import HasOwnerFilter from pet.models import Breed, Pet @@ -8,9 +10,33 @@ class PetAdmin(admin.ModelAdmin): search_fields = ["name", "owner"] list_display = ("name", "breed", "gender", "age", "owner", "has_owner", "is_neutered") - readonly_fields = ("age", ) + readonly_fields = ("age", "picture_display",) list_filter = (HasOwnerFilter,) + def picture_display(self, obj=None): + return mark_safe( + f'' + ) + + picture_display.short_description = _("Profile Picture") + + def get_fieldsets(self, request, obj=None): + if obj is None or not obj.picture_url: + picture_fields = [ + ("picture",), + ] + else: + picture_fields = [ + ("picture_display", "picture"), + ] + return ( + ( + None, { + "fields": picture_fields + [("name", "breed", "gender", "age"), ] + } + ), + ) + @admin.register(Breed) class BreedAdmin(admin.ModelAdmin): @@ -19,4 +45,5 @@ class BreedAdmin(admin.ModelAdmin): class PetInline(admin.TabularInline): model = Pet + fields = ("name", "breed", "gender") extra = 0