views.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. from django.contrib import messages
  2. from django.core.urlresolvers import reverse
  3. from django.forms.formsets import formset_factory
  4. from django.views.decorators.http import require_POST, require_GET
  5. from jet.forms import AddBookmarkForm, RemoveBookmarkForm, ToggleApplicationPinForm, UpdateDashboardModulesForm, \
  6. AddUserDashboardModuleForm, UpdateDashboardModuleCollapseForm, RemoveDashboardModuleForm, ModelLookupForm
  7. from jet.models import Bookmark, UserDashboardModule
  8. from jet.utils import JsonResponse, get_app_list, SuccessMessageMixin
  9. from django.views.generic import UpdateView
  10. from django.utils.translation import ugettext_lazy as _
  11. class UpdateDashboardModuleView(SuccessMessageMixin, UpdateView):
  12. model = UserDashboardModule
  13. fields = ('title',)
  14. template_name = 'jet/dashboard/update_module.html'
  15. success_message = _('Widget was successfully updated')
  16. object = None
  17. module = None
  18. def get_success_url(self):
  19. if self.object.app_label:
  20. return reverse('admin:app_list', kwargs={'app_label': self.object.app_label})
  21. else:
  22. return reverse('admin:index')
  23. def get_module(self):
  24. object = self.object if getattr(self, 'object', None) is not None else self.get_object()
  25. return object.load_module()
  26. def get_settings_form_kwargs(self):
  27. kwargs = {
  28. 'initial': self.module.settings
  29. }
  30. if self.request.method in ('POST', 'PUT'):
  31. kwargs.update({
  32. 'data': self.request.POST,
  33. 'files': self.request.FILES,
  34. })
  35. return kwargs
  36. def get_settings_form(self):
  37. if self.module.settings_form:
  38. return self.module.settings_form(**self.get_settings_form_kwargs())
  39. def get_children_formset_kwargs(self):
  40. kwargs = {
  41. 'initial': self.module.children,
  42. 'prefix': 'children',
  43. }
  44. if self.request.method in ('POST', 'PUT'):
  45. kwargs.update({
  46. 'data': self.request.POST,
  47. 'files': self.request.FILES,
  48. })
  49. return kwargs
  50. def get_children_formset(self):
  51. if self.module.child_form:
  52. return formset_factory(self.module.child_form, can_delete=True, extra=1)(**self.get_children_formset_kwargs())
  53. def clean_children_data(self, children):
  54. children = list(filter(
  55. lambda item: isinstance(item, dict) and item and item.get('DELETE') is not True,
  56. children
  57. ))
  58. for item in children:
  59. item.pop('DELETE')
  60. return children
  61. def get_current_app(self):
  62. app_list = get_app_list({'request': self.request})
  63. for app in app_list:
  64. if app.get('app_label', app.get('name')) == self.object.app_label:
  65. return app
  66. def get_context_data(self, **kwargs):
  67. data = super(UpdateDashboardModuleView, self).get_context_data(**kwargs)
  68. data['title'] = _('Change')
  69. data['module'] = self.module
  70. data['settings_form'] = self.get_settings_form()
  71. data['children_formset'] = self.get_children_formset()
  72. data['child_name'] = self.module.child_name if self.module.child_name else _('Items')
  73. data['child_name_plural'] = self.module.child_name_plural if self.module.child_name_plural else _('Items')
  74. data['app'] = self.get_current_app()
  75. return data
  76. def dispatch(self, request, *args, **kwargs):
  77. self.object = self.get_object()
  78. self.module = self.get_module()(model=self.object)
  79. return super(UpdateDashboardModuleView, self).dispatch(request, *args, **kwargs)
  80. def post(self, request, *args, **kwargs):
  81. settings_form = self.get_settings_form()
  82. children_formset = self.get_children_formset()
  83. data = request.POST.copy()
  84. if settings_form:
  85. if settings_form.is_valid():
  86. settings = settings_form.cleaned_data
  87. data['settings'] = self.module.dump_settings(settings)
  88. else:
  89. return self.form_invalid(self.get_form(self.get_form_class()))
  90. if children_formset:
  91. if children_formset.is_valid():
  92. self.module.children = self.clean_children_data(children_formset.cleaned_data)
  93. data['children'] = self.module.dump_children()
  94. else:
  95. return self.form_invalid(self.get_form(self.get_form_class()))
  96. request.POST = data
  97. return super(UpdateDashboardModuleView, self).post(request, *args, **kwargs)
  98. def form_valid(self, form):
  99. if 'settings' in form.data:
  100. form.instance.settings = form.data['settings']
  101. if 'children' in form.data:
  102. form.instance.children = form.data['children']
  103. return super(UpdateDashboardModuleView, self).form_valid(form)
  104. @require_POST
  105. def add_bookmark_view(request):
  106. result = {'error': False}
  107. form = AddBookmarkForm(request, request.POST)
  108. if form.is_valid():
  109. bookmark = form.save()
  110. result['id'] = bookmark.pk
  111. else:
  112. result['error'] = True
  113. return JsonResponse(result)
  114. @require_POST
  115. def remove_bookmark_view(request):
  116. result = {'error': False}
  117. try:
  118. instance = Bookmark.objects.get(pk=request.POST.get('id'))
  119. form = RemoveBookmarkForm(request, request.POST, instance=instance)
  120. if form.is_valid():
  121. form.save()
  122. else:
  123. result['error'] = True
  124. except Bookmark.DoesNotExist:
  125. result['error'] = True
  126. return JsonResponse(result)
  127. @require_POST
  128. def toggle_application_pin_view(request):
  129. result = {'error': False}
  130. form = ToggleApplicationPinForm(request, request.POST)
  131. if form.is_valid():
  132. pinned = form.save()
  133. result['pinned'] = pinned
  134. else:
  135. result['error'] = True
  136. return JsonResponse(result)
  137. @require_POST
  138. def update_dashboard_modules_view(request):
  139. result = {'error': False}
  140. form = UpdateDashboardModulesForm(request, request.POST)
  141. if form.is_valid():
  142. form.save()
  143. else:
  144. result['error'] = True
  145. return JsonResponse(result)
  146. @require_POST
  147. def add_user_dashboard_module_view(request):
  148. result = {'error': False}
  149. form = AddUserDashboardModuleForm(request, request.POST)
  150. if form.is_valid():
  151. module = form.save()
  152. result['id'] = module.pk
  153. messages.success(request, _('Widget has been successfully added'))
  154. if module.app_label:
  155. result['success_url'] = reverse('admin:app_list', kwargs={'app_label': module.app_label})
  156. else:
  157. result['success_url'] = reverse('admin:index')
  158. else:
  159. result['error'] = True
  160. return JsonResponse(result)
  161. @require_POST
  162. def update_dashboard_module_collapse_view(request):
  163. result = {'error': False}
  164. try:
  165. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  166. form = UpdateDashboardModuleCollapseForm(request, request.POST, instance=instance)
  167. if form.is_valid():
  168. module = form.save()
  169. result['collapsed'] = module.collapsed
  170. else:
  171. result['error'] = True
  172. except UserDashboardModule.DoesNotExist:
  173. result['error'] = True
  174. return JsonResponse(result)
  175. @require_POST
  176. def remove_dashboard_module_view(request):
  177. result = {'error': False}
  178. try:
  179. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  180. form = RemoveDashboardModuleForm(request, request.POST, instance=instance)
  181. if form.is_valid():
  182. form.save()
  183. else:
  184. result['error'] = True
  185. except UserDashboardModule.DoesNotExist:
  186. result['error'] = True
  187. return JsonResponse(result)
  188. @require_GET
  189. def load_dashboard_module_view(request, pk):
  190. result = {'error': False}
  191. try:
  192. instance = UserDashboardModule.objects.get(pk=pk)
  193. module_cls = instance.load_module()
  194. module = module_cls(model=instance, context={'request': request})
  195. result['html'] = module.render()
  196. except UserDashboardModule.DoesNotExist:
  197. result['error'] = True
  198. return JsonResponse(result)
  199. @require_GET
  200. def model_lookup_view(request):
  201. result = {'error': False}
  202. form = ModelLookupForm(request.GET)
  203. if form.is_valid():
  204. items, total = form.lookup()
  205. result['items'] = items
  206. result['total'] = total
  207. else:
  208. result['error'] = True
  209. return JsonResponse(result)