refactor(core): Move allowed fields logic to own function

ref: #250 #96 #93 #95 #90 #257
This commit is contained in:
2024-09-02 12:33:54 +09:30
parent 057a39091d
commit d7c3e051de
3 changed files with 84 additions and 52 deletions

View File

@ -21,7 +21,7 @@ from the root of the project to start a test server use:
``` bash
# activate python venv
/tmp/centurion_erp/bin/activate
source /tmp/centurion_erp/bin/activate
# enter app dir
cd app

View File

@ -52,71 +52,46 @@ class CommentForm(
self.fields['parent'].widget = self.fields['parent'].hidden_widget()
self.fields['comment_type'].widget = self.fields['comment_type'].hidden_widget()
self._ticket_type = kwargs['initial']['type_ticket']
if 'qs_comment_type' in kwargs['initial']:
comment_type = kwargs['initial']['qs_comment_type']
self._comment_type = kwargs['initial']['qs_comment_type']
else:
comment_type = str(self.instance.get_comment_type_display()).lower()
self._comment_type = str(self.instance.get_comment_type_display()).lower()
if self._comment_type == 'task':
self.fields['comment_type'].initial = self.Meta.model.CommentType.TASK
elif self._comment_type == 'comment':
self.fields['comment_type'].initial = self.Meta.model.CommentType.COMMENT
elif self._comment_type == 'solution':
self.fields['comment_type'].initial = self.Meta.model.CommentType.SOLUTION
elif self._comment_type == 'notification':
self.fields['comment_type'].initial = self.Meta.model.CommentType.NOTIFICATION
allowed_fields = self.fields_allowed
original_fields = self.fields.copy()
comment_fields = []
if (
kwargs['initial']['type_ticket'] == 'request'
or
kwargs['initial']['type_ticket'] == 'incident'
or
kwargs['initial']['type_ticket'] == 'problem'
or
kwargs['initial']['type_ticket'] == 'change'
or
kwargs['initial']['type_ticket'] == 'project_task'
):
if comment_type == 'task':
comment_fields = self.Meta.model.fields_itsm_task
self.fields['comment_type'].initial = self.Meta.model.CommentType.TASK
elif comment_type == 'comment':
comment_fields = self.Meta.model.common_itsm_fields
self.fields['comment_type'].initial = self.Meta.model.CommentType.COMMENT
elif comment_type == 'solution':
comment_fields = self.Meta.model.common_itsm_fields
self.fields['comment_type'].initial = self.Meta.model.CommentType.SOLUTION
elif comment_type == 'notification':
comment_fields = self.Meta.model.fields_itsm_notification
self.fields['comment_type'].initial = self.Meta.model.CommentType.NOTIFICATION
elif kwargs['initial']['type_ticket'] == 'issue':
comment_fields = self.Meta.model.fields_git_issue
elif kwargs['initial']['type_ticket'] == 'merge':
comment_fields = self.Meta.model.fields_git_merge
for field in original_fields:
if field not in comment_fields and not self.fields[field].widget.is_hidden:
if field not in allowed_fields and not self.fields[field].widget.is_hidden:
del self.fields[field]
def clean(self):
cleaned_data = super().clean()

View File

@ -13,10 +13,67 @@ class TicketCommentValidation(
original_object = None
_comment_type:str = None
"""Human readable comment type. i.e. `request` in lowercase"""
_ticket_type: str = None
"""Human readable type of ticket. i.e. `request` in lowercase"""
@property
def fields_allowed(self):
pass
comment_fields = []
if (
self._ticket_type == 'request'
or
self._ticket_type == 'incident'
or
self._ticket_type == 'problem'
or
self._ticket_type == 'change'
or
self._ticket_type == 'project_task'
):
if self._comment_type == 'task':
comment_fields = self.Meta.model.fields_itsm_task
self.fields['comment_type'].initial = self.Meta.model.CommentType.TASK
elif self._comment_type == 'comment':
comment_fields = self.Meta.model.common_itsm_fields
self.fields['comment_type'].initial = self.Meta.model.CommentType.COMMENT
elif self._comment_type == 'solution':
comment_fields = self.Meta.model.common_itsm_fields
self.fields['comment_type'].initial = self.Meta.model.CommentType.SOLUTION
elif self._comment_type == 'notification':
comment_fields = self.Meta.model.fields_itsm_notification
self.fields['comment_type'].initial = self.Meta.model.CommentType.NOTIFICATION
elif self._ticket_type == 'issue':
comment_fields = self.Meta.model.fields_git_issue
elif self._ticket_type == 'merge':
comment_fields = self.Meta.model.fields_git_merge
return comment_fields
def validate_field_permission(self):