Skip to content
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

Feat/487 reporting app grants #2850

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
8 changes: 5 additions & 3 deletions bc_obps/bc_obps/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"rls",
]

RLS_GRANT_APPS = [
"registration",
"reporting",
]

INSTALLED_APPS = [
# Django apps
"django.contrib.admin",
Expand Down Expand Up @@ -210,6 +215,3 @@
# Bypass CSRF protection in development(for admin login page only)
if not DEBUG and ENVIRONMENT == "dev":
CSRF_TRUSTED_ORIGINS = [f"https://{os.environ.get('BACKEND_HOST')}"]


RLS_FLAG = False
50 changes: 50 additions & 0 deletions bc_obps/reporting/enums/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from enum import Enum


class ReportingTableNames(Enum):
ACTIVITY_JSON_SCHEMA = 'activity_json_schema'
ACTIVITY_SOURCE_TYPE_JSON_SCHEMA = 'activity_source_type_json_schema'
CONFIGURATION_ELEMENT = 'configuration_element'
CONFIGURATION = 'configuration'
CUSTOM_METHODOLOGY_SCHEMA = 'custom_methodology_schema'
EMISSION_CATEGORY_MAPPING = 'emission_category_mapping'
EMISSION_CATEGORY = 'emission_category'
FACILITY_REPORT = 'facility_report'
FUEL_TYPE = 'fuel_type'
GAS_TYPE = 'gas_type'
METHODOLOGY = 'methodology'
NAICS_REGULATORY_VALUE = 'naics_regulatory_values'
PRODUCT_EMISSION_INTENSITY = 'product_emission_intensity'
REPORT_ACTIVITY = 'report_activity'
REPORT_ADDITIONAL_DATA = 'report_additional_data'
REPORT_ATTACHMENT = 'report_attachment'
REPORT_EMISSION = 'report_emission'
REPORT_FUEL = 'report_fuel'
REPORT_METHODOLOGY = 'report_methodology'
REPORT_NEW_ENTRANT_EMISSION = 'report_new_entrant_emission'
REPORT_NEW_ENTRANT_PRODUCTION = 'report_new_entrant_production'
REPORT_NEW_ENTRANT = 'report_new_entrant'
REPORT_NON_ATTRIBUTABLE_EMISSIONS = 'report_non_attributable_emissions'
REPORT_OPERATION_REPRESENTATIVE = 'report_operation_representative'
REPORT_OPERATION = 'report_operation'
REPORT_PERSON_RESPONSIBLE = 'report_person_responsible'
REPORT_PRODUCT_EMISSION_ALLOCATION = 'report_product_emission_allocation'
REPORT_PRODUCT = 'report_product'
REPORT_RAW_ACTIVITY_DATA = 'report_raw_activity_data'
REPORT_SOURCE_TYPE = 'report_source_type'
REPORT_UNIT = 'report_unit'
REPORT_VERIFICATION_VISIT = 'report_verification_visit'
REPORT_VERIFICATION = 'report_verification'
REPORT_VERSION = 'report_version'
REPORT = 'report'
REPORTING_FIELD = 'reporting_field'
REPORTING_YEAR = 'reporting_year'
SOURCE_TYPE = 'source_type'

# M2M tables
CONFIGURATION_ELEMENT_REPORTING_FIELDS = 'configuration_element_reporting_fields'
FACILITY_REPORT_ACTIVITIES = 'facility_report_activities'
REPORT_EMISSION_EMISSION_CATEGORIES = 'report_emission_emission_categories'
REPORT_NON_ATTRIBUTABLE_EMISSIONS_GAS_TYPE = 'report_non_attributable_emissions_gas_type'
REPORT_OPERATION_ACTIVITIES = 'report_operation_activities'
REPORT_OPERATION_REGULATED_PRODUCTS = 'report_operation_regulated_products'
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Migration(migrations.Migration):
),
],
options={
'db_table': 'erc"."verification_visit',
'db_table': 'erc"."report_verification_visit',
'db_table_comment': 'Table to store individual verification visit information',
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ class Migration(migrations.Migration):
hash='9bd782ed46dd36d8a19d8bc2e99025a8cdda7372',
operation='INSERT',
pgid='pgtrigger_set_created_audit_columns_94fd8',
table='erc"."verification_visit',
table='erc"."report_verification_visit',
when='BEFORE',
),
),
Expand All @@ -738,7 +738,7 @@ class Migration(migrations.Migration):
hash='3ac1f4334712fd96d804afb183ae1d5ad384935f',
operation='UPDATE',
pgid='pgtrigger_set_updated_audit_columns_f0954',
table='erc"."verification_visit',
table='erc"."report_verification_visit',
when='BEFORE',
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ class Migration(migrations.Migration):
trigger=pgtrigger.compiler.Trigger(
name='immutable_report_version',
sql=pgtrigger.compiler.UpsertTriggerSql(
func='\n declare\n status text;\n begin\n select rel1.status into status\n from "erc"."report_version" rel1\n join "erc"."report_verification" rel2 on rel2.report_version_id=rel1.id\n join "erc"."verification_visit" rel3 on rel3.report_verification_id=rel2.id\n where rel3.id=new.id\n limit 1;\n\n if status=\'Submitted\' then\n raise exception \'reportverificationvisit record is immutable after a report version has been submitted\';\n end if;\n\n return new;\n end;\n ',
func='\n declare\n status text;\n begin\n select rel1.status into status\n from "erc"."report_version" rel1\n join "erc"."report_verification" rel2 on rel2.report_version_id=rel1.id\n join "erc"."report_verification_visit" rel3 on rel3.report_verification_id=rel2.id\n where rel3.id=new.id\n limit 1;\n\n if status=\'Submitted\' then\n raise exception \'reportverificationvisit record is immutable after a report version has been submitted\';\n end if;\n\n return new;\n end;\n ',
hash='15ba5fea0ce08a292c133688a45344c3fc7447c0',
operation='UPDATE',
pgid='pgtrigger_immutable_report_version_4239c',
table='erc"."verification_visit',
table='erc"."report_verification_visit',
when='BEFORE',
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Generated by Django 5.0.11 on 2025-02-19 19:51

import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('reporting', '0062_industrial_water_processing'),
]

operations = [
pgtrigger.migrations.RemoveTrigger(
model_name='reportverificationvisit',
name='set_created_audit_columns',
),
pgtrigger.migrations.RemoveTrigger(
model_name='reportverificationvisit',
name='set_updated_audit_columns',
),
pgtrigger.migrations.RemoveTrigger(
model_name='reportverificationvisit',
name='immutable_report_version',
),
pgtrigger.migrations.AddTrigger(
model_name='reportverificationvisit',
trigger=pgtrigger.compiler.Trigger(
name='set_created_audit_columns',
sql=pgtrigger.compiler.UpsertTriggerSql(
func="new.created_by_id = (select current_setting('my.guid', true)); new.created_at = now(); return new;",
hash='cb4b1713568f237db7d475fea8ba73fa2e56e17f',
operation='INSERT',
pgid='pgtrigger_set_created_audit_columns_94fd8',
table='erc"."report_verification_visit',
when='BEFORE',
),
),
),
pgtrigger.migrations.AddTrigger(
model_name='reportverificationvisit',
trigger=pgtrigger.compiler.Trigger(
name='set_updated_audit_columns',
sql=pgtrigger.compiler.UpsertTriggerSql(
func="new.updated_by_id = (select current_setting('my.guid', true)); new.updated_at = now(); return new;",
hash='10da55097dd264321136899eb608f93936ee58b5',
operation='UPDATE',
pgid='pgtrigger_set_updated_audit_columns_f0954',
table='erc"."report_verification_visit',
when='BEFORE',
),
),
),
pgtrigger.migrations.AddTrigger(
model_name='reportverificationvisit',
trigger=pgtrigger.compiler.Trigger(
name='immutable_report_version',
sql=pgtrigger.compiler.UpsertTriggerSql(
func='\n declare\n status text;\n begin\n select rel1.status into status\n from "erc"."report_version" rel1\n join "erc"."report_verification" rel2 on rel2.report_version_id=rel1.id\n join "erc"."report_verification_visit" rel3 on rel3.report_verification_id=rel2.id\n where rel3.id=new.id\n limit 1;\n\n if status=\'Submitted\' then\n raise exception \'reportverificationvisit record is immutable after a report version has been submitted\';\n end if;\n\n return new;\n end;\n ',
hash='207c50d383c8a924b1b07972636eeb72f4fa8d18',
operation='UPDATE',
pgid='pgtrigger_immutable_report_version_4239c',
table='erc"."report_verification_visit',
when='BEFORE',
),
),
),
]
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/activity_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from reporting.models import Configuration
import typing
from reporting.utils import validate_overlapping_records
from reporting.models.rls_configs.activity_json_schema import Rls as ActivityJsonSchemaRls


class ActivityJsonSchema(BaseModel):
Expand All @@ -23,6 +24,8 @@ class Meta:
)
db_table = 'erc"."activity_json_schema'

