Coverage for otaku_info/routes/updates.py: 26%

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

44 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 flask import request, render_template, redirect, url_for, flash 

21from flask.blueprints import Blueprint 

22from flask_login import login_required, current_user 

23from otaku_info.enums import ListService, MediaType, MediaSubType 

24from otaku_info.db.MediaList import MediaList 

25from otaku_info.wrappers.UpdateWrapper import UpdateWrapper 

26 

27 

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

29 """ 

30 Defines the blueprint for this route 

31 :param blueprint_name: The name of the blueprint 

32 :return: The blueprint 

33 """ 

34 blueprint = Blueprint(blueprint_name, __name__) 

35 

36 @blueprint.route("/updates", methods=["POST"]) 

37 @login_required 

38 def redirect_updates(): 

39 """ 

40 Redirects a POST requests to the appropriate GET request for 

41 the /updates route 

42 :return: The response 

43 """ 

44 service, media_type, list_name = \ 

45 request.form["list_ident"].split(":", 2) 

46 mincount = request.form.get("mincount", "0") 

47 include_complete = request.form.get("include_complete", "off") == "on" 

48 filter_subtype = request.form.get("filter_subtype") 

49 

50 get_url = url_for( 

51 "updates.show_updates", 

52 service=service, 

53 media_type=media_type, 

54 list_name=list_name, 

55 mincount=mincount, 

56 include_complete=1 if include_complete else 0, 

57 filter_subtype=filter_subtype, 

58 display_mode=request.form.get("display_mode", "grid") 

59 ) 

60 return redirect(get_url) 

61 

62 @blueprint.route("/updates", methods=["GET"]) 

63 @login_required 

64 def show_updates(): 

65 """ 

66 Shows the user's manga updates for a specified service and list 

67 :return: The response 

68 """ 

69 service_name = request.args.get("service") 

70 media_type_name = request.args.get("media_type") 

71 list_name = request.args.get("list_name") 

72 mincount = int(request.args.get("mincount", "0")) 

73 include_complete = request.args.get("include_complete", "0") == "1" 

74 subtype_name = request.args.get("filter_subtype") 

75 

76 if service_name is None \ 

77 or list_name is None \ 

78 or media_type_name is None: 

79 media_lists = [ 

80 x for x in MediaList.query.filter_by(user=current_user) 

81 ] 

82 media_lists.sort(key=lambda x: x.name) 

83 media_lists.sort(key=lambda x: x.media_type.value) 

84 media_lists.sort(key=lambda x: x.service.value) 

85 return render_template( 

86 "updates/updates.html", 

87 media_lists=[ 

88 ( 

89 f"{x.service.value}:{x.media_type.value}:{x.name}", 

90 f"{x.service.value.title()}:" 

91 f"{x.media_type.value.title()}:{x.name.title()}" 

92 

93 ) 

94 for x in media_lists 

95 ], 

96 subtypes=[(x.value, x.value.title()) for x in MediaSubType] 

97 ) 

98 else: 

99 try: 

100 service = ListService(service_name) 

101 media_type = MediaType(media_type_name) 

102 if subtype_name is None or subtype_name == "": 

103 subtype = None 

104 else: 

105 subtype = MediaSubType(subtype_name) 

106 except ValueError: 

107 flash("Invalid configuration", "danger") 

108 return redirect(url_for("updates.show_updates")) 

109 

110 updates = UpdateWrapper.from_db( 

111 current_user, 

112 list_name, 

113 service, 

114 media_type, 

115 subtype, 

116 mincount, 

117 include_complete 

118 ) 

119 return render_template( 

120 "updates/updates.html", 

121 updates=updates, 

122 list_name=list_name, 

123 service=service, 

124 media_type=media_type, 

125 display_mode=request.args.get("display_mode", "grid") 

126 ) 

127 

128 return blueprint