| 1 |
def action(function=None, *, permissions=None, description=None): |
| 2 |
""" |
| 3 |
Conveniently add attributes to an action function:: |
| 4 |
|
| 5 |
@admin.action( |
| 6 |
permissions=['publish'], |
| 7 |
description='Mark selected stories as published', |
| 8 |
) |
| 9 |
def make_published(self, request, queryset): |
| 10 |
queryset.update(status='p') |
| 11 |
|
| 12 |
This is equivalent to setting some attributes (with the original, longer |
| 13 |
names) on the function directly:: |
| 14 |
|
| 15 |
def make_published(self, request, queryset): |
| 16 |
queryset.update(status='p') |
| 17 |
make_published.allowed_permissions = ['publish'] |
| 18 |
make_published.short_description = 'Mark selected stories as published' |
| 19 |
""" |
| 20 |
|
| 21 |
def decorator(func): |
| 22 |
if permissions is not None: |
| 23 |
func.allowed_permissions = permissions |
| 24 |
if description is not None: |
| 25 |
func.short_description = description |
| 26 |
return func |
| 27 |
|
| 28 |
if function is None: |
| 29 |
return decorator |
| 30 |
else: |
| 31 |
return decorator(function) |
| 32 |
|
| 33 |
|
| 34 |
def display( |
| 35 |
function=None, *, boolean=None, ordering=None, description=None, empty_value=None |
| 36 |
): |
| 37 |
""" |
| 38 |
Conveniently add attributes to a display function:: |
| 39 |
|
| 40 |
@admin.display( |
| 41 |
boolean=True, |
| 42 |
ordering='-publish_date', |
| 43 |
description='Is Published?', |
| 44 |
) |
| 45 |
def is_published(self, obj): |
| 46 |
return obj.publish_date is not None |
| 47 |
|
| 48 |
This is equivalent to setting some attributes (with the original, longer |
| 49 |
names) on the function directly:: |
| 50 |
|
| 51 |
def is_published(self, obj): |
| 52 |
return obj.publish_date is not None |
| 53 |
is_published.boolean = True |
| 54 |
is_published.admin_order_field = '-publish_date' |
| 55 |
is_published.short_description = 'Is Published?' |
| 56 |
""" |
| 57 |
|
| 58 |
def decorator(func): |
| 59 |
if boolean is not None and empty_value is not None: |
| 60 |
raise ValueError( |
| 61 |
"The boolean and empty_value arguments to the @display " |
| 62 |
"decorator are mutually exclusive." |
| 63 |
) |
| 64 |
if boolean is not None: |
| 65 |
func.boolean = boolean |
| 66 |
if ordering is not None: |
| 67 |
func.admin_order_field = ordering |
| 68 |
if description is not None: |
| 69 |
func.short_description = description |
| 70 |
if empty_value is not None: |
| 71 |
func.empty_value_display = empty_value |
| 72 |
return func |
| 73 |
|
| 74 |
if function is None: |
| 75 |
return decorator |
| 76 |
else: |
| 77 |
return decorator(function) |
| 78 |
|
| 79 |
|
| 80 |
def register(*models, site=None): |
| 81 |
""" |
| 82 |
Register the given model(s) classes and wrapped ModelAdmin class with |
| 83 |
admin site: |
| 84 |
|
| 85 |
@register(Author) |
| 86 |
class AuthorAdmin(admin.ModelAdmin): |
| 87 |
pass |
| 88 |
|
| 89 |
The `site` kwarg is an admin site to use instead of the default admin site. |
| 90 |
""" |
| 91 |
from django.contrib.admin import ModelAdmin |
| 92 |
from django.contrib.admin.sites import AdminSite |
| 93 |
from django.contrib.admin.sites import site as default_site |
| 94 |
|
| 95 |
def _model_admin_wrapper(admin_class): |
| 96 |
if not models: |
| 97 |
raise ValueError("At least one model must be passed to register.") |
| 98 |
|
| 99 |
admin_site = site or default_site |
| 100 |
|
| 101 |
if not isinstance(admin_site, AdminSite): |
| 102 |
raise ValueError("site must subclass AdminSite") |
| 103 |
|
| 104 |
if not issubclass(admin_class, ModelAdmin): |
| 105 |
raise ValueError("Wrapped class must subclass ModelAdmin.") |
| 106 |
|
| 107 |
admin_site.register(models, admin_class=admin_class) |
| 108 |
|
| 109 |
return admin_class |
| 110 |
|
| 111 |
return _model_admin_wrapper |