From 2289ced2122ed6943c19bf58860806bb22bf0c97 Mon Sep 17 00:00:00 2001 From: decfox Date: Thu, 25 Jul 2024 11:40:47 +0530 Subject: [PATCH] refactor: change incident_id to finding_id in table --- .../a037e908f3a0_init_oonifindings.py | 2 +- .../oonifindings/src/oonifindings/models.py | 2 +- .../src/oonifindings/routers/v1.py | 36 +++++++++---------- .../oonifindings/tests/test_database.py | 8 ++--- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ooniapi/common/src/common/alembic/versions/a037e908f3a0_init_oonifindings.py b/ooniapi/common/src/common/alembic/versions/a037e908f3a0_init_oonifindings.py index 7335aeb6..82ad5390 100644 --- a/ooniapi/common/src/common/alembic/versions/a037e908f3a0_init_oonifindings.py +++ b/ooniapi/common/src/common/alembic/versions/a037e908f3a0_init_oonifindings.py @@ -22,7 +22,7 @@ def upgrade() -> None: op.create_table( "oonifinding", sa.Column( - "incident_id", + "finding_id", sa.String(), nullable=False, primary_key=True, diff --git a/ooniapi/services/oonifindings/src/oonifindings/models.py b/ooniapi/services/oonifindings/src/oonifindings/models.py index 582135d3..b3465f70 100644 --- a/ooniapi/services/oonifindings/src/oonifindings/models.py +++ b/ooniapi/services/oonifindings/src/oonifindings/models.py @@ -12,7 +12,7 @@ class OONIFinding(Base): __tablename__ = "oonifinding" # TODO(decfox): add primary key finding id - incident_id: Mapped[str] = mapped_column(String, primary_key=True) + finding_id: Mapped[str] = mapped_column(String, primary_key=True) create_time: Mapped[datetime] = mapped_column(UtcDateTime()) update_time: Mapped[datetime] = mapped_column(UtcDateTime()) diff --git a/ooniapi/services/oonifindings/src/oonifindings/routers/v1.py b/ooniapi/services/oonifindings/src/oonifindings/routers/v1.py index 7dcf2063..f40b054c 100644 --- a/ooniapi/services/oonifindings/src/oonifindings/routers/v1.py +++ b/ooniapi/services/oonifindings/src/oonifindings/routers/v1.py @@ -170,7 +170,7 @@ def list_oonifindings( findings = [] for row in q.all(): oonifinding = OONIFinding( - id=row.incident_id, + id=row.finding_id, update_time=row.update_time, start_time=row.start_time, end_time=row.end_time, @@ -200,12 +200,12 @@ def list_oonifindings( @router.get( - "/v1/incidents/show/{incident_id}", + "/v1/incidents/show/{finding_id}", tags=["oonifindings"], response_model=OONIFindingIncident ) def get_oonifinding_by_id( - incident_id: str, + finding_id: str, response: Response, authorization: str = Header("authorization"), db=Depends(get_postgresql_session), @@ -216,7 +216,7 @@ def get_oonifinding_by_id( """ log.debug("showing incident") q = db.query(models.OONIFinding).filter( - models.OONIFinding.incident_id == incident_id + models.OONIFinding.finding_id == finding_id ) q = q.filter(models.OONIFinding.deleted != 1) @@ -235,7 +235,7 @@ def get_oonifinding_by_id( raise HTTPException(status_code=404, detail="OONI Finding not found") oonifinding = OONIFindingWithText( - id=finding.incident_id, + id=finding.finding_id, update_time=finding.update_time, start_time=finding.start_time, end_time=finding.end_time, @@ -315,12 +315,12 @@ def create_oonifinding( authorization, jwt_encryption_key=settings.jwt_encryption_key ) now = utcnow_seconds() - incident_id = str(generate_random_intuid(collector_id=settings.collector_id)) + finding_id = str(generate_random_intuid(collector_id=settings.collector_id)) - log.info(f"Creating incident {incident_id}") + log.info(f"Creating incident {finding_id}") db_oonifinding = models.OONIFinding( - incident_id=incident_id, + finding_id=finding_id, create_time=now, update_time=now, start_time=create_request.start_time, @@ -345,7 +345,7 @@ def create_oonifinding( db.commit() setnocacheresponse(response) - return OONIFindingsUpdateResponse(r=1, id=incident_id) + return OONIFindingsUpdateResponse(r=1, id=finding_id) @router.post( @@ -362,11 +362,11 @@ def update_oonifinding( """ Update an incident """ - incident_id = update_request.incident_id + finding_id = update_request.incident_id account_id = token["account_id"] q = db.query(models.OONIFinding).filter( - models.OONIFinding.incident_id == incident_id + models.OONIFinding.finding_id == finding_id ) if token["role"] == "user": q = q.filter(models.OONIFinding.creator_account_id == account_id) @@ -382,7 +382,7 @@ def update_oonifinding( except sa.exc.NoResultFound: raise HTTPException(status_code=404, detail="OONI Finding not found") - log.info(f"Updating incident {incident_id}") + log.info(f"Updating incident {finding_id}") now = utcnow_seconds() oonifinding.update_time = now @@ -404,7 +404,7 @@ def update_oonifinding( db.commit() setnocacheresponse(response) - return OONIFindingsUpdateResponse(r=1, id=incident_id) + return OONIFindingsUpdateResponse(r=1, id=finding_id) @router.post( @@ -422,10 +422,10 @@ def delete_oonifinding( """ assert delete_request account_id = token["account_id"] - incident_id = delete_request.incident_id + finding_id = delete_request.incident_id q = db.query(models.OONIFinding).filter( - models.OONIFinding.incident_id == incident_id + models.OONIFinding.finding_id == finding_id ) if token["role"] == "user": q = q.filter(models.OONIFinding.creator_account_id == account_id) @@ -465,10 +465,10 @@ def update_oonifinding_publish_status( raise HTTPException(status_code=400, detail="Invalid query action") assert publish_request - incident_id = publish_request.incident_id + finding_id = publish_request.incident_id q = db.query(models.OONIFinding).filter( - models.OONIFinding.incident_id == publish_request.incident_id + models.OONIFinding.finding_id == finding_id ) try: @@ -480,4 +480,4 @@ def update_oonifinding_publish_status( db.commit() setnocacheresponse(response) - return OONIFindingsUpdateResponse(r=1, id=incident_id) + return OONIFindingsUpdateResponse(r=1, id=finding_id) diff --git a/ooniapi/services/oonifindings/tests/test_database.py b/ooniapi/services/oonifindings/tests/test_database.py index 8eeee68b..65795b53 100644 --- a/ooniapi/services/oonifindings/tests/test_database.py +++ b/ooniapi/services/oonifindings/tests/test_database.py @@ -83,7 +83,7 @@ def test_upgrade_to_head(postgresql): db_finding = models.OONIFinding( **finding, - incident_id="000000000", + finding_id="000000000", create_time=utcnow_seconds(), update_time=utcnow_seconds(), creator_account_id="000000000", @@ -99,7 +99,7 @@ def test_upgrade_to_head(postgresql): with pytest.raises(sa.exc.StatementError): db_finding = models.OONIFinding( **finding, - incident_id="000000000", + finding_id="000000000", create_time="NOT A DATE", update_time=utcnow_seconds(), creator_account_id="000000000", @@ -112,7 +112,7 @@ def test_upgrade_to_head(postgresql): naive_datetime = datetime.now() db_finding = models.OONIFinding( **finding, - incident_id="000000000", + finding_id="000000000", create_time=naive_datetime, update_time=utcnow_seconds(), creator_account_id="000000000", @@ -124,7 +124,7 @@ def test_upgrade_to_head(postgresql): with pytest.raises(sa.exc.StatementError): db_finding = models.OONIFinding( **finding, - incident_id="000000000", + finding_id="000000000", create_time=None, update_time=utcnow_seconds(), creator_account_id="000000000",