-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
admin.py
36 lines (28 loc) · 1.12 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.contrib import admin
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
from avatar.models import Avatar
from avatar.signals import avatar_updated
from avatar.utils import get_user_model
class AvatarAdmin(admin.ModelAdmin):
list_display = ("get_avatar", "user", "primary", "date_uploaded")
list_filter = ("primary",)
autocomplete_fields = ("user",)
search_fields = (
"user__%s" % getattr(get_user_model(), "USERNAME_FIELD", "username"),
)
list_per_page = 50
def get_avatar(self, avatar_in):
context = {
"user": avatar_in.user,
"url": avatar_in.avatar.url,
"alt": str(avatar_in.user),
"size": 80,
}
return render_to_string("avatar/avatar_tag.html", context)
get_avatar.short_description = _("Avatar")
get_avatar.allow_tags = True
def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
avatar_updated.send(sender=Avatar, user=request.user, avatar=obj)
admin.site.register(Avatar, AvatarAdmin)