diff --git a/apps/ticket/views.py b/apps/ticket/views.py index 57a0034f..a1054fa9 100644 --- a/apps/ticket/views.py +++ b/apps/ticket/views.py @@ -55,7 +55,12 @@ def post(self, request, *args, **kwargs): json_str = request.body.decode('utf-8') if not json_str: return api_response(-1, 'post参数为空', {}) + request_data_dict = json.loads(json_str) + if not(isinstance(request_data_dict.get('workflow_id', None), int) and isinstance(request_data_dict.get('transition_id', None), int)): + # 临时先这么判断,后续针对所有view统一使用更优雅的方式来处理 + return api_response(-1, 'workflow_id或transition_id类型不合法', {}) + app_name = request.META.get('HTTP_APPNAME') request_data_dict.update(dict(username=request.META.get('HTTP_USERNAME'))) diff --git a/service/ticket/ticket_base_service.py b/service/ticket/ticket_base_service.py index c35081cc..ece3a8b4 100644 --- a/service/ticket/ticket_base_service.py +++ b/service/ticket/ticket_base_service.py @@ -480,65 +480,75 @@ def update_ticket_custom_field(cls, ticket_id, update_dict): # 判断是否存在,如果存在则更新,如果不存在则新增 ticket_custom_field_queryset = TicketCustomField.objects.filter(ticket_id=ticket_id, field_key=key) field_type_id = format_custom_field_dict[key]['field_type_id'] - if ticket_custom_field_queryset: - if field_type_id == CONSTANT_SERVICE.FIELD_TYPE_STR: - ticket_custom_field_queryset.update(char_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_INT: - ticket_custom_field_queryset.update(int_value=int(update_dict.get(key))) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_FLOAT: - ticket_custom_field_queryset.update(float_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_BOOL: - ticket_custom_field_queryset.update(bool_value=int(update_dict.get(key))) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATE: - ticket_custom_field_queryset.update(date_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATETIME: - ticket_custom_field_queryset.update(datetime_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_RADIO: - ticket_custom_field_queryset.update(radio_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_CHECKBOX: - ticket_custom_field_queryset.update(checkbox_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_SELECT: - ticket_custom_field_queryset.update(select_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_SELECT: - ticket_custom_field_queryset.update(multi_select_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_TEXT: - ticket_custom_field_queryset.update(text_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_USERNAME: - ticket_custom_field_queryset.update(username_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_USERNAME: - ticket_custom_field_queryset.update(multi_username_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_ATTACHMENT: - ticket_custom_field_queryset.update(char_value=update_dict.get(key)) + + if update_dict.get(key) is None: + # 值为None。说明此字段为可选,且用户未填写或者清空了该字段,需要清空字段 + if ticket_custom_field_queryset: + # 已经存在,需要删除 + ticket_custom_field_queryset.update(is_deleted=1) + else: + # 不存在的,直接忽略 + pass else: - if field_type_id == CONSTANT_SERVICE.FIELD_TYPE_STR: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'], ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, char_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_INT: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, int_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_FLOAT: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, float_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_BOOL: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, bool_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATE: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, date_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATETIME: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, datetime_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_RADIO: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, radio_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_CHECKBOX: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, checkbox_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_SELECT: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, select_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_SELECT: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, multi_select_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_TEXT: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, text_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_USERNAME: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, username_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_USERNAME: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, multi_username_value=update_dict.get(key)) - elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_ATTACHMENT: - new_ticket_custom_field_record = TicketCustomField(name= format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, char_value=update_dict.get(key)) - new_ticket_custom_field_record.save() + if ticket_custom_field_queryset: + if field_type_id == CONSTANT_SERVICE.FIELD_TYPE_STR: + ticket_custom_field_queryset.update(char_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_INT: + ticket_custom_field_queryset.update(int_value=int(update_dict.get(key))) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_FLOAT: + ticket_custom_field_queryset.update(float_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_BOOL: + ticket_custom_field_queryset.update(bool_value=int(update_dict.get(key))) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATE: + ticket_custom_field_queryset.update(date_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATETIME: + ticket_custom_field_queryset.update(datetime_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_RADIO: + ticket_custom_field_queryset.update(radio_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_CHECKBOX: + ticket_custom_field_queryset.update(checkbox_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_SELECT: + ticket_custom_field_queryset.update(select_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_SELECT: + ticket_custom_field_queryset.update(multi_select_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_TEXT: + ticket_custom_field_queryset.update(text_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_USERNAME: + ticket_custom_field_queryset.update(username_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_USERNAME: + ticket_custom_field_queryset.update(multi_username_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_ATTACHMENT: + ticket_custom_field_queryset.update(char_value=update_dict.get(key)) + elif not ticket_custom_field_queryset: + if field_type_id == CONSTANT_SERVICE.FIELD_TYPE_STR: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'], ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, char_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_INT: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, int_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_FLOAT: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, float_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_BOOL: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, bool_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATE: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, date_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_DATETIME: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, datetime_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_RADIO: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, radio_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_CHECKBOX: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, checkbox_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_SELECT: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, select_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_SELECT: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, multi_select_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_TEXT: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, text_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_USERNAME: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, username_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_MULTI_USERNAME: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, multi_username_value=update_dict.get(key)) + elif field_type_id == CONSTANT_SERVICE.FIELD_TYPE_ATTACHMENT: + new_ticket_custom_field_record = TicketCustomField(name=format_custom_field_dict[key]['field_name'],ticket_id=ticket_id, field_key=key, field_type_id=field_type_id, char_value=update_dict.get(key)) + new_ticket_custom_field_record.save() return True, '' @classmethod