-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chore: facility review fixes #2223
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
94eda52
chore: facility_id is uuid, other API fixes
dleard ee6bf03
chore: frontend type fixes for facility_id
dleard c065ca1
chore: split facility_report api/service out of report files
dleard b33ac41
test: add tests for facility_report endpoints
dleard a01f099
test: fix frontend facility review test
dleard 9adf8ad
chore: remove old code
dleard b2aacfd
test: add service tests
dleard dd94e5f
chore: fix incorrect type definition
dleard bb672d8
test: fix service tests
dleard 4a70088
test: fix type expectation in frontend test
dleard fc5e5fe
test: fix backend test
dleard ad2337d
test: add recipe for facility_report
dleard e05b80e
test: update tests with baker reciepes
dleard 1763233
chore: remove unncessary & overriding headers
dleard aedaaea
chore: return type fixes
dleard 4526520
test: update facility report service tests
dleard e4018bc
test: fix frontend test
dleard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from typing import Literal, Tuple | ||
from uuid import UUID | ||
from django.http import HttpRequest | ||
from registration.decorators import handle_http_errors | ||
from reporting.constants import EMISSIONS_REPORT_TAGS | ||
from reporting.schema.generic import Message | ||
from service.facility_report_service import FacilityReportService | ||
from service.error_service.custom_codes_4xx import custom_codes_4xx | ||
from .router import router | ||
from reporting.schema.facility_report import FacilityReportOut, FacilityReportIn | ||
from registration.api.utils.current_user_utils import get_current_user_guid | ||
|
||
|
||
@router.get( | ||
"/report-version/{version_id}/facility-report/{facility_id}", | ||
response={200: FacilityReportOut, 404: Message, 400: Message, 500: Message}, | ||
tags=EMISSIONS_REPORT_TAGS, | ||
description="""Takes `version_id` (primary key of the ReportVersion model) and `facility_id` to return a single matching `facility_report` object. | ||
Includes the associated activity IDs if found; otherwise, returns an error message if not found or in case of other issues.""", | ||
) | ||
@handle_http_errors() | ||
def get_facility_report_form_data( | ||
request: HttpRequest, version_id: int, facility_id: UUID | ||
) -> Tuple[Literal[200], FacilityReportOut]: | ||
facility_report = FacilityReportService.get_facility_report_by_version_and_id(version_id, facility_id) | ||
return 200, FacilityReportOut( | ||
id=facility_report.id, | ||
report_version_id=facility_report.report_version.id, | ||
facility_name=facility_report.facility_name, | ||
facility_type=facility_report.facility_type, | ||
facility_bcghgid=facility_report.facility_bcghgid, | ||
activities=list(facility_report.activities.values_list('id', flat=True)), | ||
products=list(facility_report.products.values_list('id', flat=True)), | ||
) | ||
|
||
|
||
@router.post( | ||
"/report-version/{version_id}/facility-report/{facility_id}", | ||
response={201: FacilityReportOut, custom_codes_4xx: Message}, | ||
tags=EMISSIONS_REPORT_TAGS, | ||
description="""Updates the report facility details by version_id and facility_id. The request body should include | ||
fields to be updated, such as facility name, type, BC GHG ID, activities, and products. Returns the updated report | ||
facility object or an error message if the update fails.""", | ||
) | ||
@handle_http_errors() | ||
def save_facility_report( | ||
request: HttpRequest, version_id: int, facility_id: UUID, payload: FacilityReportIn | ||
) -> Tuple[Literal[201], FacilityReportOut]: | ||
""" | ||
Save or update a report facility and its related activities. | ||
|
||
Args: | ||
request (HttpRequest): The HTTP request object. | ||
version_id (int): The ID of the report version. | ||
facility_id (int): The ID of the facility. | ||
payload (FacilityReportIn): The input data for the report facility. | ||
|
||
Returns: | ||
Tuple: HTTP status code and the response data or an error message. | ||
""" | ||
# Fetch the existing facility report or create a new one | ||
user_guid = get_current_user_guid(request) | ||
facility_report = FacilityReportService.save_facility_report(version_id, facility_id, payload, user_guid) | ||
|
||
# Prepare the response data | ||
response_data = FacilityReportOut( | ||
id=facility_id, | ||
report_version_id=facility_report.report_version.id, | ||
facility_name=facility_report.facility_name, | ||
facility_type=facility_report.facility_type, | ||
facility_bcghgid=facility_report.facility_bcghgid, | ||
activities=list(facility_report.activities.values_list('id', flat=True)), | ||
products=list(facility_report.products.values_list('id', flat=True)), | ||
) | ||
return 201, response_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import json | ||
import pytest | ||
from django.test import Client | ||
from reporting.models import FacilityReport | ||
from registration.tests.utils.bakers import user_baker | ||
from model_bakery import baker | ||
|
||
client = Client() | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestFacilityReportEndpoints: | ||
# GET | ||
def test_error_if_no_facility_report_exists(self): | ||
response = client.get('/api/reporting/report-version/9999/facility-report/00000000-0000-0000-0000-000000000000') | ||
assert response.status_code == 404 | ||
assert response.json()["message"] == "Not Found" | ||
|
||
def test_error_if_no_invalid_facility_id(self): | ||
response = client.get('/api/reporting/report-version/9999/facility-report/1') | ||
assert response.status_code == 422 | ||
assert "Input should be a valid UUID" in response.json()["detail"][0]["msg"] | ||
|
||
def test_returns_correct_data(self): | ||
facility_report = baker.make_recipe('reporting.tests.utils.facility_report') | ||
response = client.get( | ||
f'/api/reporting/report-version/{facility_report.report_version_id}/facility-report/{facility_report.facility_id}' | ||
) | ||
assert response.status_code == 200 | ||
assert response.json()['facility_name'] == facility_report.facility_name | ||
|
||
# POST | ||
def test_saves_facility_data(self): | ||
facility_report = baker.make_recipe('reporting.tests.utils.facility_report') | ||
request_data = { | ||
"facility_name": "CHANGED", | ||
"facility_type": "Single Facility Operation", | ||
"facility_bcghgid": "abc12345", | ||
"activities": ["1", "2", "3"], | ||
"products": [], | ||
} | ||
user = user_baker() | ||
client.post( | ||
f'/api/reporting/report-version/{facility_report.report_version_id}/facility-report/{facility_report.facility_id}', | ||
data=json.dumps(request_data), | ||
HTTP_AUTHORIZATION=json.dumps({"user_guid": f'{user.user_guid}'}), | ||
content_type="application/json", | ||
) | ||
assert FacilityReport.objects.get(pk=facility_report.id).facility_name == "CHANGED" | ||
assert FacilityReport.objects.get(pk=facility_report.id).activities.count() == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from reporting.models.reporting_year import ReportingYear | ||
from reporting.models.report import Report | ||
from reporting.models.report_version import ReportVersion | ||
from reporting.models.facility_report import FacilityReport | ||
from registration.tests.utils.baker_recipes import operation, operator, facility | ||
from model_bakery.recipe import Recipe, foreign_key | ||
|
||
|
||
reporting_year = Recipe(ReportingYear) | ||
|
||
report = Recipe( | ||
Report, | ||
operator=foreign_key(operator), | ||
operation=foreign_key(operation), | ||
reporting_year=foreign_key(reporting_year), | ||
) | ||
|
||
report_version = Recipe(ReportVersion, report=foreign_key(report)) | ||
|
||
facility_report = Recipe(FacilityReport, report_version=foreign_key(report_version), facility=foreign_key(facility)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from uuid import UUID | ||
from django.db import transaction | ||
from typing import List | ||
|
||
from registration.models import Activity | ||
from reporting.models.facility_report import FacilityReport | ||
from reporting.schema.facility_report import FacilityReportIn | ||
|
||
|
||
class FacilityReportService: | ||
@classmethod | ||
def get_facility_report_by_version_and_id(cls, report_version_id: int, facility_id: UUID) -> FacilityReport: | ||
return FacilityReport.objects.get(report_version_id=report_version_id, facility_id=facility_id) | ||
|
||
@classmethod | ||
def get_activity_ids_for_facility(cls, version_id: int, facility_id: UUID) -> List[int]: | ||
facility_report = FacilityReport.objects.get(report_version_id=version_id, facility_id=facility_id) | ||
return list(facility_report.activities.values_list('id', flat=True)) | ||
|
||
@classmethod | ||
@transaction.atomic() | ||
def save_facility_report( | ||
cls, report_version_id: int, facility_id: UUID, data: FacilityReportIn, user_guid: UUID | ||
) -> FacilityReport: | ||
""" | ||
Update a facility report and its related activities. | ||
|
||
Args: | ||
report_version_id (int): The ID of the report version. | ||
facility_id (int): The ID of the facility. | ||
data (FacilityReportIn): The input data for the facility report. | ||
|
||
Returns: | ||
FacilityReport: The updated or created FacilityReport instance. | ||
""" | ||
# Update FacilityReport instance | ||
facility_report = FacilityReport.objects.get(report_version_id=report_version_id, facility_id=facility_id) | ||
facility_report.facility_name = data.facility_name.strip() | ||
facility_report.facility_type = data.facility_type.strip() | ||
|
||
# Update ManyToMany fields (activities) | ||
if data.activities: | ||
facility_report.activities.set(Activity.objects.filter(id__in=data.activities)) | ||
|
||
# Save the updated FacilityReport instance | ||
facility_report.save() | ||
facility_report.set_create_or_update(user_guid) | ||
|
||
return facility_report |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌 👌