Coverage for otaku_info/routes/schedule.py: 40%

Shortcuts 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

30 statements  

1"""LICENSE 

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

3 

4This file is part of otaku-info. 

5 

6otaku-info 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 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. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import time 

21from typing import List 

22from datetime import datetime 

23from flask import render_template 

24from flask.blueprints import Blueprint 

25from flask_login import current_user, login_required 

26from jerrycan.base import db 

27from otaku_info.db.MediaUserState import MediaUserState 

28from otaku_info.db.MediaIdMapping import MediaIdMapping 

29from otaku_info.enums import MediaType 

30 

31 

32def define_blueprint(blueprint_name: str) -> Blueprint: 

33 """ 

34 Defines a Blueprint that handles schedule-related stuff 

35 :param blueprint_name: The name of the blueprint 

36 :return: The blueprint 

37 """ 

38 blueprint = Blueprint(blueprint_name, __name__) 

39 

40 @blueprint.route("/schedule/anime_week") 

41 @login_required 

42 def anime_week(): 

43 """ 

44 Shows the seasonal anime schedule for a user's media entries 

45 :return: None 

46 """ 

47 media_user_states: List[MediaUserState] = MediaUserState.query\ 

48 .filter_by(media_type=MediaType.ANIME, user_id=current_user.id)\ 

49 .options(db.joinedload(MediaUserState.media_item))\ 

50 .all() 

51 

52 time_limit = datetime.fromtimestamp(time.time() + 7 * 24 * 60 * 60) 

53 media_user_states = [ 

54 x for x in media_user_states 

55 if x.media_item.next_episode_airing_time is not None 

56 and time_limit > x.media_item.next_episode_datetime 

57 ] 

58 

59 weekdays = { 

60 1: "Monday", 

61 2: "Tuesday", 

62 3: "Wednesday", 

63 4: "Thursday", 

64 5: "Friday", 

65 6: "Saturday", 

66 7: "Sunday" 

67 } 

68 entries_by_weekday = [] 

69 

70 for weekday, weekday_name in weekdays.items(): 

71 entries = [] 

72 for user_state in media_user_states: 

73 next_airing = \ 

74 user_state.media_item.next_episode_datetime 

75 if next_airing.isoweekday() == weekday: 

76 entries.append(user_state) 

77 entries.sort( 

78 key=lambda x: x.media_item.next_episode_airing_time 

79 ) 

80 entries_by_weekday.append((weekday_name, entries)) 

81 

82 return render_template( 

83 "schedule/anime_week.html", 

84 schedule=entries_by_weekday 

85 ) 

86 

87 return blueprint