views.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. from django.contrib import messages
  2. from django.core.urlresolvers import reverse
  3. from django.forms.formsets import formset_factory
  4. from django.shortcuts import redirect
  5. from django.views.decorators.http import require_POST, require_GET
  6. from jet.dashboard.forms import UpdateDashboardModulesForm, AddUserDashboardModuleForm, \
  7. UpdateDashboardModuleCollapseForm, RemoveDashboardModuleForm
  8. from jet.dashboard.models import UserDashboardModule
  9. from jet.utils import JsonResponse, get_app_list, SuccessMessageMixin
  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. form = self.module.settings_form(**self.get_settings_form_kwargs())
  40. if hasattr(form, 'set_module'):
  41. form.set_module(self.module)
  42. return form
  43. def get_children_formset_kwargs(self):
  44. kwargs = {
  45. 'initial': self.module.children,
  46. 'prefix': 'children',
  47. }
  48. if self.request.method in ('POST', 'PUT'):
  49. kwargs.update({
  50. 'data': self.request.POST,
  51. 'files': self.request.FILES,
  52. })
  53. return kwargs
  54. def get_children_formset(self):
  55. if self.module.child_form:
  56. return formset_factory(self.module.child_form, can_delete=True, extra=1)(**self.get_children_formset_kwargs())
  57. def clean_children_data(self, children):
  58. children = list(filter(
  59. lambda item: isinstance(item, dict) and item and item.get('DELETE') is not True,
  60. children
  61. ))
  62. for item in children:
  63. item.pop('DELETE')
  64. return children
  65. def get_current_app(self):
  66. app_list = get_app_list({'request': self.request})
  67. for app in app_list:
  68. if app.get('app_label', app.get('name')) == self.object.app_label:
  69. return app
  70. def get_context_data(self, **kwargs):
  71. data = super(UpdateDashboardModuleView, self).get_context_data(**kwargs)
  72. data['title'] = _('Change')
  73. data['module'] = self.module
  74. data['settings_form'] = self.get_settings_form()
  75. data['children_formset'] = self.get_children_formset()
  76. data['child_name'] = self.module.child_name if self.module.child_name else _('Items')
  77. data['child_name_plural'] = self.module.child_name_plural if self.module.child_name_plural else _('Items')
  78. data['app'] = self.get_current_app()
  79. return data
  80. def dispatch(self, request, *args, **kwargs):
  81. self.object = self.get_object()
  82. self.module = self.get_module()(model=self.object)
  83. return super(UpdateDashboardModuleView, self).dispatch(request, *args, **kwargs)
  84. def post(self, request, *args, **kwargs):
  85. settings_form = self.get_settings_form()
  86. children_formset = self.get_children_formset()
  87. data = request.POST.copy()
  88. if settings_form:
  89. if settings_form.is_valid():
  90. settings = settings_form.cleaned_data
  91. data['settings'] = self.module.dump_settings(settings)
  92. else:
  93. return self.form_invalid(self.get_form(self.get_form_class()))
  94. if children_formset:
  95. if children_formset.is_valid():
  96. self.module.children = self.clean_children_data(children_formset.cleaned_data)
  97. data['children'] = self.module.dump_children()
  98. else:
  99. return self.form_invalid(self.get_form(self.get_form_class()))
  100. request.POST = data
  101. return super(UpdateDashboardModuleView, self).post(request, *args, **kwargs)
  102. def form_valid(self, form):
  103. if 'settings' in form.data:
  104. form.instance.settings = form.data['settings']
  105. if 'children' in form.data:
  106. form.instance.children = form.data['children']
  107. return super(UpdateDashboardModuleView, self).form_valid(form)
  108. @require_POST
  109. def update_dashboard_modules_view(request):
  110. result = {'error': False}
  111. form = UpdateDashboardModulesForm(request, request.POST)
  112. if form.is_valid():
  113. form.save()
  114. else:
  115. result['error'] = True
  116. return JsonResponse(result)
  117. @require_POST
  118. def add_user_dashboard_module_view(request):
  119. result = {'error': False}
  120. form = AddUserDashboardModuleForm(request, request.POST)
  121. if form.is_valid():
  122. module = form.save()
  123. result['id'] = module.pk
  124. messages.success(request, _('Widget has been successfully added'))
  125. if module.app_label:
  126. result['success_url'] = reverse('admin:app_list', kwargs={'app_label': module.app_label})
  127. else:
  128. result['success_url'] = reverse('admin:index')
  129. else:
  130. result['error'] = True
  131. return JsonResponse(result)
  132. @require_POST
  133. def update_dashboard_module_collapse_view(request):
  134. result = {'error': False}
  135. try:
  136. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  137. form = UpdateDashboardModuleCollapseForm(request, request.POST, instance=instance)
  138. if form.is_valid():
  139. module = form.save()
  140. result['collapsed'] = module.collapsed
  141. else:
  142. result['error'] = True
  143. except UserDashboardModule.DoesNotExist:
  144. result['error'] = True
  145. return JsonResponse(result)
  146. @require_POST
  147. def remove_dashboard_module_view(request):
  148. result = {'error': False}
  149. try:
  150. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  151. form = RemoveDashboardModuleForm(request, request.POST, instance=instance)
  152. if form.is_valid():
  153. form.save()
  154. else:
  155. result['error'] = True
  156. except UserDashboardModule.DoesNotExist:
  157. result['error'] = True
  158. return JsonResponse(result)
  159. @require_GET
  160. def load_dashboard_module_view(request, pk):
  161. result = {'error': False}
  162. try:
  163. instance = UserDashboardModule.objects.get(pk=pk)
  164. module_cls = instance.load_module()
  165. module = module_cls(model=instance, context={'request': request})
  166. result['html'] = module.render()
  167. except UserDashboardModule.DoesNotExist:
  168. result['error'] = True
  169. return JsonResponse(result)
  170. def reset_dashboard_view(request, app_label=None):
  171. UserDashboardModule.objects.filter(user=request.user.pk, app_label=app_label).delete()
  172. if app_label:
  173. url = reverse('admin:app_list', kwargs={'app_label': app_label})
  174. else:
  175. url = reverse('admin:index')
  176. return redirect(url)