Coverage for bundesliga_tippspiel/routes/chat.py: 33%

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

43 statements  

1"""LICENSE 

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

3 

4This file is part of bundesliga-tippspiel. 

5 

6bundesliga-tippspiel 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 

11bundesliga-tippspiel 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 bundesliga-tippspiel. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20from flask import Blueprint, render_template, redirect, url_for, request, flash 

21from flask_login import login_required, current_user 

22from jerrycan.base import db 

23from bundesliga_tippspiel.db.user_generated.ChatMessage import ChatMessage 

24 

25 

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

27 """ 

28 Defines the blueprint for this route 

29 :param blueprint_name: The name of the blueprint 

30 :return: The blueprint 

31 """ 

32 blueprint = Blueprint(blueprint_name, __name__) 

33 

34 @blueprint.route("/chat", methods=["GET"]) 

35 @blueprint.route("/chat/<int:page>", methods=["GET"]) 

36 @login_required 

37 def chat(page: int = 1): 

38 """ 

39 Displays the chat page 

40 :param page: The page of the chat to display 

41 :return: The response 

42 """ 

43 chat_messages = ChatMessage.query.all() 

44 chat_messages.sort(key=lambda x: x.creation_time, reverse=True) 

45 

46 page_size = 15 

47 start_comment = (page - 1) * page_size 

48 end_comment = start_comment + page_size 

49 page_messages = chat_messages[start_comment:end_comment] 

50 

51 last_page = None if page == 1 else page - 1 

52 next_page = None if len(chat_messages) <= end_comment else page + 1 

53 

54 if len(page_messages) < 1 and page != 1: 

55 return redirect(url_for("chat.chat", page=1)) 

56 else: 

57 return render_template( 

58 "chat/chat.html", 

59 messages=page_messages, 

60 last_page=last_page, 

61 next_page=next_page 

62 ) 

63 

64 @blueprint.route("/new_chat_message", methods=["POST"]) 

65 @login_required 

66 def new_chat_message(): 

67 """ 

68 Places a new chat message 

69 :return: The response 

70 """ 

71 text = request.form["text"][0:255] 

72 

73 if len(text) < 1: 

74 flash("Kommentare müssen mindestens ein Zeichen enthalten", 

75 "danger") 

76 else: 

77 parent_id = request.form.get("parent_id") 

78 message = ChatMessage( 

79 user=current_user, text=text, parent_id=parent_id 

80 ) 

81 db.session.add(message) 

82 db.session.commit() 

83 return redirect(url_for("chat.chat")) 

84 

85 @blueprint.route("/delete_chat_message/<int:message_id>", methods=["POST"]) 

86 @login_required 

87 def delete_chat_message(message_id: int): 

88 """ 

89 Deletes a chat message 

90 :param message_id: The ID of the message to delete 

91 :return: The response 

92 """ 

93 message = ChatMessage.query.get(message_id) 

94 if message is not None and message.user_id == current_user.id: 

95 message.delete() 

96 db.session.commit() 

97 flash("Kommentar wurde gelöscht", "success") 

98 else: 

99 flash("Kommentar konnte nicht gelöscht werden", "danger") 

100 return redirect(url_for("chat.chat")) 

101 

102 return blueprint