Coverage for an_website/quotes/quote_of_the_day/data.py: 95.238%

21 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-10 18: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/>. 

13 

14"""The dataclass representing a quote of the day.""" 

15 

16from dataclasses import dataclass 

17from datetime import date, datetime, timezone # noqa: F401 

18from email.utils import format_datetime 

19from typing import Any 

20 

21from ..utils import WrongQuote 

22 

23 

24@dataclass(slots=True) 

25class QuoteOfTheDayData: 

26 """The class representing data for the quote of the day.""" 

27 

28 date: date 

29 wrong_quote: WrongQuote 

30 url_without_path: str 

31 

32 def get_date_for_rss(self) -> str: 

33 """Get the date as specified in RFC 2822.""" 

34 return format_datetime( 

35 datetime( 

36 year=self.date.year, 

37 month=self.date.month, 

38 day=self.date.day, 

39 tzinfo=timezone.utc, 

40 ), 

41 True, 

42 ) 

43 

44 def get_guid(self) -> str: 

45 """Get the GUID for the quote of the day.""" 

46 return f"{self.date.isoformat()}_{self.wrong_quote.get_id_as_str()}" 

47 

48 def get_quote_as_str(self, new_line_char: str = "\n") -> str: 

49 """Get the quote as a string with new line.""" 

50 return ( 

51 f"»{self.wrong_quote.quote}«{new_line_char}" 

52 f"- {self.wrong_quote.author}" 

53 ) 

54 

55 def get_quote_image_url(self) -> str: 

56 """Get the URL of the image of the quote.""" 

57 return self.get_quote_url() + ".gif" 

58 

59 def get_quote_url(self) -> str: 

60 """Get the URL of the quote.""" 

61 return ( 

62 f"{self.url_without_path}/zitate/{self.wrong_quote.get_id_as_str()}" 

63 ) 

64 

65 def get_title(self) -> str: 

66 """Get the title for the quote of the day.""" 

67 return f"Das Zitat des Tages vom {self.date:%d. %m. %Y}" 

68 

69 def to_json(self) -> dict[str, str | dict[str, Any]]: 

70 """Get the quote of the day as JSON.""" 

71 return { 

72 "date": self.date.isoformat(), 

73 "wrong_quote": self.wrong_quote.to_json(), 

74 "url": self.get_quote_url(), 

75 "image": self.get_quote_image_url(), 

76 }