Skip to content

Commit

Permalink
reformat server code using black (#200)
Browse files Browse the repository at this point in the history
* add black

* update github actions

* fix typo
  • Loading branch information
petrjasek authored Feb 15, 2024
1 parent 6157e12 commit 8839733
Show file tree
Hide file tree
Showing 30 changed files with 251 additions and 211 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
working-directory: server

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: '3.8'

Expand All @@ -34,7 +34,9 @@ jobs:

- run: mypy .

- run: nosetests tests
- run: black --check .

- run: pytest tests

- run: python manage.py app:initialize_data

Expand All @@ -48,12 +50,12 @@ jobs:
working-directory: client

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '14.x'

- run: npm install --no-audit
- run: npm ci

- run: npm run build
10 changes: 6 additions & 4 deletions server/cp/commands/fix_events_moment_timezone_2023.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ def run(self, start, end, offset):
},
],
"ingest_provider": {"$exists": 0}, # ignore ingested events
"dates.tz": {"$nin": [
"America/Regina",
"America/Whitehorse",
]},
"dates.tz": {
"$nin": [
"America/Regina",
"America/Whitehorse",
]
},
}

updated_count = 0
Expand Down
43 changes: 27 additions & 16 deletions server/cp/ingest/parser/ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,13 @@ def parse(self, data, provider=None):

if item["type"] == "text":
try:
prev_item = superdesk.get_resource_service("archive").find_one(req=None, ingest_id=item["guid"])
if prev_item is not None and prev_item["extra"]["ap_version"] != ap_item["version"]:
prev_item = superdesk.get_resource_service("archive").find_one(
req=None, ingest_id=item["guid"]
)
if (
prev_item is not None
and prev_item["extra"]["ap_version"] != ap_item["version"]
):
item["rewrite_of"] = prev_item["guid"]
except KeyError:
pass
Expand Down Expand Up @@ -518,9 +523,13 @@ def _parse_subject(self, subject, item):
if subj.get("ap_subject"):
codes = [code.strip() for code in subj["ap_subject"].split(",")]
for ap_subj in subject:
if ap_subj.get('creator') == 'Editorial' and any(
[code for code in codes if ap_subj["code"].startswith(code)]
) and subj["qcode"] not in added:
if (
ap_subj.get("creator") == "Editorial"
and any(
[code for code in codes if ap_subj["code"].startswith(code)]
)
and subj["qcode"] not in added
):
added.add(subj["qcode"])
item["subject"].append(
{
Expand Down Expand Up @@ -735,7 +744,7 @@ def set_cat(cat):
"name": cat["name"],
"qcode": cat["qcode"],
"scheme": CATEGORY_SCHEME,
"translations": cat.get("translations")
"translations": cat.get("translations"),
}
)

Expand Down Expand Up @@ -860,14 +869,16 @@ def _parse_ap_products(self, data, item):
products = data["meta"]["products"]
except KeyError:
return
item.setdefault("subject", []).extend([
{
"name": p["name"],
"qcode": str(p["id"]),
"scheme": cp.AP_PRODUCT,
}
for p in products
])
item.setdefault("subject", []).extend(
[
{
"name": p["name"],
"qcode": str(p["id"]),
"scheme": cp.AP_PRODUCT,
}
for p in products
]
)

def categorisation_mapping(self, in_item, item):
"""Avoid extra mapping."""
Expand Down Expand Up @@ -900,8 +911,8 @@ def clean_html(html):
for elem in root.iter():
elem.attrib.pop("id", None)
elem.attrib.pop("class", None)
if elem.tag in ('hl2', 'pre', 'note'):
elem.tag = 'p'
if elem.tag in ("hl2", "pre", "note"):
elem.tag = "p"

root = cleaner.clean_html(root)
return sd_etree.to_string(root, method="html")
1 change: 0 additions & 1 deletion server/cp/ingest/parser/businesswire.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class BusinessWireParser(NewsMLOneFeedParser):

label = "BusinessWire"

