Coverage for an_website/quotes/quote_of_the_day/data.py: 96.000%
25 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-16 19:56 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-16 19:56 +0000
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU Affero General Public License as
3# published by the Free Software Foundation, either version 3 of the
4# License, or (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU Affero General Public License for more details.
10#
11# You should have received a copy of the GNU Affero General Public License
12# along with this program. If not, see <https://www.gnu.org/licenses/>.
14"""The dataclass representing a quote of the day."""
16from __future__ import annotations
18from dataclasses import dataclass
19from datetime import date, datetime, timezone # noqa: F401
20from email.utils import format_datetime
21from typing import Any
23from ..utils import WrongQuote
26@dataclass(slots=True)
27class QuoteOfTheDayData:
28 """The class representing data for the quote of the day."""
30 date: date
31 wrong_quote: WrongQuote
32 url_without_path: str
34 def get_date_for_rss(self) -> str:
35 """Get the date as specified in RFC 2822."""
36 return format_datetime(
37 datetime(
38 year=self.date.year,
39 month=self.date.month,
40 day=self.date.day,
41 tzinfo=timezone.utc,
42 ),
43 True,
44 )
46 def get_guid(self) -> str:
47 """Get the GUID for the quote of the day."""
48 return f"{self.date.isoformat()}_{self.wrong_quote.get_id_as_str()}"
50 def get_quote_as_str(self, new_line_char: str = "\n") -> str:
51 """Get the quote as a string with new line."""
52 return (
53 f"»{self.wrong_quote.quote}«{new_line_char}"
54 f"- {self.wrong_quote.author}"
55 )
57 def get_quote_image_url(self) -> str:
58 """Get the URL of the image of the quote."""
59 return self.get_quote_url() + ".gif"
61 def get_quote_url(self) -> str:
62 """Get the URL of the quote."""
63 return (
64 f"{self.url_without_path}/zitate/{self.wrong_quote.get_id_as_str()}"
65 )
67 def get_title(self) -> str:
68 """Get the title for the quote of the day."""
69 return f"Das Zitat des Tages vom {self.date:%d. %m. %Y}"
71 def to_json(self) -> dict[str, str | dict[str, Any]]:
72 """Get the quote of the day as JSON."""
73 return {
74 "date": self.date.isoformat(),
75 "wrong_quote": self.wrong_quote.to_json(),
76 "url": self.get_quote_url(),
77 "image": self.get_quote_image_url(),
78 }