Exemple #1
0
def add_participant(user_email: str,
                    incident_id: id,
                    db_session: SessionLocal,
                    role: ParticipantRoleType = None):
    """Adds a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We add the participant to the incident
    individual = individual_service.get_or_create(db_session=db_session,
                                                  email=user_email)

    participant_role = participant_role_service.create(db_session=db_session,
                                                       role=role)
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        role=participant_role,
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # We add and commit the changes
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    log.debug(f"{individual.name} has been added to incident {incident.name}.")

    return True
def test_create(session, participant_role):
    from dispatch.participant_role.service import create
    from dispatch.participant_role.models import ParticipantRoleCreate, ParticipantRoleType

    role = ParticipantRoleType.incident_commander

    participant_role_in = ParticipantRoleCreate(role=role)
    participant_role = create(db_session=session, participant_role_in=participant_role_in)
    assert participant_role.role == role
Exemple #3
0
def get_or_create(
    *,
    db_session,
    incident_id: int,
    individual_id: int,
    service_id: int,
    participant_roles: List[ParticipantRoleCreate],
) -> Participant:
    """Gets an existing participant object or creates a new one."""
    from dispatch.incident import service as incident_service

    participant = (db_session.query(Participant).filter(
        Participant.incident_id == incident_id).filter(
            Participant.individual_contact_id == individual_id).one_or_none())

    if not participant:
        incident = incident_service.get(db_session=db_session,
                                        incident_id=incident_id)

        # We get information about the individual
        individual_contact = individual_service.get(
            db_session=db_session, individual_contact_id=individual_id)

        individual_info = {}
        contact_plugin = plugin_service.get_active_instance(
            db_session=db_session,
            project_id=incident.project.id,
            plugin_type="contact")
        if contact_plugin:
            individual_info = contact_plugin.instance.get(
                individual_contact.email, db_session=db_session)

        location = individual_info.get("location", "Unknown")
        team = individual_info.get("team", "Unknown")
        department = individual_info.get("department", "Unknown")

        participant_in = ParticipantCreate(
            participant_roles=participant_roles,
            team=team,
            department=department,
            location=location,
        )

        if service_id:
            participant_in.service = {"id": service_id}

        participant = create(db_session=db_session,
                             participant_in=participant_in)
    else:
        for participant_role in participant_roles:
            participant.participant_roles.append(
                participant_role_service.create(
                    db_session=db_session,
                    participant_role_in=participant_role))
        participant.service_id = service_id

    return participant
Exemple #4
0
def create(*, db_session, participant_in: ParticipantCreate) -> Participant:
    """
    Create a new participant.
    """
    participant_roles = [
        participant_role_service.create(db_session=db_session, participant_role_in=participant_role)
        for participant_role in participant_in.participant_role
    ]
    participant = Participant(
        **participant_in.dict(exclude={"participant_role"}), participant_role=participant_roles
    )
    db_session.add(participant)
    db_session.commit()
    return participant
Exemple #5
0
def add_participant(
    user_email: str,
    incident_id: id,
    db_session: SessionLocal,
    service: Service = None,
    role: ParticipantRoleType = None,
):
    """Adds a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We get or create a new individual
    individual = individual_service.get_or_create(db_session=db_session,
                                                  email=user_email)

    # We create a role for the participant
    participant_role_in = ParticipantRoleCreate(role=role)

    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)

    # We get or create a new participant
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        service=service,
        participant_roles=[participant_role],
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # We add and commit the changes
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=
        f"{individual.name} added to incident with {participant_role.role} role",
        incident_id=incident_id,
    )

    return participant
