Coverage for otaku_info/routes/ln.py: 19%

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

48 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 

20from datetime import datetime 

21from flask import request, render_template, redirect, url_for 

22from flask.blueprints import Blueprint 

23from jerrycan.base import db 

24 

25from otaku_info.db import MediaItem 

26from otaku_info.db.LnRelease import LnRelease 

27from otaku_info.utils.dates import MONTHS, map_month_name_to_month_number, \ 

28 map_month_number_to_month_name 

29 

30 

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

32 """ 

33 Defines the blueprint for this route 

34 :param blueprint_name: The name of the blueprint 

35 :return: The blueprint 

36 """ 

37 blueprint = Blueprint(blueprint_name, __name__) 

38 

39 @blueprint.route("/ln/releases", methods=["GET"]) 

40 def ln_releases(): 

41 """ 

42 Displays light novel releases 

43 :return: The response 

44 """ 

45 try: 

46 year = int(request.args.get("year")) 

47 except (TypeError, ValueError): 

48 year = None 

49 try: 

50 month = int(request.args["month"]) 

51 except KeyError: 

52 month = None 

53 except (TypeError, ValueError): 

54 month_string = request.args["month"] 

55 if month_string.lower() == "all": 

56 month = None 

57 else: 

58 month = map_month_name_to_month_number(month_string) 

59 

60 now = datetime.utcnow() 

61 if not (year is not None and month is None): 

62 if year is None: 

63 year = now.year 

64 if month is None: 

65 month = now.month 

66 

67 all_releases = LnRelease.query.options( 

68 db.joinedload(LnRelease.media_item) 

69 .subqueryload(MediaItem.id_mappings) 

70 ).all() 

71 years = list(set([x.release_date.year for x in all_releases])) 

72 years.sort() 

73 

74 releases = [ 

75 x for x in all_releases 

76 if x.release_date.year == year 

77 ] 

78 if month is not None: 

79 releases = [x for x in releases if x.release_date.month == month] 

80 releases.sort(key=lambda x: x.release_date) 

81 

82 if month is None: 

83 month_name = "all" 

84 else: 

85 month_name = map_month_number_to_month_name(month) 

86 

87 return render_template( 

88 "ln/ln_releases.html", 

89 releases=releases, 

90 years=[(x, x) for x in years], 

91 months=[(x, x.title()) for x in MONTHS + ["all"]], 

92 selected_year=year, 

93 selected_month=month_name 

94 ) 

95 

96 @blueprint.route("/ln/releases", methods=["POST"]) 

97 def ln_releases_form(): 

98 """ 

99 Handles form requests and forwards it to the appropriate GET URL. 

100 """ 

101 year = request.form.get("year") 

102 month = request.form.get("month") 

103 get_url = url_for("ln.ln_releases") + f"?year={year}&month={month}" 

104 return redirect(get_url) 

105 

106 return blueprint