Exemple #1
0
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_create(
    session, incident, incident_type, incident_priority, participant, individual_contact
):
    from dispatch.task.service import create
    from dispatch.task.models import TaskCreate

    incident.incident_type = incident_type
    incident.incident_priority = incident_priority
    participant.individual = individual_contact

    task_in = TaskCreate(incident=incident, owner=participant)
    task = create(db_session=session, task_in=task_in)
    assert task
Exemple #3
0
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()