Rls = ActivityJsonSchemaRls

@typing.no_type_check
def save(self, *args, **kwargs) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/activity_source_type_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from reporting.models import SourceType, Configuration
import typing
from reporting.utils import validate_overlapping_records
from reporting.models.rls_configs.activity_source_type_json_schema import Rls as ActivitySourceTypeJsonSchemaRls


class ActivitySourceTypeJsonSchema(BaseModel):
Expand All @@ -30,6 +31,8 @@ class Meta:
db_table_comment = "Intersection table that assigns a json_schema as valid for a period of time given an activity-sourceType pair"
db_table = 'erc"."activity_source_type_json_schema'

Rls = ActivitySourceTypeJsonSchemaRls

@typing.no_type_check
def save(self, *args, **kwargs) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
RangeBoundary,
RangeOperators,
)
from reporting.models.rls_configs.configuration import Rls as ConfigurationRls


class TsTzRange(models.Func):
Expand All @@ -29,3 +30,5 @@ class Meta:
expressions=[(TsTzRange("valid_from", "valid_to", RangeBoundary()), RangeOperators.OVERLAPS)],
),
]

Rls = ConfigurationRls
4 changes: 3 additions & 1 deletion bc_obps/reporting/models/configuration_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from registration.models import Activity
from reporting.models import SourceType, GasType, Methodology, Configuration, ReportingField
import typing