NAME = label.lower()
Expand Down
7 changes: 6 additions & 1 deletion server/cp/ingest/parser/cp_onclusive.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ def find_subject(self, subjects, name):
if (
subject.get("translations")
and subject["translations"].get("name")
and name.lower() in [value.lower() for value in subject["translations"]["name"].values() if value]
and name.lower()
in [
value.lower()
for value in subject["translations"]["name"].values()
if value
]
):
return subject
if subject["name"].lower() == name.lower():
Expand Down
1 change: 0 additions & 1 deletion server/cp/ingest/parser/globenewswire.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@


class GlobeNewswireParser(NewsMLTwoFeedParser):

NAME = "globenewswire"
label = SOURCE
ALLOWED_EXT = {".newsml", ".xml"}
Expand Down
2 changes: 1 addition & 1 deletion server/cp/macros/auto_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def find_name_item(cv_id, name):


def callback(item, **kwargs):
""" This macro will set the language of the articles to the Desk language. """
"""This macro will set the language of the articles to the Desk language."""
rule = kwargs.get("rule")
item["profile"] = "autorouting"
if rule and ":" in rule["name"]:
Expand Down
2 changes: 1 addition & 1 deletion server/cp/macros/translate_to_desk_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


def translate_to_desk_language(item, **kwargs):
""" This macro will set the language of the articles to the Desk language. """
"""This macro will set the language of the articles to the Desk language."""

dest_desk = kwargs.get("dest_desk_id")

Expand Down
1 change: 0 additions & 1 deletion server/cp/orangelogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def count(self, **kwargs):


class OrangelogicSearchProvider(SearchProvider):

label = "Orange Logic"

TZ = "America/Toronto"
Expand Down
4 changes: 3 additions & 1 deletion server/cp/planning_exports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

def init_app(app):
app.jinja_env.globals.update(group_items_by_state=group_items_by_state)
app.jinja_env.globals.update(group_items_by_french_topics=group_items_by_french_topics)
app.jinja_env.globals.update(
group_items_by_french_topics=group_items_by_french_topics
)
57 changes: 37 additions & 20 deletions server/cp/planning_exports/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def set_item_title(item, event):
Prioritise the Event's slugline/name before Planning item's
"""

item["title"] = event.get("name") or item.get("name") or \
event.get("slugline") or item.get("slugline") or \
""
item["title"] = (
event.get("name")
or item.get("name")
or event.get("slugline")
or item.get("slugline")
or ""
)


def set_item_description(item, event):
Expand All @@ -38,18 +42,18 @@ def set_item_description(item, event):
"""

description = (
event.get("definition_long") or
item.get("definition_long") or
event.get("definition_short") or
item.get("definition_short") or
item.get("description_text") or
""
event.get("definition_long")
or item.get("definition_long")
or event.get("definition_short")
or item.get("definition_short")
or item.get("description_text")
or ""
).rstrip()
short_description = (
event.get("definition_short") or
item.get("definition_short") or
item.get("description_text") or
""
event.get("definition_short")
or item.get("definition_short")
or item.get("description_text")
or ""
).rstrip()

if description:
Expand All @@ -73,15 +77,17 @@ def set_item_dates(item, event):
else:
item["dates"] = {
"start": item["planning_date"],
"tz": app.config["DEFAULT_TIMEZONE"]
"tz": app.config["DEFAULT_TIMEZONE"],
}

# Construct the date string here so we don't have to use
# {% if %} {% else %} statements in the template
tz = item["dates"].get("tz") or app.config["DEFAULT_TIMEZONE"]
start_local = utc_to_local(tz, item["dates"]["start"])
start_local_str = start_local.strftime("%I:%M %P")
end_local = utc_to_local(tz, item["dates"]["end"]) if item["dates"].get("end") else None
end_local = (
utc_to_local(tz, item["dates"]["end"]) if item["dates"].get("end") else None
)
end_local_str = end_local.strftime("%I:%M %P") if end_local else None
tz_name = start_local.tzname()

