Coverage for otaku_info/routes/media.py: 54%

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

20 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 render_template, abort 

21from flask.blueprints import Blueprint 

22from flask_login import current_user 

23from jerrycan.base import db 

24 

25from otaku_info.db import MediaUserState 

26from otaku_info.db.MediaItem import MediaItem 

27from otaku_info.db.MediaIdMapping import MediaIdMapping 

28from otaku_info.enums import ListService, MediaType 

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( 

40 f"/media/<service>/<media_type>/<service_id>", 

41 methods=["GET"] 

42 ) 

43 def media(service: str, media_type: str, service_id: str): 

44 """ 

45 Displays information on a media item 

46 :param service: The service of the media item 

47 :param media_type: The media type of the item 

48 :param service_id: The service ID of the item 

49 :return: The page displaying information on the media item 

50 """ 

51 media_item: MediaItem = MediaItem.query.filter_by( 

52 service=ListService(service), 

53 media_type=MediaType(media_type), 

54 service_id=service_id 

55 ).first() 

56 if media_item is None: 

57 abort(404) 

58 

59 user_state = None 

60 if current_user.is_authenticated: 

61 user_state = MediaUserState.query.filter_by( 

62 service=ListService(service), 

63 media_type=MediaType(media_type), 

64 service_id=service_id, 

65 user_id=current_user.id 

66 ).first() 

67 

68 return render_template( 

69 "media/media.html", 

70 media_item=media_item, 

71 user_state=user_state 

72 ) 

73 

74 return blueprint