Skip to content

Commit

Permalink
Add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Apr 15, 2024
1 parent c812fe9 commit ced5979
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
20 changes: 19 additions & 1 deletion qtribu/gui/dlg_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def donate(self) -> None:
open_url_in_browser("https://geotribu.fr/team/sponsoring/")

def refresh_list(self, query_action: Callable[[], str]) -> None:
"""
Refresh content list as well as treewidget that list all contents
:param query_action: action to call for potentially filtering contents
:type query_action: Callable[[], str]
"""
# fetch last RSS items using JSONFeed
rss_contents = self.json_feed_client.fetch(query=query_action())
years = sorted(set([c.date_pub.year for c in rss_contents]), reverse=True)
Expand All @@ -129,6 +135,15 @@ def refresh_list(self, query_action: Callable[[], str]) -> None:

@QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem, int)
def on_tree_view_item_click(self, item: QTreeWidgetItem, column: int):
"""
Method called when a content item is clicked
:param item: item that is clicked by user
:type item: QTreeWidgetItem
:param column: column that is clicked by user
:type column: int
"""
# TODO
print(item, column, item.text(column))

def on_search_text_changed(self) -> None:
Expand All @@ -151,7 +166,10 @@ def on_category_changed(self, value: str) -> None:
@staticmethod
def _build_tree_widget_item_from_content(content: RssItem) -> QTreeWidgetItem:
"""
Builds a QTreeWidgetItem from a RSS content
Builds a QTreeWidgetItem from a RSS content item
:param content: content to generate item for
:type content: RssItem
"""
item = QTreeWidgetItem(
[
Expand Down
36 changes: 35 additions & 1 deletion qtribu/logic/json_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@


class JsonFeedClient:
"""
Class representing a Geotribu's JSON feed client
"""

items: Optional[List[RssItem]] = None
last_fetch_date: Optional[datetime] = None

Expand All @@ -28,6 +32,14 @@ def __init__(
self.url = url

def fetch(self, query: str = "") -> list[RssItem]:
"""
Fetch RSS feed items using JSON Feed
:param query: filter to look for items matching this query
:type query: str
:return: list of RssItem objects matching the query filter
"""
if not self.items or (
self.last_fetch_date
and (datetime.now() - self.last_fetch_date).total_seconds()
Expand All @@ -40,13 +52,26 @@ def fetch(self, query: str = "") -> list[RssItem]:
return [i for i in self.items if self._matches(query, i)]

def categories(self) -> list[str]:
"""
Get a list of all categories available in the RSS feed
:return: list of categories available in the RSS feed
"""
tags = []
for content in self.fetch():
tags.extend([c.lower() for c in content.categories])
return sorted(set(tags))

@staticmethod
def _map_item(item: dict[str, Any]) -> RssItem:
"""
Map raw JSON object coming from JSON feed to an RssItem object
:param item: raw JSON object
:type item: dict[str, Any]
:return: RssItem
"""
return RssItem(
abstract=item.get("content_html"),
author=[i["name"] for i in item.get("authors")],
Expand All @@ -62,7 +87,16 @@ def _map_item(item: dict[str, Any]) -> RssItem:

@staticmethod
def _matches(query: str, item: RssItem) -> bool:
"""Moteur de recherche du turfu"""
"""
Check if item matches given query
:param query: filter to look for items matching this query
:type query: str
:param item: RssItem to check
:type item: RssItem
:return: True if item matches given query, False if not
"""
words = query.split(" ")
if len(words) > 1:
return all([JsonFeedClient._matches(w, item) for w in words])
Expand Down

0 comments on commit ced5979

Please sign in to comment.