Coverage for bundesliga_tippspiel/db/user_generated/ChatMessage.py: 100%

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

34 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 

20import time 

21from typing import List, Optional, Dict, Any 

22from jerrycan.base import db 

23from jerrycan.db.ModelMixin import ModelMixin 

24from jerrycan.db.User import User 

25 

26 

27class ChatMessage(ModelMixin, db.Model): 

28 """ 

29 Model that describes the 'chat_messages' SQL table 

30 """ 

31 

32 def __init__(self, *args, **kwargs): 

33 """ 

34 Initializes the Model 

35 :param args: The constructor arguments 

36 :param kwargs: The constructor keyword arguments 

37 """ 

38 super().__init__(*args, **kwargs) 

39 

40 __tablename__ = "chat_messages" 

41 

42 id: int = db.Column(db.Integer, primary_key=True, autoincrement=True) 

43 user_id: int = db.Column( 

44 db.Integer, db.ForeignKey("users.id"), nullable=True 

45 ) 

46 parent_id: int = db.Column( 

47 db.Integer, db.ForeignKey("chat_messages.id"), nullable=True 

48 ) 

49 

50 text: str = db.Column(db.String(255), nullable=False) 

51 creation_time: float = db.Column( 

52 db.Float, nullable=False, default=time.time 

53 ) 

54 last_edit: float = db.Column(db.Float, nullable=False, default=time.time) 

55 edited: bool = db.Column(db.Boolean, nullable=False, default=False) 

56 deleted: bool = db.Column(db.Boolean, nullable=False, default=False) 

57 

58 user: Optional[User] = db.relationship( 

59 "User", backref=db.backref("chat_messages") 

60 ) 

61 

62 parent: "ChatMessage" = db.relationship( 

63 "ChatMessage", 

64 back_populates="children", 

65 remote_side=[id], 

66 uselist=False 

67 ) 

68 children: List["ChatMessage"] = db.relationship( 

69 "ChatMessage", back_populates="parent", uselist=True 

70 ) 

71 

72 def get_text(self) -> Optional[str]: 

73 """ 

74 :return: The text of the chat message if the user still exists and the 

75 message has not been deleted 

76 """ 

77 if self.deleted or self.user is None: 

78 return None 

79 else: 

80 return self.text 

81 

82 def edit(self, new_text: str): 

83 """ 

84 Edits the message 

85 :param new_text: The new message text 

86 :return: None 

87 """ 

88 self.text = new_text 

89 self.edited = True 

90 self.last_edit = time.time() 

91 

92 def delete(self): 

93 """ 

94 Marks the message as deleted 

95 :return: None 

96 """ 

97 self.text = "" 

98 self.deleted = True 

99 

100 def __json__( 

101 self, 

102 include_children: bool = False, 

103 ignore_keys: Optional[List[str]] = None 

104 ) -> Dict[str, Any]: 

105 """ 

106 Makes sure to include children and parents 

107 :param include_children: Whether or not to include child objects 

108 :param ignore_keys: Which keys to ignore 

109 :return: The JSON data 

110 """ 

111 data = super().__json__(include_children, ignore_keys) 

112 # TODO Add child and parent relations 

113 return data