Skip to content

Commit

Permalink
Merge pull request #18 from Fer-Bar/feature/profile-picture-in-admin-…
Browse files Browse the repository at this point in the history
…site

Added profile pictures to admin site
  • Loading branch information
Fer-Bar authored May 23, 2024
2 parents 0c6a66f + c0f36f3 commit 1811b3c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
37 changes: 15 additions & 22 deletions people/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<img src="{obj.picture.url}" height=250>')
return mark_safe(
f'<img style="object-fit: cover;" src="{obj.picture_url}" height=250 width=100%>'
)

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",
),
]

Expand All @@ -62,7 +58,7 @@ def get_fieldsets(self, request, obj=None):
{
"fields": main_fields
+ [
("city",),
("first_name", "last_name", "gender", "city"),
("personal_email", "birthday"),
]
},
Expand All @@ -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",),
]
},
),
Expand All @@ -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")
9 changes: 9 additions & 0 deletions people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion pet/admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'<img style="object-fit: cover;" src="{obj.picture_url}" height=250 width=100%>'
)

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):
Expand All @@ -19,4 +45,5 @@ class BreedAdmin(admin.ModelAdmin):

class PetInline(admin.TabularInline):
model = Pet
fields = ("name", "breed", "gender")
extra = 0

0 comments on commit 1811b3c

Please sign in to comment.