views.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.dashboard.forms import UpdateDashboardModulesForm, AddUserDashboardModuleForm, \
  6. UpdateDashboardModuleCollapseForm, RemoveDashboardModuleForm, ResetDashboardForm
  7. from jet.dashboard.models import 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 update_dashboard_modules_view(request):
  109. result = {'error': False}
  110. form = UpdateDashboardModulesForm(request, request.POST)
  111. if form.is_valid():
  112. form.save()
  113. else:
  114. result['error'] = True
  115. return JsonResponse(result)
  116. @require_POST
  117. def add_user_dashboard_module_view(request):
  118. result = {'error': False}
  119. form = AddUserDashboardModuleForm(request, request.POST)
  120. if form.is_valid():
  121. module = form.save()
  122. result['id'] = module.pk
  123. messages.success(request, _('Widget has been successfully added'))
  124. if module.app_label:
  125. result['success_url'] = reverse('admin:app_list', kwargs={'app_label': module.app_label})
  126. else:
  127. result['success_url'] = reverse('admin:index')
  128. else:
  129. result['error'] = True
  130. return JsonResponse(result)
  131. @require_POST
  132. def update_dashboard_module_collapse_view(request):
  133. result = {'error': False}
  134. try:
  135. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  136. form = UpdateDashboardModuleCollapseForm(request, request.POST, instance=instance)
  137. if form.is_valid():
  138. module = form.save()
  139. result['collapsed'] = module.collapsed
  140. else:
  141. result['error'] = True
  142. except UserDashboardModule.DoesNotExist:
  143. result['error'] = True
  144. return JsonResponse(result)
  145. @require_POST
  146. def remove_dashboard_module_view(request):
  147. result = {'error': False}
  148. try:
  149. instance = UserDashboardModule.objects.get(pk=request.POST.get('id'))
  150. form = RemoveDashboardModuleForm(request, request.POST, instance=instance)
  151. if form.is_valid():
  152. form.save()
  153. else:
  154. result['error'] = True
  155. except UserDashboardModule.DoesNotExist:
  156. result['error'] = True
  157. return JsonResponse(result)
  158. @require_GET
  159. def load_dashboard_module_view(request, pk):
  160. result = {'error': False}
  161. try:
  162. instance = UserDashboardModule.objects.get(pk=pk)
  163. module_cls = instance.load_module()
  164. module = module_cls(model=instance, context={'request': request})
  165. result['html'] = module.render()
  166. except UserDashboardModule.DoesNotExist:
  167. result['error'] = True
  168. return JsonResponse(result)
  169. @require_POST
  170. def reset_dashboard_view(request):
  171. result = {'error': False}
  172. form = ResetDashboardForm(request, request.POST)
  173. if form.is_valid():
  174. form.save()
  175. else:
  176. result['error'] = True
  177. return JsonResponse(result)