From a874f58fff16ecdb03d091a9de49e7039fe2e66d Mon Sep 17 00:00:00 2001 From: Maxime Vergez <85738261+mvergez@users.noreply.github.com> Date: Wed, 18 Jan 2023 10:22:03 +0100 Subject: [PATCH] Fix/review checkpoint1 (#37) * refactor(api): change offset to page * refactor(api): rename id_nomenclature * fix(admin): add compare field to Unique Since now, BibTypeSite and TNomenclature do not share the same column anymore (id_nomenclature_type_site vs id_nomenclature) --- .../a54bafb13ce8_create_cor_module_type.py | 4 ++-- .../b53bafb13ce8_create_bib_type_site.py | 14 ++++++------- .../ce54ba49ce5c_create_cor_type_site.py | 4 ++-- .../gn_module_monitoring/monitoring/admin.py | 21 ++++++++++++------- .../gn_module_monitoring/monitoring/models.py | 6 +++--- .../monitoring/schemas.py | 2 +- backend/gn_module_monitoring/routes/site.py | 10 ++++----- .../routes/sites_groups.py | 4 ++-- .../tests/fixtures/site.py | 2 +- .../tests/fixtures/type_site.py | 2 +- .../test_models/test_bib_type_site.py | 8 +++---- .../test_schemas/test_bib_site_type_schema.py | 2 +- .../tests/test_routes/test_site.py | 4 ++-- .../tests/test_utils/test_routes.py | 14 ++++++------- backend/gn_module_monitoring/utils/routes.py | 6 +++--- 15 files changed, 55 insertions(+), 48 deletions(-) diff --git a/backend/gn_module_monitoring/migrations/a54bafb13ce8_create_cor_module_type.py b/backend/gn_module_monitoring/migrations/a54bafb13ce8_create_cor_module_type.py index 718194469..5bb62791f 100644 --- a/backend/gn_module_monitoring/migrations/a54bafb13ce8_create_cor_module_type.py +++ b/backend/gn_module_monitoring/migrations/a54bafb13ce8_create_cor_module_type.py @@ -25,8 +25,8 @@ def upgrade(): "id_type_site", sa.Integer(), sa.ForeignKey( - f"{monitorings_schema}.bib_type_site.id_nomenclature", - name="fk_cor_module_type_id_nomenclature", + f"{monitorings_schema}.bib_type_site.id_nomenclature_type_site", + name="fk_cor_module_type_id_nomenclature_type_site", ondelete="CASCADE", onupdate="CASCADE", ), diff --git a/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py b/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py index f0a4f26f3..b079edf9e 100644 --- a/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py +++ b/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py @@ -22,25 +22,25 @@ def upgrade(): op.create_table( "bib_type_site", sa.Column( - "id_nomenclature", + "id_nomenclature_type_site", sa.Integer(), sa.ForeignKey( f"{nomenclature_schema}.t_nomenclatures.id_nomenclature", - name="fk_t_nomenclatures_id_nomenclature", + name="fk_t_nomenclatures_id_nomenclature_type_site", ), nullable=False, unique=True, ), - sa.PrimaryKeyConstraint("id_nomenclature"), + sa.PrimaryKeyConstraint("id_nomenclature_type_site"), sa.Column("config", sa.JSON(), nullable=True), schema=monitorings_schema, ) # FIXME: if sqlalchemy >= 1.4.32, it should work with postgresql_not_valid=True: cleaner # op.create_check_constraint( - # "ck_bib_type_site_id_nomenclature", + # "ck_bib_type_site_id_nomenclature_type_site", # "bib_type_site", - # f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature,'TYPE_SITE')", + # f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature_type_site,'TYPE_SITE')", # schema=monitorings_schema, # postgresql_not_valid=True # ) @@ -48,9 +48,9 @@ def upgrade(): f""" ALTER TABLE {monitorings_schema}.bib_type_site ADD - CONSTRAINT ck_bib_type_site_id_nomenclature CHECK ( + CONSTRAINT ck_bib_type_site_id_nomenclature_type_site CHECK ( {nomenclature_schema}.check_nomenclature_type_by_mnemonique( - id_nomenclature, 'TYPE_SITE' :: character varying + id_nomenclature_type_site, 'TYPE_SITE' :: character varying ) ) NOT VALID """ diff --git a/backend/gn_module_monitoring/migrations/ce54ba49ce5c_create_cor_type_site.py b/backend/gn_module_monitoring/migrations/ce54ba49ce5c_create_cor_type_site.py index 8befc1f68..f02e267e8 100644 --- a/backend/gn_module_monitoring/migrations/ce54ba49ce5c_create_cor_type_site.py +++ b/backend/gn_module_monitoring/migrations/ce54ba49ce5c_create_cor_type_site.py @@ -24,8 +24,8 @@ def upgrade(): "id_type_site", sa.Integer(), sa.ForeignKey( - f"{monitorings_schema}.bib_type_site.id_nomenclature", - name="fk_cor_type_site_id_nomenclature", + f"{monitorings_schema}.bib_type_site.id_nomenclature_type_site", + name="fk_cor_type_site_id_nomenclature_type_site", ondelete="CASCADE", onupdate="CASCADE", ), diff --git a/backend/gn_module_monitoring/monitoring/admin.py b/backend/gn_module_monitoring/monitoring/admin.py index 5406513ff..5e77d0a0a 100644 --- a/backend/gn_module_monitoring/monitoring/admin.py +++ b/backend/gn_module_monitoring/monitoring/admin.py @@ -11,18 +11,22 @@ class Unique: - """ validator that checks field uniqueness """ - def __init__(self, model, field, message=None): + """validator that checks field uniqueness""" + + def __init__(self, model, field, compare_field, message=None): self.model = model self.field = field + self.compare_field = compare_field if not message: - message = u'A type is already created with this nomenclature' + message = "A type is already created with this nomenclature" self.message = message def __call__(self, form, field): if field.object_data == field.data: return - if self.model.query.filter(getattr(self.model, self.field) == getattr(field.data, self.field)).first(): + if self.model.query.filter( + getattr(self.model, self.field) == getattr(field.data, self.compare_field) + ).first(): raise ValidationError(self.message) @@ -60,9 +64,12 @@ def list_label_nomenclature_formatter(view, _context, model, _name): column_hide_backrefs = False form_args = dict( - nomenclature=dict(query_factory=get_only_nomenclature_asc, get_label=get_label_fr_nomenclature, - validators=[Unique(BibTypeSite, "id_nomenclature")]) + nomenclature=dict( + query_factory=get_only_nomenclature_asc, + get_label=get_label_fr_nomenclature, + validators=[Unique(BibTypeSite, "id_nomenclature_type_site", "id_nomenclature")], + ) ) - column_list = ("nomenclature","config") + column_list = ("nomenclature", "config") column_formatters = dict(nomenclature=list_label_nomenclature_formatter) diff --git a/backend/gn_module_monitoring/monitoring/models.py b/backend/gn_module_monitoring/monitoring/models.py index 936e9c418..65160fbac 100644 --- a/backend/gn_module_monitoring/monitoring/models.py +++ b/backend/gn_module_monitoring/monitoring/models.py @@ -32,7 +32,7 @@ DB.Column( "id_type_site", DB.Integer, - DB.ForeignKey("gn_monitoring.bib_type_site.id_nomenclature"), + DB.ForeignKey("gn_monitoring.bib_type_site.id_nomenclature_type_site"), primary_key=True, ), schema="gn_monitoring") @@ -47,7 +47,7 @@ DB.Column( "id_type_site", DB.Integer, - DB.ForeignKey("gn_monitoring.bib_type_site.id_nomenclature"), + DB.ForeignKey("gn_monitoring.bib_type_site.id_nomenclature_type_site"), primary_key=True, ), schema="gn_monitoring") @@ -58,7 +58,7 @@ class BibTypeSite(DB.Model): __table_args__ = {"schema": "gn_monitoring"} query_class = MonitoringQuery - id_nomenclature = DB.Column(DB.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"), + id_nomenclature_type_site = DB.Column(DB.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"), nullable=False, primary_key=True) config = DB.Column(JSONB) diff --git a/backend/gn_module_monitoring/monitoring/schemas.py b/backend/gn_module_monitoring/monitoring/schemas.py index 085af2751..f34066c55 100644 --- a/backend/gn_module_monitoring/monitoring/schemas.py +++ b/backend/gn_module_monitoring/monitoring/schemas.py @@ -16,7 +16,7 @@ def paginate_schema(schema): class PaginationSchema(Schema): count = fields.Integer() limit = fields.Integer() - offset = fields.Integer() + page = fields.Integer() items = fields.Nested(schema, many=True, dump_only=True) return PaginationSchema diff --git a/backend/gn_module_monitoring/routes/site.py b/backend/gn_module_monitoring/routes/site.py index 989858feb..92a209ad0 100644 --- a/backend/gn_module_monitoring/routes/site.py +++ b/backend/gn_module_monitoring/routes/site.py @@ -6,7 +6,7 @@ from gn_module_monitoring.monitoring.models import BibTypeSite, TMonitoringSites from gn_module_monitoring.utils.routes import ( filter_params, - get_limit_offset, + get_limit_page, get_sort, paginate, sort, @@ -17,9 +17,9 @@ @blueprint.route("/sites/types", methods=["GET"]) def get_types_site(): params = MultiDict(request.args) - limit, page = get_limit_offset(params=params) + limit, page = get_limit_page(params=params) sort_label, sort_dir = get_sort( - params=params, default_sort="id_nomenclature", default_direction="desc" + params=params, default_sort="id_nomenclature_type_site", default_direction="desc" ) query = filter_params(query=BibTypeSite.query, params=params) @@ -35,7 +35,7 @@ def get_types_site(): @blueprint.route("/sites/types/", methods=["GET"]) def get_type_site_by_id(id_type_site): - query = BibTypeSite.query.filter_by(id_nomenclature=id_type_site) + query = BibTypeSite.query.filter_by(id_nomenclature_type_site=id_type_site) res = query.first() schema = BibTypeSiteSchema() return schema.dump(res) @@ -45,7 +45,7 @@ def get_type_site_by_id(id_type_site): def get_sites(): params = MultiDict(request.args) # TODO: add filter support - limit, page = get_limit_offset(params=params) + limit, page = get_limit_page(params=params) sort_label, sort_dir = get_sort( params=params, default_sort="id_base_site", default_direction="desc" ) diff --git a/backend/gn_module_monitoring/routes/sites_groups.py b/backend/gn_module_monitoring/routes/sites_groups.py index 57317445d..e999c2351 100644 --- a/backend/gn_module_monitoring/routes/sites_groups.py +++ b/backend/gn_module_monitoring/routes/sites_groups.py @@ -5,7 +5,7 @@ from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups from gn_module_monitoring.utils.routes import ( filter_params, - get_limit_offset, + get_limit_page, get_sort, paginate, sort, @@ -16,7 +16,7 @@ @blueprint.route("/sites_groups", methods=["GET"]) def get_sites_groups(): params = MultiDict(request.args) - limit, page = get_limit_offset(params=params) + limit, page = get_limit_page(params=params) sort_label, sort_dir = get_sort( params=params, default_sort="id_sites_group", default_direction="desc" ) diff --git a/backend/gn_module_monitoring/tests/fixtures/site.py b/backend/gn_module_monitoring/tests/fixtures/site.py index 397b36703..e3d16d973 100644 --- a/backend/gn_module_monitoring/tests/fixtures/site.py +++ b/backend/gn_module_monitoring/tests/fixtures/site.py @@ -19,7 +19,7 @@ def sites(users, types_site, sites_groups): base_site_description=f"Description{i}", base_site_code=f"Code{i}", geom=geom_4326, - id_nomenclature_type_site=types_site[key].id_nomenclature, + id_nomenclature_type_site=types_site[key].id_nomenclature_type_site, types_site=[types_site[key]], id_sites_group=sites_groups["Site_Groupe"].id_sites_group, ) diff --git a/backend/gn_module_monitoring/tests/fixtures/type_site.py b/backend/gn_module_monitoring/tests/fixtures/type_site.py index 36d419c86..1a213a037 100644 --- a/backend/gn_module_monitoring/tests/fixtures/type_site.py +++ b/backend/gn_module_monitoring/tests/fixtures/type_site.py @@ -31,7 +31,7 @@ def nomenclature_types_site(): def types_site(nomenclature_types_site): types_site = { nomenc_type_site.mnemonique: BibTypeSite( - id_nomenclature=nomenc_type_site.id_nomenclature, config={} + id_nomenclature_type_site=nomenc_type_site.id_nomenclature, config={} ) for nomenc_type_site in nomenclature_types_site } diff --git a/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_bib_type_site.py b/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_bib_type_site.py index ede321732..ae580ccbf 100644 --- a/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_bib_type_site.py +++ b/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_bib_type_site.py @@ -8,16 +8,16 @@ class TestBibTypeSite: def test_get_bib_type_site(self, types_site): type_site = list(types_site.values())[0] get_type_site = BibTypeSite.query.filter_by( - id_nomenclature=type_site.id_nomenclature + id_nomenclature_type_site=type_site.id_nomenclature_type_site ).one() - assert get_type_site.id_nomenclature == type_site.id_nomenclature + assert get_type_site.id_nomenclature_type_site == type_site.id_nomenclature_type_site def test_get_all_bib_type_site(self, types_site): get_types_site = BibTypeSite.query.all() assert all( - type_site.id_nomenclature - in [get_type_site.id_nomenclature for get_type_site in get_types_site] + type_site.id_nomenclature_type_site + in [get_type_site.id_nomenclature_type_site for get_type_site in get_types_site] for type_site in types_site.values() ) diff --git a/backend/gn_module_monitoring/tests/test_monitoring/test_schemas/test_bib_site_type_schema.py b/backend/gn_module_monitoring/tests/test_monitoring/test_schemas/test_bib_site_type_schema.py index e3852cdb1..91aa19e0f 100644 --- a/backend/gn_module_monitoring/tests/test_monitoring/test_schemas/test_bib_site_type_schema.py +++ b/backend/gn_module_monitoring/tests/test_monitoring/test_schemas/test_bib_site_type_schema.py @@ -11,4 +11,4 @@ def test_dump(self, types_site): schema = BibTypeSiteSchema() type_site = schema.dump(one_type_site) - assert type_site["id_nomenclature"] == one_type_site.id_nomenclature + assert type_site["id_nomenclature_type_site"] == one_type_site.id_nomenclature_type_site diff --git a/backend/gn_module_monitoring/tests/test_routes/test_site.py b/backend/gn_module_monitoring/tests/test_routes/test_site.py index 59733d2c9..07448b1a3 100644 --- a/backend/gn_module_monitoring/tests/test_routes/test_site.py +++ b/backend/gn_module_monitoring/tests/test_routes/test_site.py @@ -11,10 +11,10 @@ def test_get_type_site_by_id(self, types_site): r = self.client.get( url_for( "monitorings.get_type_site_by_id", - id_type_site=type_site.id_nomenclature, + id_type_site=type_site.id_nomenclature_type_site, ) ) - assert r.json["id_nomenclature"] == type_site.id_nomenclature + assert r.json["id_nomenclature_type_site"] == type_site.id_nomenclature_type_site def test_get_types_site(self, types_site): schema = BibTypeSiteSchema() diff --git a/backend/gn_module_monitoring/tests/test_utils/test_routes.py b/backend/gn_module_monitoring/tests/test_utils/test_routes.py index 39434141d..e81cb8017 100644 --- a/backend/gn_module_monitoring/tests/test_utils/test_routes.py +++ b/backend/gn_module_monitoring/tests/test_utils/test_routes.py @@ -3,17 +3,17 @@ from gn_module_monitoring.monitoring.models import TMonitoringSites from gn_module_monitoring.monitoring.schemas import MonitoringSitesSchema -from gn_module_monitoring.utils.routes import get_limit_offset, paginate +from gn_module_monitoring.utils.routes import get_limit_page, paginate -@pytest.mark.parametrize("limit, offset", [("1", "2"), (1, 2), ("1", 2), (1, "2")]) -def test_get_limit_offset(limit, offset): - multi_dict = MultiDict([("limit", limit), ("offset", offset)]) +@pytest.mark.parametrize("limit, page", [("1", "2"), (1, 2), ("1", 2), (1, "2")]) +def test_get_limit_page(limit, page): + multi_dict = MultiDict([("limit", limit), ("page", page)]) - comp_limit, comp_offset = get_limit_offset(params=multi_dict) + comp_limit, comp_page = get_limit_page(params=multi_dict) assert isinstance(comp_limit, int) - assert isinstance(comp_offset, int) + assert isinstance(comp_page, int) def test_paginate(sites): @@ -24,4 +24,4 @@ def test_paginate(sites): query=TMonitoringSites.query, schema=MonitoringSitesSchema, limit=limit, page=page ) - assert res.json["offset"] == page + assert res.json["page"] == page diff --git a/backend/gn_module_monitoring/utils/routes.py b/backend/gn_module_monitoring/utils/routes.py index e5adabab0..473f8500b 100644 --- a/backend/gn_module_monitoring/utils/routes.py +++ b/backend/gn_module_monitoring/utils/routes.py @@ -10,8 +10,8 @@ from gn_module_monitoring.monitoring.schemas import paginate_schema -def get_limit_offset(params: MultiDict) -> Tuple[int]: - return int(params.pop("limit", 50)), int(params.pop("offset", 1)) +def get_limit_page(params: MultiDict) -> Tuple[int]: + return int(params.pop("limit", 50)), int(params.pop("page", 1)) def get_sort(params: MultiDict, default_sort: str, default_direction) -> Tuple[str]: @@ -22,7 +22,7 @@ def paginate(query: Query, schema: Schema, limit: int, page: int) -> Response: result = query.paginate(page=page, error_out=False, per_page=limit) pagination_schema = paginate_schema(schema) data = pagination_schema().dump( - dict(items=result.items, count=result.total, limit=limit, offset=page) + dict(items=result.items, count=result.total, limit=limit, page=page) ) return jsonify(data)