Skip to content

Commit

Permalink
Modify AnnouncementChecker Class with bulletin url
Browse files Browse the repository at this point in the history
  • Loading branch information
shrivaths16 committed Jan 11, 2024
1 parent 02757e6 commit 090e36f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
75 changes: 38 additions & 37 deletions sleap/gui/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

REPO_ID = "talmolab/sleap"
ANALYTICS_ENDPOINT = "https://analytics.sleap.ai/ping"
BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, os.path.pardir)))
BULLETIN_JSON = os.path.join(BASE_DIR, "..", "docs", "bulletin.json")
# TODO: Change the Bulletin URL to the main website before deploying
BULLETIN_JSON_ENDPOINT = "https://sleap.ai/develop/docs/_static/bulletin.json"


@attr.s(auto_attribs=True)
Expand Down Expand Up @@ -156,57 +156,58 @@ class AnnouncementChecker:
"""Checker for new announcements on the bulletin page of sleap."""

state: "GuiState"
bulletin_json_path: str = BULLETIN_JSON
_previous_announcement_date: str = None
_latest_data: Optional[Dict[str, str]] = None
bulletin_json_data: Optional[List[Dict[str, str]]] = None
json_data_url: str = BULLETIN_JSON_ENDPOINT
checked: bool = attr.ib(default=False, init=False)

@property
def previous_announcement_date(self):
_previous_announcement_date = self.state["announcement last seen date"]
return _previous_announcement_date

def _read_bulletin_data(self) -> Optional[Dict]:
"""Reads the bulletin data from the JSON file."""
def check_for_bulletin_data(self) -> Optional[List[Dict]]:
"""Reads the bulletin data from the JSON file endpoint."""
try:
with open(self.bulletin_json_path, "r") as jsf:
data = json.load(jsf)
self._latest_data = data[0]
except FileNotFoundError:
self._latest_data = None
self.checked = True
self.bulletin_json_data = requests.get(self.json_data_url).json()
except (requests.ConnectionError, requests.Timeout):
self.bulletin_json_data = None

def new_announcement_available(self) -> bool:
"""Check if latest announcement is available."""
self._read_bulletin_data()
if self.previous_announcement_date and self._latest_data:
latest_date = datetime.strptime(self._latest_data["date"], "%m/%d/%Y")
previous_date = datetime.strptime(
self.previous_announcement_date, "%m/%d/%Y"
)
if latest_date > previous_date:
return True
if not self.checked:
self.check_for_bulletin_data()
if self.bulletin_json_data:
if self.previous_announcement_date:
latest_date = datetime.strptime(
self.bulletin_json_data[0]["date"], "%m/%d/%Y"
)
previous_date = datetime.strptime(
self.previous_announcement_date, "%m/%d/%Y"
)
if latest_date > previous_date:
return True
else:
return False
else:
return False
return True
else:
return True
return False

def get_latest_announcement(self) -> Optional[Tuple[str, str, str]]:
def update_latest_announcement(self) -> Optional[Tuple[str, str, str]]:
"""Return latest announcements on the releases page not seen by user."""
if self.new_announcement_available():
return (
self._latest_data["title"],
self._latest_data["date"],
self._latest_data["content"],
)
return None

def update_announcement(self):
"""Update the last seen date of announcement in preferences."""
announcement = self.get_latest_announcement()
if announcement is None:
return
self.state["announcement last seen date"] = announcement[1]
new_announcement = "\n".join(announcement[2].split("\n"))
self.state["announcement"] = "## " + announcement[0] + "\n" + new_announcement
announcement_markdown = ""
for announcement in self.bulletin_json_data:
announcement_content = "\n".join(announcement["content"].split("\n"))
announcement_markdown += (
"## " + announcement["title"] + "\n" + announcement_content + "\n"
)
self.state["announcement"] = announcement_markdown
self.state["announcement last seen date"] = self.bulletin_json_data[0][
"date"
]


def get_analytics_data() -> Dict[str, Any]:
Expand Down
42 changes: 21 additions & 21 deletions tests/gui/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,47 +82,47 @@ def test_release_checker():
assert checker.releases[1] != rls_test


def test_announcementchecker(bulletin_json_path):

def test_announcementchecker():
labels = Labels()
context = CommandContext.from_labels(labels=labels)
context.state = {}
context.state["announcement last seen date"] = "10/10/2023"
checker = AnnouncementChecker(
state=context.state, bulletin_json_path=bulletin_json_path
)

# Check if the announcement checker gets the correct date from the app
assert checker.previous_announcement_date == "10/10/2023"

# Create dummy JSON file to check
bulletin_data = [
{"title": "title1", "date": "10/12/2023", "content": "New announcement"},
{"title": "title2", "date": "10/07/2023", "content": "Old Announcment"},
]
with open(bulletin_json_path, "w") as test_file:
json.dump(bulletin_data, test_file)
checker = AnnouncementChecker(state=context.state, bulletin_json_data=bulletin_data)
checker.checked = True
# Check if the announcement checker gets the correct date from the app
assert checker.previous_announcement_date == "10/10/2023"

# Check if latest announcement is fetched
announcement = checker.get_latest_announcement()
assert announcement == ("title1", "10/12/2023", "New announcement")
is_announcement_available = checker.new_announcement_available()
assert is_announcement_available == True

# Concatenate the bulletin content to check updated announcement text
announcement_markdown = ""
for announcement in bulletin_data:
announcement_content = "\n".join(announcement["content"].split("\n"))
announcement_markdown += (
"## " + announcement["title"] + "\n" + announcement_content + "\n"
)

# Check if announcement is updated
checker.update_announcement()
checker.update_latest_announcement()
assert context.state["announcement last seen date"] == "10/12/2023"
assert context.state["announcement"] == "## title1\nNew announcement"
assert context.state["announcement"] == announcement_markdown

# Create dummy JSON file
# Create another dummy JSON file
bulletin_data = [
{"title": "title1", "date": "10/09/2023", "content": "New announcement"},
{"title": "title2", "date": "10/07/2023", "content": "Old Announcment"},
]
with open(bulletin_json_path, "w") as test_file:
json.dump(bulletin_data, test_file)

checker.bulletin_json_data = bulletin_data
# Check to ensure no new announcement is created
announcement = checker.get_latest_announcement()
assert announcement == None
is_announcement_available = checker.new_announcement_available()
assert is_announcement_available == False


def test_get_analytics_data():
Expand Down

0 comments on commit 090e36f

Please sign in to comment.