forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer.py
37 lines (31 loc) · 1.2 KB
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from datetime import datetime
from votable import Votable
from commentable import Commentable
from vote import Vote
class Answer(Votable, Commentable):
def __init__(self, author, question, content):
self.id = id(self)
self.author = author
self.question = question
self.content = content
self.creation_date = datetime.now()
self.votes = []
self.comments = []
self.is_accepted = False
def vote(self, user, value):
if value not in [-1, 1]:
raise ValueError("Vote value must be either 1 or -1")
self.votes = [v for v in self.votes if v.user != user]
self.votes.append(Vote(user, value))
self.author.update_reputation(value * 10) # +10 for upvote, -10 for downvote
def get_vote_count(self) -> int:
return sum(v.value for v in self.votes)
def add_comment(self, comment):
self.comments.append(comment)
def get_comments(self):
return self.comments.copy()
def accept(self):
if self.is_accepted:
raise ValueError("This answer is already accepted")
self.is_accepted = True
self.author.update_reputation(15) # +15 reputation for accepted answer