Skip to content

Commit

Permalink
Fix/review checkpoint1 (#37)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
mvergez authored and andriacap committed Jul 28, 2023
1 parent 8740f38 commit 6dd8821
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ 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
# )
statement = sa.text(
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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
Expand Down
21 changes: 14 additions & 7 deletions backend/gn_module_monitoring/monitoring/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions backend/gn_module_monitoring/monitoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion backend/gn_module_monitoring/monitoring/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions backend/gn_module_monitoring/routes/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -35,7 +35,7 @@ def get_types_site():

@blueprint.route("/sites/types/<int:id_type_site>", 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)
Expand All @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions backend/gn_module_monitoring/routes/sites_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion backend/gn_module_monitoring/tests/fixtures/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion backend/gn_module_monitoring/tests/fixtures/type_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions backend/gn_module_monitoring/tests/test_routes/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions backend/gn_module_monitoring/tests/test_utils/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
6 changes: 3 additions & 3 deletions backend/gn_module_monitoring/utils/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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)

Expand Down

0 comments on commit 6dd8821

Please sign in to comment.