request, extra_context=extra_context, ) def get_image(self, obj): thumbnailer = get_thumbnailer(obj.image) thumbnail_options = {'size': (100, 100)} return format_html('<img src="/media/{0}" />'.format( thumbnailer.get_thumbnail(thumbnail_options))) get_image.short_description = 'Image' def has_add_permission(self, request): return False def has_delete_permission(self, request, obj=None): return False def get_queryset(self, request): gallery_id = request.GET.get('gallery') qs = super().get_queryset(request) return qs.filter(gallery_id=gallery_id) # hide link from admin home page def has_module_permission(self, request): return False phx_admin.register(Gallery, GalleryAdmin) phx_admin.register(Image, ImageOrderAdmin)
list_display = [ 'current_image', 'caption', 'categories', 'created_date', 'author' ] list_display_links = ['current_image', 'caption'] readonly_fields = ['current_image'] exclude = ['author'] def categories(self, obj): return ', '.join([cat.category for cat in obj.image_categories.all()]) def current_image(self, obj): thumbnailer = get_thumbnailer(obj.image) thumbnail_options = {'size': (200, 200)} return format_html('<img src="/media/{0}" />'.format( thumbnailer.get_thumbnail(thumbnail_options))) def save_model(self, request, obj, form, change): if getattr(obj, 'author', None) is None: obj.author = request.user obj.save() class HeroImageCategoryAdmin(admin.ModelAdmin): list_display = ['category', 'count'] phx_admin.register(Content, ContentAdmin) phx_admin.register(Announcement, AnnouncementAdmin) phx_admin.register(Hero, HeroAdmin) phx_admin.register(HeroImageCategory, HeroImageCategoryAdmin)
return ['fixture', 'author'] if obj else ['author'] # if adding, hide readonly fixture detail field def get_readonly_fields(self, request, obj): return [] if obj is None else ['selected_fixture'] # update dropdown to contain date alongside name # fields = ['results_url', 'summary', 'results', 'get_'] # only list fixtures that don't already have results associated # past fixtures from (or created in) the last month only def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == "fixture": one_month_ago = timezone.now() - timezone.timedelta(days=30) kwargs["queryset"] = Fixture.objects.filter( Q(fixture__isnull=True) & ((Q(event_date__lte=timezone.now()) & Q(event_date__gte=one_month_ago)) | (Q(event_date__lte=timezone.now()) & Q(modified_date__gte=one_month_ago)))) return super().formfield_for_foreignkey(db_field, request, **kwargs) def save_model(self, request, obj, form, change): if getattr(obj, 'author', None) is None: obj.author = request.user obj.save() phx_admin.register(Result, ResultAdmin)
from phx.admin import phx_admin from phx.helpers.file import file_size_string from .models import File class FileAdmin(admin.ModelAdmin): list_display = [ 'title', 'url', 'size', 'uploaded_by', 'created_date', ] exclude = ['uploaded_by'] def url(self, obj): return '{0}{1}'.format(settings.SITE_URL, obj.file.url) def size(self, obj): return file_size_string(obj.file.size) def save_model(self, request, obj, form, change): if getattr(obj, 'uploaded_by', None) is None: obj.uploaded_by = request.user obj.save() phx_admin.register(File, FileAdmin)
'title', 'event_date', 'age_groups', 'location', 'has_results', 'author', ] list_select_related = ['fixture'] ordering = ['-event_date'] exclude = ['author'] def has_results(self, obj): return True if obj.fixture else False # return obj.results has_results.boolean = True def save_model(self, request, obj, form, change): if getattr(obj, 'author', None) is None: obj.author = request.user obj.save() class CategoryAdmin(admin.ModelAdmin): list_display = ['title', 'abbreviation'] pass phx_admin.register(Category, CategoryAdmin) phx_admin.register(Fixture, FixtureAdmin)
title = '↣ {0}'.format(obj.title) else: title = obj.title return title get_title.short_description = 'title' # if adding, hide readonly fixture detail field def get_readonly_fields(self, request, obj): return [] if obj is None else ['slug'] # if adding, hide slug select field def get_exclude(self, request, obj): return ['author'] if obj is None else ['author', 'slug'] def save_model(self, request, obj, form, change): if getattr(obj, 'author', None) is None: obj.author = request.user obj.save() # base PageAdmin form doesn't contain files, # so doesn't register as requring multipart support (`has_file_field`) # this fix ensures `enctype="multipart/form-data"` is added to HTML form def get_form(self, request, obj=None, **kwargs): form = super().get_form(request, obj, **kwargs) form.is_multipart = lambda n: True return form phx_admin.register(Page, PageAdmin)
from .models import Contact, Message, Topic class ContactAdmin(admin.ModelAdmin): list_display = ['name', 'email', 'created_date'] class TopicAdmin(admin.ModelAdmin): list_display = ['topic', 'contacts', 'created_date'] def contacts(self, obj): return ', '.join([contact.name for contact in obj.contact.all()]) contacts.short_description = 'Contacts' class MessageAdmin(admin.ModelAdmin): list_display = ['name', 'email', 'topic', 'message_received'] readonly_fields = ['name', 'email', 'topic', 'message', 'created_date'] def message_received(self, obj): return obj.created_date def has_add_permission(self, request): return False phx_admin.register(Contact, ContactAdmin) phx_admin.register(Topic, TopicAdmin) phx_admin.register(Message, MessageAdmin)
from django.contrib import admin from phx.admin import phx_admin from .models import Social class SocialAdmin(admin.ModelAdmin): list_display = ['model', 'title', 'url', 'created_date', 'posted'] readonly_fields = ['model', 'title', 'url', 'posted', 'reposted'] def has_add_permission(self, request): return False phx_admin.register(Social, SocialAdmin)
model = Thumbnail readonly_fields = ['current_image'] def current_image(self, obj): thumbnailer = get_thumbnailer(obj.image) thumbnail_options = {'size': (200, 200)} return format_html('<img src="/media/{0}" />'.format( thumbnailer.get_thumbnail(thumbnail_options))) class NewsAdmin(nested_admin.NestedModelAdmin): list_display = ['current_image', 'title', 'created_date', 'author'] list_display_links = ['current_image', 'title'] list_select_related = ['author', 'thumbnail'] exclude = ['author'] inlines = [ThumbnailAdmin, ComponentAdmin] def current_image(self, obj): thumbnailer = get_thumbnailer(obj.thumbnail.image) thumbnail_options = {'size': (100, 100)} return format_html('<img src="/media/{0}" />'.format( thumbnailer.get_thumbnail(thumbnail_options))) def save_model(self, request, obj, form, change): if getattr(obj, 'author', None) is None: obj.author = request.user obj.save() phx_admin.register(News, NewsAdmin)