def create_or_update_task(db_session, incident, task: dict, notify: bool = False, sync_external: bool = True): """Creates a new task in the database or updates an existing one.""" existing_task = task_service.get_by_resource_id( db_session=db_session, resource_id=task["resource_id"]) if existing_task: # we save the existing task status before we attempt to update the record existing_status = existing_task.status task = task_service.update( db_session=db_session, task=existing_task, task_in=TaskUpdate( **task, incident=incident, ), sync_external=sync_external, ) if notify: # determine if task was previously resolved if task.status == TaskStatus.resolved: if existing_status != TaskStatus.resolved: send_task_notification( incident, INCIDENT_TASK_RESOLVED_NOTIFICATION, task.creator, task.assignees, task.description, task.weblink, db_session, ) else: # we don't attempt to create new tasks if the incident is currently closed if incident.status == IncidentStatus.closed: return task = task_service.create( db_session=db_session, task_in=TaskCreate(**task, incident=incident), ) if notify: send_task_notification( incident, INCIDENT_TASK_NEW_NOTIFICATION, task.creator, task.assignees, task.description, task.weblink, db_session, ) db_session.commit()
def test_update(session, task, incident, incident_type, incident_priority, project): from dispatch.task.service import update from dispatch.task.models import TaskUpdate description = "Updated description" incident.incident_type = incident_type incident.incident_priority = incident_priority incident.project = project task.incident = incident task_in = TaskUpdate(description=description, incident=incident) task = update( db_session=session, task=task, task_in=task_in, ) assert task.description == description
def create_or_update_task(db_session, incident, task: dict, notify: bool = False): """Creates a new task in the database or updates an existing one.""" existing_task = task_service.get_by_resource_id( db_session=db_session, resource_id=task["resource_id"]) if existing_task: # save the status before we attempt to update the record existing_status = existing_task.status task = task_service.update(db_session=db_session, task=existing_task, task_in=TaskUpdate(**task)) if notify: # determine if task was previously resolved if task.status == TaskStatus.resolved.value: if existing_status != TaskStatus.resolved.value: send_task_notification( incident.conversation.channel_id, INCIDENT_TASK_RESOLVED_NOTIFICATION, task.assignees, task.description, task.weblink, db_session, ) else: task = task_service.create( db_session=db_session, incident=incident, task_in=TaskCreate(**task), ) if notify: send_task_notification( incident.conversation.channel_id, INCIDENT_TASK_NEW_NOTIFICATION, task.assignees, task.description, task.weblink, db_session, ) db_session.commit()