Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of otaku-info-web. 

5 

6otaku-info-web is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11otaku-info-web is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with otaku-info-web. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import time 

21from typing import Dict, Any 

22from puffotter.flask.base import db 

23from puffotter.flask.db.ModelMixin import ModelMixin 

24from otaku_info_web.db.MediaId import MediaId 

25from otaku_info_web.utils.anilist.api import guess_latest_manga_chapter 

26 

27 

28class MangaChapterGuess(ModelMixin, db.Model): 

29 """ 

30 Database model that keeps track of manga chapter guesses. 

31 """ 

32 

33 __tablename__ = "manga_chapter_guesses" 

34 """ 

35 The name of the database table 

36 """ 

37 

38 def __init__(self, *args, **kwargs): 

39 """ 

40 Initializes the Model 

41 :param args: The constructor arguments 

42 :param kwargs: The constructor keyword arguments 

43 """ 

44 super().__init__(*args, **kwargs) 

45 

46 media_id_id: int = db.Column( 

47 db.Integer, 

48 db.ForeignKey( 

49 "media_ids.id", ondelete="CASCADE", onupdate="CASCADE" 

50 ), 

51 nullable=False, 

52 unique=True 

53 ) 

54 """ 

55 The ID of the media ID referenced by this manga chapter guess 

56 """ 

57 

58 media_id: MediaId = db.relationship( 

59 "MediaId", 

60 backref=db.backref( 

61 "manga_chapter_guesses", lazy=True, cascade="all,delete" 

62 ) 

63 ) 

64 """ 

65 The media ID referenced by this manga chapter guess 

66 """ 

67 

68 guess: int = db.Column(db.Integer, nullable=True) 

69 """ 

70 The actual guess for the most current chapter of the manga series 

71 """ 

72 

73 last_update: int = db.Column(db.Integer, nullable=False, default=0) 

74 """ 

75 Timestamp from when the guess was last updated 

76 """ 

77 

78 def update(self): 

79 """ 

80 Updates the manga chapter guess 

81 (if the latest guess is older than an hour) 

82 :return: None 

83 """ 

84 delta = time.time() - self.last_update 

85 if delta > 60 * 60: 

86 self.last_update = int(time.time()) 

87 self.guess = guess_latest_manga_chapter(self.media_id.service_id) 

88 

89 def __json__(self, include_children: bool = False) -> Dict[str, Any]: 

90 """ 

91 Generates a dictionary containing the information of this model 

92 :param include_children: Specifies if children data models 

93 will be included or if they're limited to IDs 

94 :return: A dictionary representing the model's values 

95 """ 

96 data = { 

97 "id": self.id, 

98 "media_id_id": self.media_id_id, 

99 "guess": self.guess, 

100 "last_update": self.last_update 

101 } 

102 if include_children: 

103 data["media_id"] = self.media_id.__json__(include_children) 

104 return data