dashboard.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. from importlib import import_module
  2. from django.core.urlresolvers import reverse
  3. from django.template.loader import render_to_string
  4. from jet.dashboard import modules
  5. from jet.dashboard.models import UserDashboardModule
  6. from django.core.context_processors import csrf
  7. from django.utils.translation import ugettext_lazy as _
  8. from jet.ordered_set import OrderedSet
  9. from jet.utils import get_admin_site_name
  10. class Dashboard(object):
  11. columns = 2
  12. children = None
  13. available_children = None
  14. app_label = None
  15. context = None
  16. modules = None
  17. class Media:
  18. css = ()
  19. js = ()
  20. def __init__(self, context, **kwargs):
  21. for key in kwargs:
  22. if hasattr(self.__class__, key):
  23. setattr(self, key, kwargs[key])
  24. self.children = self.children or []
  25. self.available_children = self.available_children or []
  26. self.set_context(context)
  27. def set_context(self, context):
  28. self.context = context
  29. self.init_with_context(context)
  30. self.load_modules()
  31. def init_with_context(self, context):
  32. pass
  33. def load_module(self, module_fullname):
  34. package, module_name = module_fullname.rsplit('.', 1)
  35. package = import_module(package)
  36. module = getattr(package, module_name)
  37. return module
  38. def create_initial_module_models(self, user):
  39. module_models = []
  40. i = 0
  41. for module in self.children:
  42. column = module.column if module.column else i % self.columns
  43. order = module.order if module.order else int(i / self.columns)
  44. module_models.append(UserDashboardModule.objects.create(
  45. title=module.title,
  46. app_label=self.app_label,
  47. user=user.pk,
  48. module=module.fullname(),
  49. column=column,
  50. order=order,
  51. settings=module.dump_settings(),
  52. children=module.dump_children()
  53. ))
  54. i += 1
  55. return module_models
  56. def load_modules(self):
  57. module_models = UserDashboardModule.objects.filter(
  58. app_label=self.app_label,
  59. user=self.context['request'].user.pk
  60. ).all()
  61. if len(module_models) == 0:
  62. module_models = self.create_initial_module_models(self.context['request'].user)
  63. loaded_modules = []
  64. for module_model in module_models:
  65. module_cls = module_model.load_module()
  66. if module_cls is not None:
  67. module = module_cls(model=module_model, context=self.context)
  68. loaded_modules.append(module)
  69. self.modules = loaded_modules
  70. def render(self):
  71. context = self.context
  72. context.update({
  73. 'columns': range(self.columns),
  74. 'modules': self.modules,
  75. 'app_label': self.app_label,
  76. })
  77. context.update(csrf(context['request']))
  78. return render_to_string('jet.dashboard/dashboard.html', context)
  79. def render_tools(self):
  80. context = self.context
  81. context.update({
  82. 'children': self.children,
  83. 'app_label': self.app_label,
  84. 'available_children': self.available_children
  85. })
  86. context.update(csrf(context['request']))
  87. return render_to_string('jet.dashboard/dashboard_tools.html', context)
  88. def media(self):
  89. unique_css = OrderedSet()
  90. unique_js = OrderedSet()
  91. for js in getattr(self.Media, 'js', ()):
  92. unique_js.add(js)
  93. for css in getattr(self.Media, 'css', ()):
  94. unique_css.add(css)
  95. for module in self.modules:
  96. for js in getattr(module.Media, 'js', ()):
  97. unique_js.add(js)
  98. for css in getattr(module.Media, 'css', ()):
  99. unique_css.add(css)
  100. class Media:
  101. css = list(unique_css)
  102. js = list(unique_js)
  103. return Media
  104. class AppIndexDashboard(Dashboard):
  105. def get_app_content_types(self):
  106. return self.app_label + '.*',
  107. def models(self):
  108. return self.app_label + '.*',
  109. class DefaultIndexDashboard(Dashboard):
  110. columns = 3
  111. def init_with_context(self, context):
  112. self.available_children.append(modules.LinkList)
  113. self.available_children.append(modules.Feed)
  114. site_name = get_admin_site_name(context)
  115. # append a link list module for "quick links"
  116. self.children.append(modules.LinkList(
  117. _('Quick links'),
  118. layout='inline',
  119. draggable=False,
  120. deletable=False,
  121. collapsible=False,
  122. children=[
  123. [_('Return to site'), '/'],
  124. [_('Change password'),
  125. reverse('%s:password_change' % site_name)],
  126. [_('Log out'), reverse('%s:logout' % site_name)],
  127. ],
  128. column=0,
  129. order=0
  130. ))
  131. # append an app list module for "Applications"
  132. self.children.append(modules.AppList(
  133. _('Applications'),
  134. exclude=('auth.*',),
  135. column=1,
  136. order=0
  137. ))
  138. # append an app list module for "Administration"
  139. self.children.append(modules.AppList(
  140. _('Administration'),
  141. models=('auth.*',),
  142. column=2,
  143. order=0
  144. ))
  145. # append a recent actions module
  146. self.children.append(modules.RecentActions(
  147. _('Recent Actions'),
  148. 10,
  149. column=0,
  150. order=1
  151. ))
  152. # append a feed module
  153. self.children.append(modules.Feed(
  154. _('Latest Django News'),
  155. feed_url='http://www.djangoproject.com/rss/weblog/',
  156. limit=5,
  157. column=1,
  158. order=1
  159. ))
  160. # append another link list module for "support".
  161. self.children.append(modules.LinkList(
  162. _('Support'),
  163. children=[
  164. {
  165. 'title': _('Django documentation'),
  166. 'url': 'http://docs.djangoproject.com/',
  167. 'external': True,
  168. },
  169. {
  170. 'title': _('Django "django-users" mailing list'),
  171. 'url': 'http://groups.google.com/group/django-users',
  172. 'external': True,
  173. },
  174. {
  175. 'title': _('Django irc channel'),
  176. 'url': 'irc://irc.freenode.net/django',
  177. 'external': True,
  178. },
  179. ],
  180. column=2,
  181. order=1
  182. ))
  183. class DefaultAppIndexDashboard(AppIndexDashboard):
  184. def init_with_context(self, context):
  185. self.available_children.append(modules.LinkList)
  186. self.children.append(modules.ModelList(
  187. title=_('Application models'),
  188. models=self.models(),
  189. column=0,
  190. order=0
  191. ))
  192. self.children.append(modules.RecentActions(
  193. include_list=self.get_app_content_types(),
  194. column=1,
  195. order=0
  196. ))
  197. class DashboardUrls(object):
  198. _urls = []
  199. def get_urls(self):
  200. return self._urls
  201. def register_url(self, url):
  202. self._urls.append(url)
  203. def register_urls(self, urls):
  204. self._urls.extend(urls)
  205. urls = DashboardUrls()