from reporting.models.custom_methodology_schema import CustomMethodologySchema
from reporting.utils import validate_overlapping_records
from reporting.models.rls_configs.configuration_element import Rls as ConfigurationElementRls


class ConfigurationElement(BaseModel):
Expand Down Expand Up @@ -35,6 +35,8 @@ class Meta:
db_table_comment = "Element of a configuration, representing a single relationship between multiple entities. Used to define an allowable activity-sourceType-gasType-methodology relationship as per WCI"
db_table = 'erc"."configuration_element'

Rls = ConfigurationElementRls

@typing.no_type_check
def save(self, *args, **kwargs) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/custom_methodology_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models
from registration.models import Activity
from reporting.models import Configuration, SourceType, GasType, Methodology
from reporting.models.rls_configs.custom_methodology_schema import Rls as CustomMethodologySchemaRls


class CustomMethodologySchema(BaseModel):
Expand All @@ -18,3 +19,5 @@ class CustomMethodologySchema(BaseModel):
class Meta:
db_table = 'erc"."custom_methodology_schema'
db_table_comment = "Custom methodology schema used to define additional fields for reporting"

Rls = CustomMethodologySchemaRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/emission_category.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from common.models import BaseModel
from django.db import models
from reporting.models.rls_configs.emission_category import Rls as EmissionCategoryRls


class EmissionCategory(BaseModel):
Expand All @@ -25,3 +26,5 @@ class Meta:
"This table contains the set of emission categories that greenhouse gas emissions can be counted under."
)
db_table = 'erc"."emission_category'

Rls = EmissionCategoryRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/emission_category_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models
from registration.models import Activity
from reporting.models import SourceType, EmissionCategory
from reporting.models.rls_configs.emission_category_mapping import Rls as EmissionCategoryMappingRls


