forms.py 5.4 KB

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