dashboard_custom_module.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. =======================
  2. Custom Dashboard Module
  3. =======================
  4. In order create your own dashboard module you need to follow these steps:
  5. * Inherit :ref:`Dashboard Module <Dashboard Module>`
  6. * Create module template
  7. * *(optional) Add module views*
  8. Also you can always see build-in modules as examples in ``jet/dashboard/modules.py`` file and
  9. ``jet/dashboard/dashboard_modules/`` directory on the repository.
  10. Inherit Dashboard Module
  11. ------------------------
  12. * Create dashboard modules file ``dashboard_modules.py`` (or any other you prefer) inside your Django application
  13. * Create dashboard module class inherited from base :ref:`dashboard module <Dashboard Module>` class and add it to
  14. ``dashboard_modules.py`` file. You can see list of all available module attributes :ref:`here <Dashboard Module>`.
  15. ``init_with_context`` method allows you to load data and initialize module's state. You can store data in
  16. module's fields as this instance will be passed to template.
  17. Example of ``dashboard_modules.py``:
  18. .. code-block:: python
  19. from jet.dashboard.modules import DashboardModule
  20. from contact.models import Ticket
  21. class RecentTickets(DashboardModule):
  22. title = 'Recent tickets'
  23. title_url = Ticket.get_admin_changelist_url()
  24. template = 'contact/dashboard_modules/recent_tickets.html'
  25. limit = 10
  26. def init_with_context(self, context):
  27. self.children = Ticket.objects.order_by('-date_add')[:self.limit]
  28. * Optionally you can add customizable module settings and content which will be seen in administration interface.
  29. For customizable settings ``settings_form`` should be set, also ``settings_dict`` and ``load_settings`` methods
  30. should be implemented. For customizable content items ``child_form``, ``child_name`` and ``child_name_plural``
  31. should be set, also ``store_children`` should return ``True``. You can validate loaded from database children
  32. in ``__init__`` method.
  33. .. image:: _static/dashboard_module_settings.png
  34. :width: 100%
  35. Example of ``LinkList`` dashboard module which has custom settings and editable list of links:
  36. .. code-block:: python
  37. class LinkList(DashboardModule):
  38. title = 'Links'
  39. template = 'jet.dashboard/modules/link_list.html'
  40. layout = 'stacked'
  41. children = []
  42. settings_form = LinkListSettingsForm
  43. child_form = LinkListItemForm
  44. child_name = 'Link'
  45. child_name_plural = 'Links'
  46. def __init__(self, title=None, children=list(), **kwargs):
  47. children = list(map(self.parse_link, children))
  48. kwargs.update({'children': children})
  49. super(LinkList, self).__init__(title, **kwargs)
  50. def settings_dict(self):
  51. return {
  52. 'layout': self.layout
  53. }
  54. def load_settings(self, settings):
  55. self.layout = settings.get('layout', self.layout)
  56. def store_children(self):
  57. return True
  58. def parse_link(self, link):
  59. if isinstance(link, (tuple, list)):
  60. link_dict = {'title': link[0], 'url': link[1]}
  61. if len(link) >= 3:
  62. link_dict['external'] = link[2]
  63. return link_dict
  64. elif isinstance(link, (dict,)):
  65. return link
  66. class LinkListSettingsForm(forms.Form):
  67. layout = forms.ChoiceField(label='Layout', choices=(('stacked', 'Stacked'), ('inline', 'Inline')))
  68. class LinkListItemForm(forms.Form):
  69. url = forms.CharField(label='URL')
  70. title = forms.CharField(label='Title')
  71. external = forms.BooleanField(label='External link', required=False)
  72. Create Module Template
  73. ----------------------
  74. Create template at path specified in module class. Module instance is passed to template as ``module`` variable
  75. so you can get data directly from it.
  76. .. code-block:: html
  77. {% load humanize %}
  78. <ul>
  79. {% for ticket in module.children %}
  80. <li>
  81. <span class="float-right">
  82. <span class="dim">
  83. {{ ticket.date_add|naturalday }} <span class="icon-clock tooltip" title="{{ ticket.date_add }}"></span>
  84. </span>
  85. </span>
  86. {% if ticket.forwarded %}
  87. <span class="icon-tick" style="color: #8ecb8e;"></span>
  88. {% else %}
  89. <span class="icon-cross" style="color: #dba4a4;"></span>
  90. {% endif %}
  91. <a href="{{ ticket.get_admin_url }}">{{ ticket.name }}</a>
  92. </li>
  93. {% empty %}
  94. <li>
  95. Nothing to show
  96. </li>
  97. {% endfor %}
  98. </ul>
  99. Add Module Views (Optional)
  100. ---------------------------
  101. If your dashboard module needs to have own views you can register them the following way and store for example
  102. in ``dashboard_modules_views.py`` file inside your application:
  103. .. code-block:: python
  104. from django.conf.urls import url
  105. from django.contrib import messages
  106. from django.shortcuts import redirect
  107. from jet.dashboard import dashboard
  108. from core.utils.utils import DatabaseManager
  109. def update_database(request):
  110. database_manager = DatabaseManager()
  111. database_manager.update_database()
  112. messages.success(request, 'Database was successfully updated')
  113. return redirect(request.META.get('HTTP_REFERER'))
  114. # This method registers view's url
  115. dashboard.urls.register_urls([
  116. url(
  117. r'^update_database/',
  118. update_database,
  119. name='update-database'
  120. ),
  121. ])
  122. You should import this file before dashboard urls have been imported in you main ``urls.py`` file.
  123. .. code-block:: python
  124. from django.conf import settings
  125. from django.conf.urls import include, url
  126. from django.contrib import admin
  127. # Import dashboard module views
  128. from core import dashboard_modules_views
  129. urlpatterns = [
  130. url(r'^admin/', include(admin.site.urls)),
  131. url(r'^jet/', include('jet.urls', 'jet')),
  132. url(r'^jet/dashboard/', include('jet.dashboard.urls', 'jet-dashboard')),
  133. ...
  134. ]
  135. After that you can reverse url to module's view this way:
  136. .. code-block:: html
  137. {% url "jet-dashboard:update-database" %}