Skip to content

Commit

Permalink
1. bugfix: create or handle ticket fail when there are some optional …
Browse files Browse the repository at this point in the history
…field in the ticket detail form and user set the field none value

2. optimize: parameter type checking in new ticket api
  • Loading branch information
blackholll committed Jun 20, 2019
1 parent 8f8f454 commit a9fe17c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 58 deletions.
5 changes: 5 additions & 0 deletions apps/ticket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')))

Expand Down
126 changes: 68 additions & 58 deletions service/ticket/ticket_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a9fe17c

Please sign in to comment.