class EmissionCategoryMapping(BaseModel):
Expand All @@ -16,3 +17,5 @@ class EmissionCategoryMapping(BaseModel):
class Meta:
db_table_comment = "This table contains the set of rules for applying an emission category to an emission based on the activity & source type the emission was reported under."
db_table = 'erc"."emission_category_mapping'

Rls = EmissionCategoryMappingRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/facility_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from registration.models.time_stamped_model import TimeStampedModel
from reporting.models import ReportVersion
from reporting.models.triggers import immutable_report_version_trigger
from reporting.models.rls_configs.facility_report import Rls as FacilityReportRls


class FacilityReport(TimeStampedModel):
Expand Down Expand Up @@ -64,3 +65,5 @@ class Meta(TimeStampedModel.Meta):
*TimeStampedModel.Meta.triggers,
immutable_report_version_trigger(),
]

Rls = FacilityReportRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/fuel_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from common.models import BaseModel
from django.db import models
from reporting.models.rls_configs.fuel_type import Rls as FuelTypeRls


class FuelType(BaseModel):
Expand All @@ -18,3 +19,5 @@ class FuelType(BaseModel):
class Meta:
db_table_comment = "This table contains the list of fuel types that can be reported."
db_table = 'erc"."fuel_type'

Rls = FuelTypeRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/gas_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from common.models import BaseModel
from django.db import models
from reporting.models.rls_configs.gas_type import Rls as GasTypeRls


class GasType(BaseModel):
Expand All @@ -22,3 +23,5 @@ def __str__(self) -> str:
class Meta:
db_table_comment = "This table contains the list of gas types that can be reported as defined in GGERR (Greenhous Gas Emission Reporting Regulation)"
db_table = 'erc"."gas_type'

Rls = GasTypeRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/methodology.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from common.models import BaseModel
from django.db import models
from reporting.models.rls_configs.methodology import Rls as MethodologyRls


class Methodology(BaseModel):
Expand All @@ -10,3 +11,5 @@ class Methodology(BaseModel):
class Meta:
db_table_comment = "Table contains the set of reporting methodologies that can be applied to an emission as outlined in GGERR (Greenhous Gas Emission Reporting Regulation)"
db_table = 'erc"."methodology'

Rls = MethodologyRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/naics_regulatory_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
RangeBoundary,
RangeOperators,
)
from reporting.models.rls_configs.naics_regulatory_value import Rls as NaicsRegulatoryValueRls


class TsTzRange(models.Func):
Expand Down Expand Up @@ -52,3 +53,5 @@ class Meta:
],
),
]

Rls = NaicsRegulatoryValueRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/product_emission_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
RangeBoundary,
RangeOperators,
)
from reporting.models.rls_configs.product_emission_intensity import Rls as ProductEmissionIntensityRls


class TsTzRange(models.Func):
Expand Down Expand Up @@ -54,3 +55,5 @@ class Meta:
],
),
]

Rls = ProductEmissionIntensityRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from registration.models.operator import Operator
from registration.models.time_stamped_model import TimeStampedModel
from reporting.models.reporting_year import ReportingYear
from reporting.models.rls_configs.report import Rls as ReportRls


class Report(TimeStampedModel):
Expand Down Expand Up @@ -35,3 +36,5 @@ class Meta(TimeStampedModel.Meta):
constraints = [
models.UniqueConstraint(name="unique_report_per_reporting_year", fields=['operation', 'reporting_year'])
]

Rls = ReportRls
3 changes: 3 additions & 0 deletions bc_obps/reporting/models/report_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from reporting.models.facility_report import FacilityReport
from reporting.models.report_data_base_model import ReportDataBaseModel
from reporting.models.triggers import immutable_report_version_trigger
from reporting.models.rls_configs.report_activity import Rls as ReportActivityRls


class ReportActivity(ReportDataBaseModel):
Expand Down Expand Up @@ -41,3 +42,5 @@ class Meta(ReportDataBaseModel.Meta):
*ReportDataBaseModel.Meta.triggers,
immutable_report_version_trigger(),
]

Rls = ReportActivityRls
Loading
Loading