1
0

decorators.py 3.4 KB

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