def update(*, db_session, incident_type: IncidentType, incident_type_in: IncidentTypeUpdate) -> IncidentType: """Updates an incident type.""" if incident_type_in.template_document: template_document = document_service.get( db_session=db_session, document_id=incident_type_in.template_document.id) incident_type.template_document = template_document if incident_type_in.commander_service: commander_service = service_service.get( db_session=db_session, service_id=incident_type_in.commander_service.id) incident_type.commander_service = commander_service if incident_type_in.liaison_service: liaison_service = service_service.get( db_session=db_session, service_id=incident_type_in.liaison_service.id) incident_type.liaison_service = liaison_service incident_type_data = jsonable_encoder(incident_type) update_data = incident_type_in.dict( skip_defaults=True, exclude={"commander_service", "liaison_service", "template_document"}) for field in incident_type_data: if field in update_data: setattr(incident_type, field, update_data[field]) db_session.add(incident_type) db_session.commit() return incident_type
def create(*, db_session, incident_type_in: IncidentTypeCreate) -> IncidentType: """Creates an incident type.""" project = project_service.get_by_name(db_session=db_session, name=incident_type_in.project.name) incident_type = IncidentType( **incident_type_in.dict( exclude={"commander_service", "template_document", "project"}), project=project, ) if incident_type_in.template_document: template_document = document_service.get( db_session=db_session, document_id=incident_type_in.template_document.id) incident_type.template_document = template_document if incident_type_in.commander_service: commander_service = service_service.get( db_session=db_session, service_id=incident_type_in.commander_service.id) incident_type.commander_service = commander_service if incident_type_in.liaison_service: liaison_service = service_service.get( db_session=db_session, service_id=incident_type_in.liaison_service.id) incident_type.liaison_service = liaison_service db_session.add(incident_type) db_session.commit() return incident_type
def update(*, db_session, incident_type: IncidentType, incident_type_in: IncidentTypeUpdate) -> IncidentType: """Updates an incident type.""" if incident_type_in.incident_template_document: incident_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.incident_template_document.id) incident_type.incident_template_document = incident_template_document if incident_type_in.executive_template_document: executive_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.executive_template_document.id) incident_type.executive_template_document = executive_template_document if incident_type_in.review_template_document: review_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.review_template_document.id) incident_type.review_template_document = review_template_document if incident_type_in.tracking_template_document: tracking_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.tracking_template_document.id) incident_type.tracking_template_document = tracking_template_document if incident_type_in.commander_service: commander_service = service_service.get( db_session=db_session, service_id=incident_type_in.commander_service.id) incident_type.commander_service = commander_service if incident_type_in.liaison_service: liaison_service = service_service.get( db_session=db_session, service_id=incident_type_in.liaison_service.id) incident_type.liaison_service = liaison_service incident_type_data = incident_type.dict() update_data = incident_type_in.dict( skip_defaults=True, exclude={ "commander_service", "liaison_service", "incident_template_document", "executive_template_document", "tracking_template_document", "review_template_document", }, ) for field in incident_type_data: if field in update_data: setattr(incident_type, field, update_data[field]) db_session.commit() return incident_type
def create(*, db_session, incident_type_in: IncidentTypeCreate) -> IncidentType: """Creates an incident type.""" project = project_service.get_by_name_or_raise( db_session=db_session, project_in=incident_type_in.project) incident_type = IncidentType( **incident_type_in.dict( exclude={ "commander_service", "incident_template_document", "executive_template_document", "tracking_template_document", "review_template_document", "project", }), project=project, ) if incident_type_in.incident_template_document: incident_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.incident_template_document.id) incident_type.incident_template_document = incident_template_document if incident_type_in.executive_template_document: executive_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.executive_template_document.id) incident_type.executive_template_document = executive_template_document if incident_type_in.review_template_document: review_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.review_template_document.id) incident_type.review_template_document = review_template_document if incident_type_in.tracking_template_document: tracking_template_document = document_service.get( db_session=db_session, document_id=incident_type_in.tracking_template_document.id) incident_type.tracking_template_document = tracking_template_document if incident_type_in.commander_service: commander_service = service_service.get( db_session=db_session, service_id=incident_type_in.commander_service.id) incident_type.commander_service = commander_service if incident_type_in.liaison_service: liaison_service = service_service.get( db_session=db_session, service_id=incident_type_in.liaison_service.id) incident_type.liaison_service = liaison_service db_session.add(incident_type) db_session.commit() return incident_type
def update( *, db_session, service_plugin: ServicePlugin, service_plugin_in: ServicePluginUpdate, ) -> ServicePlugin: """ Updates a service_plugin. """ if service_plugin_in.service: service = service_service.get(db_session=db_session, service_id=service_plugin_in.service.id) service_plugin.service = service if service_plugin_in.plugin: plugin = plugin_service.get(db_session=db_session, plugin_id=service_plugin_in.plugin.id) service_plugin.plugin = plugin service_plugin_data = jsonable_encoder(service_plugin) update_data = service_plugin_in.dict(skip_defaults=True, exclude={"service", "plugin"}) for field in service_plugin_data: if field in update_data: setattr(service_plugin, field, update_data[field]) db_session.add(service_plugin) db_session.commit() return service_plugin
def add_planning_plugin( plugin_id: id, service_id: id, db_session: SessionLocal, planning_plugin_type=KandboxPlannerPluginType.kandbox_env, ): """Adds a planning_plugin.""" # We load the service service = service_service.get(db_session=db_session, service_id=service_id) # plugin = plugin_service.get(db_session=db_session, plugin_id=plugin_id) # We get or create a new service_plugin service_plugin = get_or_create( db_session=db_session, service_id=service.id, plugin_id=plugin_id, planning_plugin_type=planning_plugin_type, ) service.planning_plugins.append(service_plugin) # We add and commit the changes db_session.add(service) db_session.commit() event_service.log( db_session=db_session, source="Kandbox Planning", description=f"{plugin.name} is added to service with type {planning_plugin_type}", # service_id=service_id, ) return service_plugin
def remove_planning_plugin(user_email: str, service_id: int, db_session: SessionLocal): """Removes a planning_plugin.""" # We load the service service = service_service.get(db_session=db_session, service_id=service_id) # We get information about the plugin contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG) plugin_info = contact_plugin.get(user_email) plugin_fullname = plugin_info["fullname"] log.debug(f"Removing {plugin_fullname} from service {service.name}...") planning_plugin = get_by_service_id_and_email( db_session=db_session, service_id=service_id, code=user_email ) if not planning_plugin: log.debug( f"Can't remove {plugin_fullname}. They're not an active planning_plugin of service {service.name}." ) return False # We mark the planning_plugin as inactive planning_plugin.is_active = False # We make the planning_plugin renounce to their active roles planning_plugin_active_roles = planning_plugin_role_service.get_all_active_roles( db_session=db_session, planning_plugin_id=planning_plugin.id ) for planning_plugin_active_role in planning_plugin_active_roles: planning_plugin_role_service.renounce_role( db_session=db_session, planning_plugin_role=planning_plugin_active_role ) # We add and commit the changes db_session.add(planning_plugin) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description=f"{planning_plugin.plugin.name} removed from service", service_id=service_id, ) return True
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
def reactivate_planning_plugin(user_email: str, service_id: int, db_session: SessionLocal): """Reactivates a planning_plugin.""" # We load the service service = service_service.get(db_session=db_session, service_id=service_id) # We get information about the plugin contact_plugin = plugins.get(INCIDENT_PLUGIN_CONTACT_SLUG) plugin_info = contact_plugin.get(user_email) plugin_fullname = plugin_info["fullname"] log.debug(f"Reactivating {plugin_fullname} on service {service.name}...") planning_plugin = get_by_service_id_and_email( db_session=db_session, service_id=service_id, code=user_email ) if not planning_plugin: log.debug( f"{plugin_fullname} is not an inactive planning_plugin of service {service.name}." ) return False # We mark the planning_plugin as active planning_plugin.is_active = True # We create a role for the planning_plugin planning_plugin_role_in = ServicePluginRoleCreate(role=ServicePluginRoleType.planning_plugin) planning_plugin_role = planning_plugin_role_service.create( db_session=db_session, planning_plugin_role_in=planning_plugin_role_in ) planning_plugin.planning_plugin_role.append(planning_plugin_role) # We add and commit the changes db_session.add(planning_plugin) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description=f"{plugin_fullname} reactivated", service_id=service_id, ) return True
def create(*, db_session, incident_type_in: IncidentTypeCreate) -> IncidentType: if incident_type_in.template_document: template_document = document_service.get( db_session=db_session, document_id=incident_type_in.template_document.id) if incident_type_in.commander_service: commander_service = service_service.get( db_session=db_session, service_id=incident_type_in.commander_service.id) incident_type = IncidentType( **incident_type_in.dict( exclude={"commander_service", "template_document"}, ), commander_service=commander_service, template_document=template_document, ) db_session.add(incident_type) db_session.commit() return incident_type
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
def test_delete(session, service): from dispatch.service.service import delete, get delete(db_session=session, service_id=service.id) assert not get(db_session=session, service_id=service.id)
def test_get_service(session, service): from dispatch.service.service import get t_service = get(db_session=session, service_id=service.id) assert t_service.id == service.id
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: # we add additional roles to the participant for participant_role in participant_roles: participant.participant_roles.append( participant_role_service.create( db_session=db_session, participant_role_in=participant_role)) # we associate the service with the participant participant.service_id = service_id service = service_service.get(db_session=db_session, service_id=service_id) participant.service = service db_session.commit() return participant