forms.py 4.9 KB

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