Coverage for an_website / quotes / quote_of_the_day / data.py: 95.238%
21 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-15 14:36 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-15 14:36 +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."""
17from dataclasses import dataclass
18from datetime import date, datetime, timezone # noqa: F401
19from email.utils import format_datetime
20from typing import Any
22from ..utils import WrongQuote
25@dataclass(slots=True)
26class QuoteOfTheDayData:
27 """The class representing data for the quote of the day."""
29 date: date
30 wrong_quote: WrongQuote
31 url_without_path: str
33 def get_date_for_rss(self) -> str:
34 """Get the date as specified in RFC 2822."""
35 return format_datetime(
36 datetime(
37 year=self.date.year,
38 month=self.date.month,
39 day=self.date.day,
40 tzinfo=timezone.utc,
41 ),
42 True,
43 )
45 def get_guid(self) -> str:
46 """Get the GUID for the quote of the day."""
47 return f"{self.date.isoformat()}_{self.wrong_quote.get_id_as_str()}"
49 def get_quote_as_str(self, new_line_char: str = "\n") -> str:
50 """Get the quote as a string with new line."""
51 return (
52 f"»{self.wrong_quote.quote}«{new_line_char}"
53 f"- {self.wrong_quote.author}"
54 )
56 def get_quote_image_url(self) -> str:
57 """Get the URL of the image of the quote."""
58 return self.get_quote_url() + ".gif"
60 def get_quote_url(self) -> str:
61 """Get the URL of the quote."""
62 return (
63 f"{self.url_without_path}/zitate/{self.wrong_quote.get_id_as_str()}"
64 )
66 def get_title(self) -> str:
67 """Get the title for the quote of the day."""
68 return f"Das Zitat des Tages vom {self.date:%d. %m. %Y}"
70 def to_json(self) -> dict[str, str | dict[str, Any]]:
71 """Get the quote of the day as JSON."""
72 return {
73 "date": self.date.isoformat(),
74 "wrong_quote": self.wrong_quote.to_json(),
75 "url": self.get_quote_url(),
76 "image": self.get_quote_image_url(),
77 }