def create(*, db_session, team_contact_in: TeamContactCreate) -> TeamContact: terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in team_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in team_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in team_contact_in.incident_types ] project = project_service.get_by_name(db_session=db_session, name=team_contact_in.project.name) team = TeamContact( **team_contact_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), project=project, terms=terms, incident_types=incident_types, incident_priorities=incident_priorities, ) db_session.add(team) db_session.commit() return team
def create( *, db_session, individual_contact_in: IndividualContactCreate) -> IndividualContact: """Creates an individual.""" project = project_service.get_by_name( db_session=db_session, name=individual_contact_in.project.name) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in individual_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in individual_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in individual_contact_in.incident_types ] contact = IndividualContact( **individual_contact_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), terms=terms, incident_types=incident_types, incident_priorities=incident_priorities, project=project, ) db_session.add(contact) db_session.commit() return contact
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service: service_data = jsonable_encoder(service) terms = [term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_types ] update_data = service_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"} ) for field in service_data: if field in update_data: setattr(service, field, update_data[field]) service.terms = terms service.incident_priorities = incident_priorities service.incident_types = incident_types db_session.add(service) db_session.commit() return service
def test_get_by_name(session, incident_priority): from dispatch.incident_priority.service import get_by_name t_incident_priority = get_by_name(db_session=session, project_id=incident_priority.project.id, name=incident_priority.name) assert t_incident_priority.name == incident_priority.name
def update(*, db_session, document: Document, document_in: DocumentUpdate) -> Document: """Updates a document.""" document_data = jsonable_encoder(document) update_data = document_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in document_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_types ] for field in document_data: if field in update_data: setattr(document, field, update_data[field]) document.terms = terms document.incident_priorities = incident_priorities document.incident_types = incident_types db_session.add(document) db_session.commit() return document
def create(*, db_session, document_in: DocumentCreate) -> Document: """Creates a new document.""" terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in document_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_types ] document = Document( **document_in.dict( exclude={"terms", "incident_priorities", "incident_types"}), incident_priorities=incident_priorities, incident_types=incident_types, terms=terms, ) db_session.add(document) db_session.commit() return document
def create(*, db_session, document_in: DocumentCreate) -> Document: """Creates a new document.""" terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in document_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in document_in.incident_types ] project = project_service.get_by_name(db_session=db_session, name=document_in.project.name) # set the last reminder to now if document_in.evergreen: document_in.evergreen_last_reminder_at = datetime.utcnow() document = Document( **document_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), incident_priorities=incident_priorities, incident_types=incident_types, terms=terms, project=project, ) db_session.add(document) db_session.commit() return document
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident: incident_data = jsonable_encoder(incident) incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_in.incident_priority.name) incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_in.incident_type.name) # TODO find code to add command as participant incident.commander = commander # commander = individual_service.get_by_email( # db_session=db_session, email=incident_in.commander.email # ) update_data = incident_in.dict( skip_defaults=True, exclude={"incident_type", "incident_priority", "commander"}) for field in incident_data: if field in update_data: setattr(incident, field, update_data[field]) incident.incident_priority = incident_priority incident.incident_type = incident_type db_session.add(incident) db_session.commit() return incident
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service: """Updates an existing service.""" service_data = jsonable_encoder(service) terms = [term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_types ] update_data = service_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"} ) if service_in.is_active: # user wants to enable the service oncall_plugin = plugin_service.get_by_slug(db_session=db_session, slug=service_in.type) if not oncall_plugin.enabled: raise InvalidConfiguration( f"Cannot enable service: {service.name}. Its associated plugin {oncall_plugin.title} is not enabled." ) for field in service_data: if field in update_data: setattr(service, field, update_data[field]) service.terms = terms service.incident_priorities = incident_priorities service.incident_types = incident_types db_session.add(service) db_session.commit() return service
def create(*, db_session, service_in: ServiceCreate) -> Service: """Creates a new service.""" project = project_service.get_by_name(db_session=db_session, name=service_in.project.name) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, project_id=project.id, name=n.name) for n in service_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, project_id=project.id, name=n.name) for n in service_in.incident_types ] service = Service( **service_in.dict(exclude={ "terms", "incident_priorities", "incident_types", "project" }), incident_priorities=incident_priorities, incident_types=incident_types, terms=terms, project=project, ) db_session.add(service) db_session.commit() return service
def update( *, db_session, individual_contact: IndividualContact, individual_contact_in: IndividualContactUpdate, ) -> IndividualContact: individual_contact_data = jsonable_encoder(individual_contact_in) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in individual_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in individual_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in individual_contact_in.incident_types ] update_data = individual_contact_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}) for field in individual_contact_data: if field in update_data: setattr(individual_contact, field, update_data[field]) individual_contact.terms = terms individual_contact.incident_types = incident_types individual_contact.incident_priorities = incident_priorities db_session.add(individual_contact) db_session.commit() return individual_contact
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident: incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_in.incident_priority.name) incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_in.incident_type.name) update_data = incident_in.dict( skip_defaults=True, exclude={ "incident_type", "incident_priority", "commander", "reporter", "status", "visibility", }, ) for field in update_data.keys(): setattr(incident, field, update_data[field]) incident.status = incident_in.status incident.visibility = incident_in.visibility incident.incident_priority = incident_priority incident.incident_type = incident_type db_session.add(incident) db_session.commit() return incident
def update(*, db_session, team_contact: TeamContact, team_contact_in: TeamContactUpdate) -> TeamContact: team_contact_data = jsonable_encoder(team_contact) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in team_contact_in.terms ] incident_priorities = [ incident_priority_service.get_by_name( db_session=db_session, project_id=team_contact.project.id, name=n.name) for n in team_contact_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, project_id=team_contact.project.id, name=n.name) for n in team_contact_in.incident_types ] update_data = team_contact_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}) for field in team_contact_data: if field in update_data: setattr(team_contact, field, update_data[field]) team_contact.terms = terms team_contact.incident_priorities = incident_priorities team_contact.incident_types = incident_types db_session.add(team_contact) db_session.commit() return team_contact
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident: """Updates an existing incident.""" incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_in.incident_priority.name) incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_in.incident_type.name) tags = [] for t in incident_in.tags: tags.append( tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) terms = [] for t in incident_in.terms: terms.append( term_service.get_or_create(db_session=db_session, term_in=TermUpdate(**t))) duplicates = [] for d in incident_in.duplicates: duplicates.append(get(db_session=db_session, incident_id=d.id)) update_data = incident_in.dict( skip_defaults=True, exclude={ "incident_type", "incident_priority", "commander", "reporter", "status", "visibility", "tags", "terms", "duplicates", }, ) for field in update_data.keys(): setattr(incident, field, update_data[field]) incident.terms = terms incident.tags = tags incident.duplicates = duplicates incident.status = incident_in.status incident.visibility = incident_in.visibility incident.incident_priority = incident_priority incident.incident_type = incident_type db_session.add(incident) db_session.commit() return incident
def create_recommendation(*, db_session, text=str, context: ContextBase, matched_terms: List[Term], resources: List[Any]): """Create recommendation object for accuracy tracking.""" accuracy = [ RecommendationAccuracy(resource_id=r.id, resource_type=type(r).__name__) for r in resources ] incident_priorities = [] incident_types = [] if context: incident_priorities = [ incident_priority_service.get_by_name( db_session=db_session, project_id=context.project.id, name=n.name) for n in context.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, project_id=context.project.id, name=n.name) for n in context.incident_types ] service_contacts = [x for x in resources if type(x).__name__ == "Service"] individual_contacts = [ x for x in resources if type(x).__name__ == "IndividualContact" ] team_contacts = [x for x in resources if type(x).__name__ == "TeamContact"] documents = [x for x in resources if type(x).__name__ == "Document"] log.debug( f"Recommendation: Documents: {documents} Individuals: {individual_contacts} Teams: {team_contacts} Services: {service_contacts}" ) r = Recommendation( accuracy=accuracy, service_contacts=service_contacts, individual_contacts=individual_contacts, team_contacts=team_contacts, documents=documents, incident_types=incident_types, incident_priorities=incident_priorities, matched_terms=matched_terms, text=text, ) db_session.add(r) db_session.commit() return r
def create(*, db_session, service_in: ServiceCreate) -> Service: terms = [term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_types ] service = Service( **service_in.dict(exclude={"terms", "incident_priorities", "incident_types"}), incident_priorities=incident_priorities, incident_types=incident_types, terms=terms, ) db_session.add(service) db_session.commit() return service
def update(*, db_session, document: Document, document_in: DocumentUpdate) -> Document: """Updates a document.""" # reset the last reminder to now if document_in.evergreen: if not document.evergreen: document_in.evergreen_last_reminder_at = datetime.utcnow() document_data = jsonable_encoder(document) update_data = document_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}) terms = [ term_service.get_or_create(db_session=db_session, term_in=t) for t in document_in.terms ] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, project_id=n.project.id, name=n.name) for n in document_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, project_id=n.project.id, name=n.name) for n in document_in.incident_types ] for field in document_data: if field in update_data: setattr(document, field, update_data[field]) document.terms = terms document.incident_priorities = incident_priorities document.incident_types = incident_types db_session.add(document) db_session.commit() return document
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident: incident_data = jsonable_encoder(incident) incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_in.incident_priority.name) incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_in.incident_type.name) update_data = incident_in.dict( skip_defaults=True, exclude={"incident_type", "incident_priority", "commander"}) for field in incident_data: if field in update_data: setattr(incident, field, update_data[field]) incident.incident_priority = incident_priority incident.incident_type = incident_type db_session.add(incident) db_session.commit() return incident
def create( *, db_session, incident_priority: str, incident_type: str, reporter_email: str, title: str, status: str, description: str, tags: List[dict], visibility: str = None, ) -> Incident: """Creates a new incident.""" # We get the incident type by name if not incident_type: incident_type = incident_type_service.get_default(db_session=db_session) if not incident_type: raise Exception("No incident type specified and no default has been defined.") else: incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_type["name"] ) # We get the incident priority by name if not incident_priority: incident_priority = incident_priority_service.get_default(db_session=db_session) if not incident_priority: raise Exception("No incident priority specified and no default has been defined.") else: incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority["name"] ) if not visibility: visibility = incident_type.visibility tag_objs = [] for t in tags: tag_objs.append(tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) # We create the incident incident = Incident( title=title, description=description, status=status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, tags=tag_objs, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # Add other incident roles (e.g. commander and liaison) assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.reporter) assign_incident_role( db_session, incident, reporter_email, ParticipantRoleType.incident_commander ) assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.liaison) return incident
def create( *, db_session, incident_priority: str, incident_type: str, reporter_email: str, title: str, status: str, description: str, tags: List[dict], visibility: str = None, ) -> Incident: """Creates a new incident.""" # We get the incident type by name if not incident_type: incident_type = incident_type_service.get_default( db_session=db_session) if not incident_type: raise Exception( "No incident type specified and no default has been defined.") else: incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_type["name"]) # We get the incident priority by name if not incident_priority: incident_priority = incident_priority_service.get_default( db_session=db_session) if not incident_priority: raise Exception( "No incident priority specified and no default has been defined." ) else: incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority["name"]) if not visibility: visibility = incident_type.visibility tag_objs = [] for t in tags: tag_objs.append( tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) # We create the incident incident = Incident( title=title, description=description, status=status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, tags=tag_objs, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # We add the reporter to the incident reporter_participant = participant_flows.add_participant( reporter_email, incident.id, db_session, ParticipantRoleType.reporter) # We resolve the incident commander email incident_commander_email = resolve_incident_commander_email( db_session, reporter_email, incident_type.name, "", title, description, incident_priority.page_commander, ) if reporter_email == incident_commander_email: # We add the role of incident commander the reporter participant_role_service.add_role( participant_id=reporter_participant.id, participant_role=ParticipantRoleType.incident_commander, db_session=db_session, ) else: # We create a new participant for the incident commander and we add it to the incident participant_flows.add_participant( incident_commander_email, incident.id, db_session, ParticipantRoleType.incident_commander, ) return incident
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
def incident_edit_flow(user_email: str, incident_id: int, action: dict, db_session=None): """Runs the incident edit flow.""" notify = action["submission"]["notify"] incident_title = action["submission"]["title"] incident_description = action["submission"]["description"] incident_type = action["submission"]["type"] incident_priority = action["submission"]["priority"] incident_visibility = action["submission"]["visibility"] conversation_topic_change = False # we load the incident instance incident = incident_service.get(db_session=db_session, incident_id=incident_id) # we update the incident title incident.title = incident_title log.debug(f"Updated the incident title to {incident_title}.") # we update the incident description incident.description = incident_description log.debug(f"Updated the incident description to {incident_description}.") if incident_type != incident.incident_type.name: # we update the incident type incident_type_obj = incident_type_service.get_by_name( db_session=db_session, name=incident_type) incident.incident_type_id = incident_type_obj.id log.debug(f"Updated the incident type to {incident_type}.") conversation_topic_change = True if incident_priority != incident.incident_priority.name: # we update the incident priority incident_priority_obj = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority) incident.incident_priority_id = incident_priority_obj.id log.debug(f"Updated the incident priority to {incident_priority}.") conversation_topic_change = True if incident_visibility != incident.visibility: # we update the incident visibility incident.visibility = incident_visibility log.debug(f"Updated the incident visibility to {incident_visibility}.") if notify == "Yes": send_incident_change_notifications(incident, incident_title, incident_type, incident_priority) # we commit the changes to the incident db_session.add(incident) db_session.commit() if conversation_topic_change: # we update the conversation topic set_conversation_topic(incident) # we get the incident document incident_document = get_document( db_session=db_session, incident_id=incident_id, resource_type=INCIDENT_RESOURCE_INVESTIGATION_DOCUMENT, ) # we update the external ticket update_incident_ticket( incident.ticket.resource_id, title=incident.title, description=incident.description, incident_type=incident_type, priority=incident_priority, commander_email=incident.commander.email, conversation_weblink=incident.conversation.weblink, document_weblink=incident_document.weblink, storage_weblink=incident.storage.weblink, ) log.debug(f"Updated the external ticket {incident.ticket.resource_id}.") # get the incident participants based on incident type and priority individual_participants, team_participants = get_incident_participants( db_session, incident.incident_type, incident.incident_priority, incident.description) # we add the individuals as incident participants for individual in individual_participants: incident_add_or_reactivate_participant_flow(individual.email, incident.id, db_session=db_session) # we get the tactical group notification_group = get_group( db_session=db_session, incident_id=incident.id, resource_type=INCIDENT_RESOURCE_NOTIFICATIONS_GROUP, ) team_participant_emails = [x.email for x in team_participants] # we add the team distributions lists to the notifications group group_plugin = plugins.get(INCIDENT_PLUGIN_GROUP_SLUG) group_plugin.add(notification_group.email, team_participant_emails) log.debug(f"Resolved and added new participants to the incident.")
def create( *, db_session, incident_priority: str, incident_type: str, reporter_email: str, title: str, status: str, description: str, visibility: str = None, ) -> Incident: """Creates a new incident.""" # We get the incident type by name incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_type["name"]) # We get the incident priority by name incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority["name"]) if not visibility: visibility = incident_type.visibility # We create the incident incident = Incident( title=title, description=description, status=status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # We add the reporter to the incident reporter_participant = participant_flows.add_participant( reporter_email, incident.id, db_session, ParticipantRoleType.reporter) # We resolve the incident commander email incident_commander_email = resolve_incident_commander_email( db_session, reporter_email, incident_type.name, "", title, description, incident_priority.page_commander, ) if reporter_email == incident_commander_email: # We add the role of incident commander the reporter participant_role_service.add_role( participant_id=reporter_participant.id, participant_role=ParticipantRoleType.incident_commander, db_session=db_session, ) else: # We create a new participant for the incident commander and we add it to the incident participant_flows.add_participant( incident_commander_email, incident.id, db_session, ParticipantRoleType.incident_commander, ) return incident
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident: """Updates an existing incident.""" incident_priority = incident_priority_service.get_by_name( db_session=db_session, project_id=incident.project.id, name=incident_in.incident_priority.name, ) if not incident_priority.enabled: raise Exception("Incident priority must be enabled.") incident_type = incident_type_service.get_by_name( db_session=db_session, project_id=incident.project.id, name=incident_in.incident_type.name ) if not incident_type.enabled: raise Exception("Incident type must be enabled.") tags = [] for t in incident_in.tags: tags.append(tag_service.get_or_create(db_session=db_session, tag_in=TagUpdate(**t))) terms = [] for t in incident_in.terms: terms.append(term_service.get_or_create(db_session=db_session, term_in=TermUpdate(**t))) duplicates = [] for d in incident_in.duplicates: duplicates.append(get(db_session=db_session, incident_id=d.id)) incident_costs = [] for incident_cost in incident_in.incident_costs: incident_costs.append( incident_cost_service.get_or_create( db_session=db_session, incident_cost_in=incident_cost ) ) update_data = incident_in.dict( skip_defaults=True, exclude={ "commander", "duplicates", "incident_costs", "incident_priority", "incident_type", "reporter", "status", "tags", "terms", "visibility", "project", }, ) for field in update_data.keys(): setattr(incident, field, update_data[field]) incident.duplicates = duplicates incident.incident_costs = incident_costs incident.incident_priority = incident_priority incident.incident_type = incident_type incident.status = incident_in.status incident.tags = tags incident.terms = terms incident.visibility = incident_in.visibility db_session.add(incident) db_session.commit() return incident