Expand Down Expand Up @@ -117,7 +123,12 @@ def set_item_location(item, event):
try:
address_qcode = (item["location"][0] or {}).get("qcode")
if address_qcode:
address_item = get_resource_service("locations").find_one(req=None, guid=address_qcode) or {}
address_item = (
get_resource_service("locations").find_one(
req=None, guid=address_qcode
)
or {}
)
address = address_item.get("address") or {}

try:
Expand All @@ -127,13 +138,17 @@ def set_item_location(item, event):

item["address"] = {
"country": address["country"] if address.get("country") else None,
"locality": address["locality"] if address.get("locality") else None,
"locality": address["locality"]
if address.get("locality")
else None,
"city": address["city"] if address.get("city") else "",
"state": address["state"] if address.get("state") else None,
"name": address.get("city") or address_item.get("name") or "",
"full": address_item.get("unique_name") or address_item.get("formatted_address") or "",
"full": address_item.get("unique_name")
or address_item.get("formatted_address")
or "",
"title": address_item.get("name") or "",
"address": address_line
"address": address_line,
}
except (IndexError, KeyError):
pass
Expand All @@ -143,7 +158,9 @@ def set_item_location(item, event):
item["address"]["name"] = item["address"]["name"].upper()

if item["address"]["title"] and item["address"]["address"]:
item["address"]["short"] = item["address"]["title"] + ", " + item["address"]["address"]
item["address"]["short"] = (
item["address"]["title"] + ", " + item["address"]["address"]
)
else:
item["address"]["short"] = item["address"]["full"]

Expand Down
32 changes: 19 additions & 13 deletions server/cp/planning_exports/french_news_events_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,20 @@


def get_french_name(item) -> str:
return ((item.get("translations") or {}).get("name") or {}).get("fr-CA") or item.get("name")
return ((item.get("translations") or {}).get("name") or {}).get(
"fr-CA"
) or item.get("name")


def set_item_group(item):
"""Set the item's topic from Calendar or Agenda for grouping"""

qcodes = []
if len(item.get("calendars") or []):
qcodes = [
calendar.get("qcode")
for calendar in item["calendars"]
]
qcodes = [calendar.get("qcode") for calendar in item["calendars"]]
elif len(item.get("agendas") or []):
# Agendas are converted from `_id` to Agenda item in `planning_article_export` endpoint
qcodes = [
agenda.get("_id")
for agenda in item["agendas"]
]
qcodes = [agenda.get("_id") for agenda in item["agendas"]]

if not len(qcodes):
return
Expand Down Expand Up @@ -112,11 +108,17 @@ def group_items_by_french_topics(items):
item_translations = {
"calendars": {
calendar.get("qcode"): get_french_name(calendar).upper()
for calendar in vocabs_service.find_one(req=None, _id="event_calendars").get("items") or []
for calendar in vocabs_service.find_one(
req=None, _id="event_calendars"
).get("items")
or []
},
"coverage_types": {
coverage_type.get("name"): get_french_name(coverage_type).lower()
for coverage_type in vocabs_service.find_one(req=None, _id="g2_content_type").get("items") or []
for coverage_type in vocabs_service.find_one(
req=None, _id="g2_content_type"
).get("items")
or []
},
}

Expand All @@ -140,7 +142,9 @@ def group_items_by_french_topics(items):
date_groups[local_date_str] = {
# Format name for heading, in Canadian French i.e.
# jeudi 24 juin
"name": format_date(item["local_date"], "EEEE d MMMM", locale="fr_CA").capitalize(),
"name": format_date(
item["local_date"], "EEEE d MMMM", locale="fr_CA"
).capitalize(),
"groups": {
item_translations["calendars"].get(qcode): []
for qcode in GROUP_QCODE_ORDER
Expand All @@ -151,6 +155,8 @@ def group_items_by_french_topics(items):
date_groups[local_date_str]["groups"][group_name].append(item)

if not date_groups:
raise SuperdeskApiError.badRequestError(_("No items matched the required calendar/agenda"))
raise SuperdeskApiError.badRequestError(
_("No items matched the required calendar/agenda")
)

return date_groups.items()
Loading

0 comments on commit 8839733

Please sign in to comment.