views.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. from django.contrib import messages
  2. from django.contrib.messages.views import SuccessMessageMixin
  3. from django.core.urlresolvers import reverse
  4. from django.forms.formsets import formset_factory
  5. from django.views.decorators.http import require_POST, require_GET
  6. from jet.forms import AddBookmarkForm, RemoveBookmarkForm, ToggleApplicationPinForm, UpdateDashboardModulesForm, \
  7. AddUserDashboardModuleForm, UpdateDashboardModuleCollapseForm, RemoveDashboardModuleForm, ModelLookupForm
  8. from jet.models import Bookmark, UserDashboardModule
  9. from jet.utils import JsonResponse, get_app_list
  10. from django.views.generic import UpdateView
  11. from django.utils.translation import ugettext_lazy as _
  12. class UpdateDashboardModuleView(SuccessMessageMixin, UpdateView):
  13. model = UserDashboardModule
  14. fields = ('title',)
  15. template_name = 'jet/dashboard/update_module.html'
  16. success_message = _('Widget was successfully updated')
  17. object = None
  18. module = None
  19. def get_success_url(self):
  20. if self.object.app_label:
  21. return reverse('admin:app_list', kwargs={'app_label': self.object.app_label})
  22. else:
  23. return reverse('admin:index')
  24. def get_module(self):
  25. object = self.object if getattr(self, 'object', None) is not None else self.get_object()
  26. return object.load_module()
  27. def get_settings_form_kwargs(self):
  28. kwargs = {
  29. 'initial': self.module.settings
  30. }
  31. if self.request.method in ('POST', 'PUT'):
  32. kwargs.update({
  33. 'data': self.request.POST,
  34. 'files': self.request.FILES,
  35. })
  36. return kwargs
  37. def get_settings_form(self):
  38. if self.module.settings_form:
  39. return self.module.settings_form(**self.get_settings_form_kwargs())
  40. def get_children_formset_kwargs(self):
  41. kwargs = {
  42. 'initial': self.module.children,
  43. 'prefix': 'children',
  44. }
  45. if self.request.method in ('POST', 'PUT'):
  46. kwargs.update({
  47. 'data': self.request.POST,
  48. 'files': self.request.FILES,
  49. })
  50. return kwargs
  51. def get_children_formset(self):
  52. if self.module.child_form:
  53. return formset_factory(self.module.child_form, can_delete=True, extra=1)(**self.get_children_formset_kwargs())
  54. def clean_children_data(self, children):
  55. children = list(filter(
  56. lambda item: isinstance(item, dict) and item and item.get('DELETE') is not True,
  57. children
  58. ))
  59. for item in children:
  60. item.pop('DELETE')
  61. return children
  62. def get_current_app(self):
  63. app_list = get_app_list({'request': self.request})
  64. for app in app_list:
  65. if app['app_label'] == self.object.app_label:
  66. return app
  67. def get_context_data(self, **kwargs):
  68. data = super(UpdateDashboardModuleView, self).get_context_data(**kwargs)
  69. data['title'] = _('Change')
  70. data['module'] = self.module
  71. data['settings_form'] = self.get_settings_form()
  72. data['children_formset'] = self.get_children_formset()
  73. data['child_name'] = self.module.child_name if self.module.child_name else _('Items')
  74. data['child_name_plural'] = self.module.child_name_plural if self.module.child_name_plural else _('Items')
  75. data['app'] = self.get_current_app()
  76. return data
  77. def dispatch(self, request, *args, **kwargs):
  78. self.object = self.get_object()
  79. self.module = self.get_module()(model=self.object)
  80. return super(UpdateDashboardModuleView, self).dispatch(request, *args, **kwargs)
  81. def post(self, request, *args, **kwargs):
  82. settings_form = self.get_settings_form()
  83. children_formset = self.get_children_formset()
  84. data = request.POST.copy()
  85. if settings_form:
  86. if settings_form.is_valid():
  87. settings = settings_form.cleaned_data
  88. data['settings'] = self.module.dump_settings(settings)
  89. else:
  90. return self.form_invalid(self.get_form())
  91. if children_formset:
  92. if children_formset.is_valid():
  93. self.module.children = self.clean_children_data(children_formset.cleaned_data)
  94. data['children'] = self.module.dump_children()
  95. else:
  96. return self.form_invalid(self.get_form())
  97. request.POST = data
  98. return super(UpdateDashboardModuleView, self).post(request, *args, **kwargs)
  99. def form_valid(self, form):
  100. if 'settings' in form.data:
  101. form.instance.settings = form.data['settings']
  102. if 'children' in form.data:
  103. form.instance.children = form.data['children']
  104. return super(UpdateDashboardModuleView, self).form_valid(form)
  105. @require_POST
  106. def add_bookmark_view(request):
  107. result = {'error': False}
  108. form = AddBookmarkForm(request, request.POST)
  109. if form.is_valid():
  110. bookmark = form.save()
  111. result['id'] = bookmark.pk
  112. else:
  113. result['error'] = True
  114. return JsonResponse(result)
  115. @require_POST
  116. def remove_bookmark_view(request):
  117. result = {'error': False}
  118. try:
  119. instance = Bookmark.objects.get(pk=request.POST.get('id'))
  120. form = RemoveBookmarkForm(request, request.POST, instance=instance)
  121. if form.is_valid():
  122. form.save()
  123. else:
  124. result['error'] = True
  125. except Bookmark.DoesNotExist:
  126. result['error'] = True
  127. return JsonResponse(result)
  128. @require_POST
  129. def toggle_application_pin_view(request):
  130. result = {'error': False}
  131. form = ToggleApplicationPinForm(request, request.POST)
  132. if form.is_valid():
  133. pinned = form.save()
  134. result['pinned'] = pinned
  135. else:
  136. result['error'] = True
  137. return JsonResponse(result)
  138. @require_POST
  139. def update_dashboard_modules_view(request):
  140. result = {'error': False}
  141. form = UpdateDashboardModulesForm(request, request.POST)
  142. if form.is_valid():
  143. form.save()
  144. else:
  145. result['error'] = True
  146. return JsonResponse(result)
  147. @require_POST
  148. def add_user_dashboard_module_view(request):
  149. result = {'error': False}
  150. form = AddUserDashboardModuleForm(request, request.POST)
  151. if form.is_valid():
  152. module = form.save()
  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)