views.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. form = self.module.settings_form(**self.get_settings_form_kwargs())
  39. if hasattr(form, 'set_module'):
  40. form.set_module(self.module)
  41. return form
  42. def get_children_formset_kwargs(self):
  43. kwargs = {
  44. 'initial': self.module.children,
  45. 'prefix': 'children',
  46. }
  47. if self.request.method in ('POST', 'PUT'):
  48. kwargs.update({
  49. 'data': self.request.POST,
  50. 'files': self.request.FILES,
  51. })
  52. return kwargs
  53. def get_children_formset(self):
  54. if self.module.child_form:
  55. return formset_factory(self.module.child_form, can_delete=True, extra=1)(**self.get_children_formset_kwargs())
  56. def clean_children_data(self, children):
  57. children = list(filter(
  58. lambda item: isinstance(item, dict) and item and item.get('DELETE') is not True,
  59. children
  60. ))
  61. for item in children:
  62. item.pop('DELETE')
  63. return children
  64. def get_current_app(self):
  65. app_list = get_app_list({'request': self.request})
  66. for app in app_list:
  67. if app.get('app_label', app.get('name')) == self.object.app_label:
  68. return app
  69. def get_context_data(self, **kwargs):
  70. data = super(UpdateDashboardModuleView, self).get_context_data(**kwargs)
  71. data['title'] = _('Change')
  72. data['module'] = self.module
  73. data['settings_form'] = self.get_settings_form()
  74. data['children_formset'] = self.get_children_formset()
  75. data['child_name'] = self.module.child_name if self.module.child_name else _('Items')
  76. data['child_name_plural'] = self.module.child_name_plural if self.module.child_name_plural else _('Items')
  77. data['app'] = self.get_current_app()
  78. return data
  79. def dispatch(self, request, *args, **kwargs):
  80. self.object = self.get_object()
  81. self.module = self.get_module()(model=self.object)
  82. return super(UpdateDashboardModuleView, self).dispatch(request, *args, **kwargs)
  83. def post(self, request, *args, **kwargs):
  84. settings_form = self.get_settings_form()
  85. children_formset = self.get_children_formset()
  86. data = request.POST.copy()
  87. if settings_form:
  88. if settings_form.is_valid():
  89. settings = settings_form.cleaned_data
  90. data['settings'] = self.module.dump_settings(settings)
  91. else:
  92. return self.form_invalid(self.get_form(self.get_form_class()))
  93. if children_formset:
  94. if children_formset.is_valid():
  95. self.module.children = self.clean_children_data(children_formset.cleaned_data)
  96. data['children'] = self.module.dump_children()
  97. else:
  98. return self.form_invalid(self.get_form(self.get_form_class()))
  99. request.POST = data
  100. return super(UpdateDashboardModuleView, self).post(request, *args, **kwargs)
  101. def form_valid(self, form):
  102. if 'settings' in form.data:
  103. form.instance.settings = form.data['settings']
  104. if 'children' in form.data:
  105. form.instance.children = form.data['children']
  106. return super(UpdateDashboardModuleView, self).form_valid(form)
  107. @require_POST
  108. def add_bookmark_view(request):
  109. result = {'error': False}
  110. form = AddBookmarkForm(request, request.POST)
  111. if form.is_valid():
  112. bookmark = form.save()
  113. result['id'] = bookmark.pk
  114. else:
  115. result['error'] = True
  116. return JsonResponse(result)
  117. @require_POST
  118. def remove_bookmark_view(request):
  119. result = {'error': False}
  120. try:
  121. instance = Bookmark.objects.get(pk=request.POST.get('id'))
  122. form = RemoveBookmarkForm(request, request.POST, instance=instance)
  123. if form.is_valid():
  124. form.save()
  125. else:
  126. result['error'] = True
  127. except Bookmark.DoesNotExist:
  128. result['error'] = True
  129. return JsonResponse(result)
  130. @require_POST
  131. def toggle_application_pin_view(request):
  132. result = {'error': False}
  133. form = ToggleApplicationPinForm(request, request.POST)
  134. if form.is_valid():
  135. pinned = form.save()
  136. result['pinned'] = pinned
  137. else:
  138. result['error'] = True
  139. return JsonResponse(result)
  140. @require_POST
  141. def update_dashboard_modules_view(request):
  142. result = {'error': False}
  143. form = UpdateDashboardModulesForm(request, request.POST)
  144. if form.is_valid():
  145. form.save()
  146. else:
  147. result['error'] = True
  148. return JsonResponse(result)
  149. @require_POST
  150. def add_user_dashboard_module_view(request):
  151. result = {'error': False}
  152. form = AddUserDashboardModuleForm(request, request.POST)
  153. if form.is_valid():
  154. module = form.save()
  155. result['id'] = module.pk
  156. messages.success(request, _('Widget has been successfully added'))
  157. if module.app_label:
  158. result['success_url'] = reverse('admin:app_list', kwargs={'app_label': module.app_label})
  159. else:
  160. result['success_url'] = reverse('admin:index')
  161. else:
  162. result['error'] = True
  163. return JsonResponse(result)
  164. @require_POST
  165. def update_dashboard_module_collapse_view(request):
  166. result = {'error': False}
  167. try:
  168. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  169. form = UpdateDashboardModuleCollapseForm(request, request.POST, instance=instance)
  170. if form.is_valid():
  171. module = form.save()
  172. result['collapsed'] = module.collapsed
  173. else:
  174. result['error'] = True
  175. except UserDashboardModule.DoesNotExist:
  176. result['error'] = True
  177. return JsonResponse(result)
  178. @require_POST
  179. def remove_dashboard_module_view(request):
  180. result = {'error': False}
  181. try:
  182. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  183. form = RemoveDashboardModuleForm(request, request.POST, instance=instance)
  184. if form.is_valid():
  185. form.save()
  186. else:
  187. result['error'] = True
  188. except UserDashboardModule.DoesNotExist:
  189. result['error'] = True
  190. return JsonResponse(result)
  191. @require_GET
  192. def load_dashboard_module_view(request, pk):
  193. result = {'error': False}
  194. try:
  195. instance = UserDashboardModule.objects.get(pk=pk)
  196. module_cls = instance.load_module()
  197. module = module_cls(model=instance, context={'request': request})
  198. result['html'] = module.render()
  199. except UserDashboardModule.DoesNotExist:
  200. result['error'] = True
  201. return JsonResponse(result)
  202. @require_GET
  203. def model_lookup_view(request):
  204. result = {'error': False}
  205. form = ModelLookupForm(request.GET)
  206. if form.is_valid():
  207. items, total = form.lookup()
  208. result['items'] = items
  209. result['total'] = total
  210. else:
  211. result['error'] = True
  212. return JsonResponse(result)