Exemple #6
0
def reactivate_participant(user_email: str, incident_id: int,
                           db_session: SessionLocal):
    """Reactivates a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We get information about the individual
    contact_plugin = plugin_service.get_active(db_session=db_session,
                                               plugin_type="contact")
    individual_info = contact_plugin.instance.get(user_email)
    individual_fullname = individual_info["fullname"]

    log.debug(
        f"Reactivating {individual_fullname} on incident {incident.name}...")

    participant = get_by_incident_id_and_email(db_session=db_session,
                                               incident_id=incident_id,
                                               email=user_email)

    if not participant:
        log.debug(
            f"{individual_fullname} is not an inactive participant of incident {incident.name}."
        )
        return False

    # We mark the participant as active
    participant.is_active = True

    # We create a role for the participant
    participant_role_in = ParticipantRoleCreate(
        role=ParticipantRoleType.participant)
    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)
    participant.participant_roles.append(participant_role)

    # We add and commit the changes
    db_session.add(participant)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=f"{individual_fullname} reactivated",
        incident_id=incident_id,
    )

    return True
Exemple #7
0
def reactivate_participant(user_email: str,
                           incident: Incident,
                           db_session: SessionLocal,
                           service_id: int = None):
    """Reactivates a participant."""
    participant = get_by_incident_id_and_email(db_session=db_session,
                                               incident_id=incident.id,
                                               email=user_email)

    if not participant:
        log.debug(
            f"{user_email} is not an inactive participant of {incident.name} incident."
        )
        return False

    log.debug(
        f"Reactivating {participant.individual.name} on {incident.name} incident..."
    )

    # we get the last active role
    participant_role = participant_role_service.get_last_active_role(
        db_session=db_session, participant_id=participant.id)
    # we create a new role based on the last active role
    participant_role_in = ParticipantRoleCreate(role=participant_role.role)
    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)
    participant.participant_roles.append(participant_role)

    if service_id:
        service = service_service.get(db_session=db_session,
                                      service_id=service_id)
        participant.service = service

    db_session.add(participant)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=f"{participant.individual.name} has been reactivated",
        incident_id=incident.id,
    )

    return True
Exemple #8
0
def reactivate_participant(user_email: str, incident_id: int,
                           db_session: SessionLocal):
    """Reactivates a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We get information about the individual
    contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG)
    individual_info = contact_plugin.get(user_email)
    individual_fullname = individual_info["fullname"]

    log.debug(
        f"Reactivating {individual_fullname} on incident {incident.name}...")

    participant = get_by_incident_id_and_email(db_session=db_session,
                                               incident_id=incident_id,
                                               email=user_email)

    if not participant:
        log.debug(
            f"{individual_fullname} is not an inactive participant of incident {incident.name}."
        )
        return False

    # We mark the participant as active
    participant.is_active = True

    # We create a role for the participant
    participant_role_in = ParticipantRoleCreate(
        role=ParticipantRoleType.participant)
    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)
    participant.participant_role.append(participant_role)

    # We add and commit the changes
    db_session.add(participant)
    db_session.commit()

    log.debug(f"{individual_fullname} has been reactivated.")

    return True
Exemple #9
0
def add_participant(user_email: str,
                    incident_id: id,
                    db_session: SessionLocal,
                    role: ParticipantRoleType = None):
    """Adds a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We get or create a new individual
    individual = individual_service.get_or_create(db_session=db_session,
                                                  email=user_email)

    # We create a role for the participant
    participant_role_in = ParticipantRoleCreate(role=role)
    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)

    # We get or create a new participant
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        participant_roles=[participant_role],
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # We add and commit the changes
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    log.debug(
        f"{individual.name} with email address {individual.email} has been added to incident id {incident.id} with role {participant_role.role}."
    )

    return participant
Exemple #10
0
def create(*, db_session, participant_in: ParticipantCreate) -> Participant:
    """Create a new participant."""
    participant_roles = [
        participant_role_service.create(db_session=db_session,
                                        participant_role_in=participant_role)
        for participant_role in participant_in.participant_roles
    ]

    service = None
    if participant_in.service:
        service = service_service.get(db_session=db_session,
                                      service_id=participant_in.service.id)

    participant = Participant(
        **participant_in.dict(exclude={"participant_roles", "service"}),
        service=service,
        participant_roles=participant_roles,
    )

    db_session.add(participant)
    db_session.commit()
    return participant
Exemple #11
0
def create(
    *,
    db_session,
    incident_priority: str,
    incident_type: str,
    reporter_email: str,
    title: str,
    status: str,
    description: str,
) -> Incident:
    participants = []

    # TODO should some of this logic be in the incident_create_flow_instead? (kglisson)
    incident_priority = incident_priority_service.get_by_name(
        db_session=db_session, name=incident_priority["name"])

    incident_type = incident_type_service.get_by_name(
        db_session=db_session, name=incident_type["name"])

    commander_email = resolve_incident_commander_email(
        db_session,
        reporter_email,
        incident_type.name,
        incident_priority.name,
        "",
        title,
        description,
    )

    commander_info = individual_service.resolve_user_by_email(commander_email)

    incident_commander_role = participant_role_service.create(
        db_session=db_session, role=ParticipantRoleType.incident_commander)

    commander_participant = participant_service.create(
        db_session=db_session, participant_role=[incident_commander_role])

    commander = individual_service.get_or_create(
        db_session=db_session,
        email=commander_info["email"],
        name=commander_info["fullname"],
        weblink=commander_info["weblink"],
    )

    incident_reporter_role = participant_role_service.create(
        db_session=db_session, role=ParticipantRoleType.reporter)

    if reporter_email == commander_email:
        commander_participant.participant_role.append(incident_reporter_role)
    else:
        reporter_participant = participant_service.create(
            db_session=db_session, participant_role=[incident_reporter_role])
        reporter_info = individual_service.resolve_user_by_email(
            reporter_email)
        reporter = individual_service.get_or_create(
            db_session=db_session,
            email=reporter_info["email"],
            name=reporter_info["fullname"],
            weblink=commander_info["weblink"],
        )
        reporter.participant.append(reporter_participant)
        db_session.add(reporter)
        participants.append(reporter_participant)

    participants.append(commander_participant)
    incident = Incident(
        title=title,
        description=description,
        status=status,
        incident_priority=incident_priority,
        incident_type=incident_type,
        participants=participants,
    )

    commander.participant.append(commander_participant)
    db_session.add(commander)
    db_session.add(incident)
    db_session.commit()
    return incident