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) 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)) 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 test_update(session, tag): from dispatch.tag.service import update from dispatch.tag.models import TagUpdate name = "updated name" tag_in = TagUpdate(name=name, ) tag = update( db_session=session, tag=tag, tag_in=tag_in, ) assert tag.name == name
def update(*, db_session, job: Job, job_in: JobUpdate) -> Job: tags = [] for t in job_in.tags: tags.append( tag_service.get_or_create(db_session=db_session, tag_in=TagUpdate(**t))) scheduled_secondary_workers = [] for w in job_in.scheduled_secondary_workers: scheduled_secondary_workers.append( worker_service.get(db_session=db_session, tag_in=WorkerUpdate(**w))) update_data = job_in.dict( skip_defaults=True, exclude={ "tags", "scheduled_secondary_workers", "requested_primary_worker", "scheduled_primary_worker", "team", "location", }, ) for field in update_data.keys(): setattr(job, field, update_data[field]) job.scheduled_secondary_workers = scheduled_secondary_workers job.tags = tags if job_in.scheduled_primary_worker is not None: job.scheduled_primary_worker = worker_service.get_by_code( db_session=db_session, code=job_in.scheduled_primary_worker.code) if job_in.requested_primary_worker is not None: job.requested_primary_worker = worker_service.get_by_code( db_session=db_session, code=job_in.requested_primary_worker.code) db_session.add(job) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description=f"Job ({job_in.code}) is updated {job_in.flex_form_data}", job_id=job.id, ) post_job_to_kafka(job=job, message_type=KafkaMessageType.UPDATE_JOB, db_session=db_session) return job
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
def update(*, db_session, job: Job, job_in: JobUpdate, org_code: str) -> Job: tags = [] for t in job_in.tags: tags.append(tag_service.get_or_create(db_session=db_session, tag_in=TagUpdate(**t))) scheduled_secondary_workers = [] if job_in.scheduled_secondary_workers: for w in job_in.scheduled_secondary_workers: scheduled_secondary_workers.append( worker_service.get_by_code(db_session=db_session, code=w.code)) if job_in.team and job_in.team.code != job.team.code: team_obj = team_service.get_by_code(db_session=db_session, code=job_in.team.code) job.team = team_obj if job_in.location and job_in.location.location_code and job_in.location.location_code != job.location.location_code: location_obj = location_service.get_or_create_by_code( db_session=db_session, location_in=job_in.location) job.location = location_obj update_data = job_in.dict( skip_defaults=True, exclude={ "tags", "scheduled_secondary_workers", "requested_primary_worker", "scheduled_primary_worker", "team", "location", }, ) for field in update_data.keys(): setattr(job, field, update_data[field]) job.scheduled_secondary_workers = scheduled_secondary_workers job.tags = tags if job_in.scheduled_primary_worker is not None: job.scheduled_primary_worker = worker_service.get_by_code( db_session=db_session, code=job_in.scheduled_primary_worker.code) if job_in.requested_primary_worker is not None: job.requested_primary_worker = worker_service.get_by_code( db_session=db_session, code=job_in.requested_primary_worker.code) db_session.add(job) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description=f"Job ({job_in.code}) is updated {job_in.flex_form_data}", job_id=job.id, ) print(f"\033[37;46m\t1: job update succeed {job.code}\033[0m") post_job_to_kafka(job=job, message_type=KafkaMessageType.UPDATE_JOB, db_session=db_session, org_code=org_code) print(f"\033[37;46m\t2: job psot kafka in succeed {job.code}\033[0m") # zulip send message if job.planning_status != JobPlanningStatus.UNPLANNED: zulip_dict = get_zulip_client_by_org_id(job.org_id) if zulip_dict: zulip_core = zulip_dict['client'] zulip_core.update_job_send_message( job, [job.scheduled_primary_worker] + job.scheduled_secondary_workers) return job