forked from IlanSimchon/SocialNetworkAssignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.py
91 lines (74 loc) · 2.76 KB
/
User.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from Post import TextPost, ImagePost, SalePost
class Notification:
def __init__(self, message, notification_type):
self.message = message
self.notification_type = notification_type
# User class behaves as the subject in the observer design pattern.
class User:
def __init__(self, username, password):
if not (4 <= len(password) <= 8):
raise ValueError("Password must be between 4 and 8 characters.")
self.username = username
self.password = password
self.status = True
self.followers = [] # represents the observers
self.following = []
self.posts = []
self.notifications = []
def __str__(self):
return f"User name: {self.username}, Number of posts: {len(self.posts)}, Number of followers: {len(self.followers) or ' '}"
def disconnect(self):
self.status = False
def connect(self):
self.status = True
def notifications_manager(self):
pass
def notify(self, message):
self.notifications.append(message)
def follow(self, user):
if user not in self.following:
self.following.append(user)
user.followers.append(self)
print(f"{self.username} started following {user.username}")
def unfollow(self, user):
if user in self.following:
if self.status:
self.following.remove(user)
user.followers.remove(self)
print(f"{self.username} unfollowed {user.username}")
else:
print("User is not connected.")
def publish_post(self, post_type, *args):
post = None
if not self.status:
print("User is not connected. Cannot publish post.")
return None
if post_type == "Text":
post = TextPost(*args)
elif post_type == "Image":
print(f"{self.username} posted a picture")
print()
post = ImagePost(*args)
elif post_type == "Sale":
post = SalePost(*args)
if post:
post._author = self
self.posts.append(post)
self.update(post)
return post
else:
print(f"Invalid post type: {post_type}")
return None
def update(self, post):
for follower in self.followers:
follower.notify(f"{self.username} has a new post")
if post.post_type != "Image":
post.display()
if post.post_type == "Text":
print()
def print_notifications(self):
if self.status:
print(f"{self.username}'s notifications:")
for notification in self.notifications:
if notification is not None:
print(f"{notification}")