Skip to content

Commit

Permalink
Hiding importance filter from clients that have restricted coverage i…
Browse files Browse the repository at this point in the history
…nformation [STTNHUB-327] (#900)

* Hiding importance filter from clients that have restricted coverage information [STTNHUB-327]

* address comment

* refactore logic

* add tests

* update logic

* fix flake
  • Loading branch information
devketanpro authored and petrjasek committed May 21, 2024
1 parent e6822f3 commit c8540fc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
3 changes: 2 additions & 1 deletion newsroom/agenda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_public_contacts,
get_links,
get_vocabulary,
get_groups,
)
from newsroom.wire.utils import update_action_list
from newsroom.wire.views import set_item_permission
Expand Down Expand Up @@ -143,7 +144,7 @@ def get_view_data() -> Dict:
"restrict_coverage_info": company.get("restrict_coverage_info", False) if company else False,
"locators": get_vocabulary("locators"),
"ui_config": get_resource_service("ui_config").get_section_config("agenda"),
"groups": app.config.get("AGENDA_GROUPS", []),
"groups": get_groups(app.config.get("AGENDA_GROUPS", []), company),
"has_agenda_featured_items": get_resource_service("agenda_featured").find_one(req=None) is not None,
"user_folders": get_user_folders(user, "agenda") if user else [],
"company_folders": get_company_folders(company, "agenda") if company else [],
Expand Down
8 changes: 8 additions & 0 deletions newsroom/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def assert_never(value: NoReturn) -> NoReturn:
NavigationIds = List[Union[str, ObjectId]]
Section = Literal["wire", "agenda", "monitoring", "news_api", "media_releases", "factcheck", "am_news", "aapX"]
SectionAllowedMap = Dict[Section, bool]
Permissions = Literal["coverage_info"]


class Group(TypedDict):
field: str
label: str
nested: dict
permissions: List[Permissions]


class Country(TypedDict):
Expand Down
15 changes: 14 additions & 1 deletion newsroom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from flask import current_app as app, json, abort, request, g, flash, session, url_for
from flask_babel import gettext

from newsroom.types import PublicUserData, User, Company
from newsroom.types import PublicUserData, User, Company, Group, Permissions
from newsroom.template_filters import (
time_short,
parse_date as parse_short_date,
Expand Down Expand Up @@ -664,3 +664,16 @@ def parse_objectid(value: Union[str, ObjectId]) -> Union[str, ObjectId]:
return ObjectId(value)
except InvalidId:
return value


def get_groups(groups: List[Group], company: Optional[Company]):
company_permissions = get_company_permissions(company)
return [
group
for group in groups
if not group.get("permissions") or all([company_permissions[permission] for permission in group["permissions"]])
]


def get_company_permissions(company: Optional[Company]) -> Dict[Permissions, bool]:
return {"coverage_info": not company or company.get("restrict_coverage_info") is not True}
35 changes: 34 additions & 1 deletion tests/core/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from newsroom.utils import short_highlighted_text
from newsroom.utils import short_highlighted_text, get_groups


def test_short_highlighted_text():
Expand Down Expand Up @@ -80,3 +80,36 @@ def test_short_highlighted_text():
== """On olemassa myös sellaisia ihmisiä, joiden silmissä
jokainen parisuhde muuttuu <span class="es-highlight">mukaanalkuhuuma</span> <span class="es-highlight">kestää</span> yleensä 1–2 vuotta..."""
)


def test_filter_groups():
agenda_groups = [
{
"field": "event_type",
"nested": {
"parent": "subject",
"field": "scheme",
"value": "event_type",
},
"permissions": [],
},
{
"field": "stturgency",
"nested": {
"parent": "subject",
"field": "scheme",
"value": "stturgency",
"include_planning": True,
},
"permissions": ["coverage_info"],
},
]

company = {"_id": "1234", "name": "test company", "restrict_coverage_info": True}

assert len(agenda_groups) == 2

assert len(get_groups(agenda_groups, company)) == 1

company["restrict_coverage_info"] = False
assert len(get_groups(agenda_groups, company)) == 2

0 comments on commit c8540fc

Please sign in to comment.