forms.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import json
  2. from django import forms
  3. from django.core.exceptions import ValidationError
  4. from jet.dashboard.models import UserDashboardModule
  5. from jet.dashboard.utils import get_current_dashboard
  6. class UpdateDashboardModulesForm(forms.Form):
  7. app_label = forms.CharField(required=False)
  8. modules = forms.CharField()
  9. modules_objects = []
  10. def __init__(self, request, *args, **kwargs):
  11. self.request = request
  12. super(UpdateDashboardModulesForm, self).__init__(*args, **kwargs)
  13. def clean(self):
  14. data = super(UpdateDashboardModulesForm, self).clean()
  15. if not self.request.user.is_authenticated() or not self.request.user.is_staff:
  16. raise ValidationError('error')
  17. try:
  18. modules = json.loads(data['modules'])
  19. for module in modules:
  20. db_module = UserDashboardModule.objects.get(
  21. user=self.request.user.pk,
  22. app_label=data['app_label'] if data['app_label'] else None,
  23. pk=module['id']
  24. )
  25. column = module['column']
  26. order = module['order']
  27. if db_module.column != column or db_module.order != order:
  28. db_module.column = column
  29. db_module.order = order
  30. self.modules_objects.append(db_module)
  31. except Exception:
  32. raise ValidationError('error')
  33. return data
  34. def save(self):
  35. for module in self.modules_objects:
  36. module.save()
  37. class AddUserDashboardModuleForm(forms.ModelForm):
  38. type = forms.CharField()
  39. module = forms.IntegerField()
  40. module_cls = None
  41. def __init__(self, request, *args, **kwargs):
  42. self.request = request
  43. super(AddUserDashboardModuleForm, self).__init__(*args, **kwargs)
  44. class Meta:
  45. model = UserDashboardModule
  46. fields = ['app_label']
  47. def clean_app_label(self):
  48. data = self.cleaned_data['app_label']
  49. return data if data != '' else None
  50. def clean(self):
  51. data = super(AddUserDashboardModuleForm, self).clean()
  52. if not self.request.user.is_authenticated() or not self.request.user.is_staff:
  53. raise ValidationError('error')
  54. if 'app_label' in data:
  55. index_dashboard_cls = get_current_dashboard('app_index' if data['app_label'] else 'index')
  56. index_dashboard = index_dashboard_cls({'request': self.request}, app_label=data['app_label'])
  57. if 'type' in data:
  58. if data['type'] == 'children':
  59. module = index_dashboard.children[data['module']]
  60. elif data['type'] == 'available_children':
  61. module = index_dashboard.available_children[data['module']]()
  62. else:
  63. raise ValidationError('error')
  64. self.module_cls = module
  65. return data
  66. def save(self, commit=True):
  67. self.instance.title = self.module_cls.title
  68. self.instance.module = self.module_cls.fullname()
  69. self.instance.user = self.request.user.pk
  70. self.instance.column = 0
  71. self.instance.order = -1
  72. self.instance.settings = self.module_cls.dump_settings()
  73. self.instance.children = self.module_cls.dump_children()
  74. return super(AddUserDashboardModuleForm, self).save(commit)
  75. class UpdateDashboardModuleCollapseForm(forms.ModelForm):
  76. def __init__(self, request, *args, **kwargs):
  77. self.request = request
  78. super(UpdateDashboardModuleCollapseForm, self).__init__(*args, **kwargs)
  79. class Meta:
  80. model = UserDashboardModule
  81. fields = ['collapsed']
  82. def clean(self):
  83. data = super(UpdateDashboardModuleCollapseForm, self).clean()
  84. if not self.request.user.is_authenticated() or not self.request.user.is_staff:
  85. raise ValidationError('error')
  86. if self.instance.user != self.request.user.pk:
  87. raise ValidationError('error')
  88. return data
  89. class RemoveDashboardModuleForm(forms.ModelForm):
  90. def __init__(self, request, *args, **kwargs):
  91. self.request = request
  92. super(RemoveDashboardModuleForm, self).__init__(*args, **kwargs)
  93. class Meta:
  94. model = UserDashboardModule
  95. fields = []
  96. def clean(self):
  97. cleaned_data = super(RemoveDashboardModuleForm, self).clean()
  98. if not self.request.user.is_authenticated() or self.instance.user != self.request.user.pk:
  99. raise ValidationError('error')
  100. return cleaned_data
  101. def save(self, commit=True):
  102. if commit:
  103. self.instance.delete()
  104. class ResetDashboardForm(forms.Form):
  105. app_label = forms.CharField(required=False)
  106. def __init__(self, request, *args, **kwargs):
  107. self.request = request
  108. super(ResetDashboardForm, self).__init__(*args, **kwargs)
  109. class Meta:
  110. model = UserDashboardModule
  111. fields = []
  112. def clean(self):
  113. data = super(ResetDashboardForm, self).clean()
  114. data['app_label'] = data['app_label'] if data['app_label'] else None
  115. if not self.request.user.is_authenticated() or not self.request.user.is_staff:
  116. raise ValidationError('error')
  117. return data
  118. def save(self, commit=True):
  119. if commit:
  120. UserDashboardModule.objects.filter(
  121. user=self.request.user.pk,
  122. app_label=self.cleaned_data['app_label']
  123. ).delete()