def get_or_create( *, db_session, incident_id: int, individual_id: int, participant_roles: List[ParticipantRoleType], ) -> Participant: """Gets an existing participant object or creates a new one.""" participant = (db_session.query(Participant).filter( Participant.incident_id == incident_id).filter( Participant.individual_contact_id == individual_id).one_or_none()) if not participant: # We get information about the individual contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG) individual_contact = individual_service.get( db_session=db_session, individual_contact_id=individual_id) individual_info = contact_plugin.get(individual_contact.email) location = individual_info["location"] team = individual_info["team"] department = individual_info["department"] participant_in = ParticipantCreate(participant_roles=participant_roles, team=team, department=department, location=location) participant = create(db_session=db_session, participant_in=participant_in) return participant
def get_or_create( *, db_session, incident_id: int, individual_id: int, participant_roles: List[ParticipantRoleType], ) -> Participant: """Gets an existing participant object or creates a new one.""" participant = (db_session.query(Participant).filter( Participant.incident_id == incident_id).filter( Participant.individual_contact_id == individual_id).one_or_none()) if not participant: # We get information about the individual contact_plugin = plugin_service.get_active(db_session=db_session, plugin_type="contact") individual_contact = individual_service.get( db_session=db_session, individual_contact_id=individual_id) 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) participant = create(db_session=db_session, participant_in=participant_in) return participant
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
def log( db_session, source: str, description: str, incident_id: int, individual_id: int = None, started_at: datetime = None, ended_at: datetime = None, details: dict = None, ) -> Event: """ Logs an event """ uuid = uuid4() if not started_at: started_at = datetime.datetime.utcnow() if not ended_at: ended_at = started_at event_in = EventCreate( uuid=uuid, started_at=started_at, ended_at=ended_at, source=source, description=description, details=details, ) event = create(db_session=db_session, event_in=event_in) incident = incident_service.get(db_session=db_session, incident_id=incident_id) incident.events.append(event) db_session.add(incident) if individual_id: individual = individual_service.get( db_session=db_session, individual_contact_id=individual_id) individual.events.append(event) db_session.add(individual) db_session.commit() logger.info(f"{source}: {description}") return event
def test_delete(session, individual_contact): from dispatch.individual.service import delete, get delete(db_session=session, individual_contact_id=individual_contact.id) assert not get(db_session=session, individual_contact_id=individual_contact.id)
def test_get(session, individual_contact): from dispatch.individual.service import get t_individual_contact = get(db_session=session, individual_contact_id=individual_contact.id) assert t_individual_contact.id == individual_contact.id