Skip to content

Commit

Permalink
feat: home discussions user filter enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
20cents committed Oct 10, 2024
1 parent 0a6c1de commit 008aa06
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
15 changes: 13 additions & 2 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pypnnomenclature.models import BibNomenclaturesTypes, TNomenclatures
from werkzeug.exceptions import Forbidden, NotFound, BadRequest, Conflict
from werkzeug.datastructures import MultiDict
from sqlalchemy import distinct, func, desc, asc, select, case
from sqlalchemy import distinct, func, desc, asc, select, case, or_
from sqlalchemy.orm import joinedload, lazyload, selectinload, contains_eager
from geojson import FeatureCollection, Feature
import sqlalchemy as sa
Expand Down Expand Up @@ -1528,7 +1528,18 @@ def list_all_reports(permissions):

# Filter by id_role for 'pin' type only or if my_reports is true
if type_name == "pin" or my_reports:
query = query.where(TReport.id_role == g.current_user.id_role)
query = query.where(
or_(
TReport.id_role == g.current_user.id_role,
TReport.id_synthese.in_(
select(TReport.id_synthese).where(TReport.id_role == g.current_user.id_role)
),
TReport.synthese.has(Synthese.id_digitiser == g.current_user.id_role),
TReport.synthese.has(
Synthese.cor_observers.any(User.id_role == g.current_user.id_role)
),
)
)

# On vérifie les permissions en lecture sur la synthese
synthese_query = select(Synthese.id_synthese).select_from(Synthese)
Expand Down
11 changes: 6 additions & 5 deletions backend/geonature/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,9 @@ def reports_data(users, synthese_data):
data = []

# do not commit directly on current transaction, as we want to rollback all changes at the end of tests
def create_report(id_synthese, id_role, content, id_type, deleted):
def create_report(id_synthese, id_report, id_role, content, id_type, deleted):
new_report = TReport(
id_report=id_report,
id_synthese=id_synthese,
id_role=id_role,
content=content,
Expand All @@ -982,10 +983,10 @@ def create_report(id_synthese, id_role, content, id_type, deleted):
)
with db.session.begin_nested():
reports = [
(ids[0], users["admin_user"].id_role, "comment1", discussionId, False),
(ids[1], users["admin_user"].id_role, "comment1", alertId, False),
(ids[2], users["user"].id_role, "a_comment1", discussionId, True),
(ids[3], users["user"].id_role, "b_comment1", discussionId, True),
(ids[0], 10001, users["admin_user"].id_role, "comment1", discussionId, False),
(ids[1], 10002, users["admin_user"].id_role, "comment1", alertId, False),
(ids[2], 10003, users["user"].id_role, "a_comment1", discussionId, True),
(ids[3], 10004, users["user"].id_role, "b_comment1", discussionId, True),
]
for id_synthese, *args in reports:
data.append(create_report(id_synthese, *args))
Expand Down
20 changes: 12 additions & 8 deletions backend/geonature/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,23 @@ def test_list_all_reports(
assert isinstance(response.json["items"], list)
assert len(response.json["items"]) >= 0

ids = [s.id_synthese for s in synthese_data.values()]
# TEST WITH MY_REPORTS TRUE
set_logged_user(self.client, users["user"])
response = self.client.get(url_for(url, type="discussion", my_reports="true"))
assert response.status_code == 200
items = response.json["items"]
# Check that all items belong to the current user
id_role = users["user"].id_role
nom_complet = users["user"].nom_complet
assert all(
item["id_role"] == id_role and item["user"]["nom_complet"] == nom_complet
for item in items
)
expected_ids = [
10001, # User is observer
10003, # User is report owner
10004, # User is report owner
]
# Missing cases:
# - User is digitiser
# - User has post a report in the same synthese
# They involve adding data to the `synthese_data` fixture, which could cause other tests to fail.
item_ids = [item["id_report"] for item in items]
item_ids.sort()
assert expected_ids == item_ids

# Test undefined type
response = self.client.get(url_for(url, type="UNKNOW-REPORT-TYPE", my_reports="true"))
Expand Down

0 comments on commit 008aa06

Please sign in to comment.