-
Notifications
You must be signed in to change notification settings - Fork 70
/
p2c3_forum.py
127 lines (94 loc) · 3.34 KB
/
p2c3_forum.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Définit les classes propres à notre forum. ;)"""
from abc import ABC
class File(ABC):
"""Fichier."""
def __init__(self, name, size):
"""Initialise le nom et la taille."""
self.name = name
self.size = size
def display(self):
"""Affiche le fichier."""
pass
class ImageFile(File):
"""Fichier image."""
def display(self):
"""Affiche l'image."""
print(f"Fichier image '{self.name}'.")
class User:
"""Utilisateur."""
def __init__(self, username, password):
"""Initialise le nom d'utilisateur et le mot de passe."""
self.username = username
self.password = password
def login(self):
"""Connecte l'utilisateur."""
print(f"L'utilisateur {self.username} est connecté.")
def post(self, thread, content, file=None):
"""Poste un message dans un fil de discussion."""
if file:
post = FilePost(self, "aujourd'hui", content, file)
else:
post = Post(user=self, time_posted="aujourd'hui", content=content)
thread.add_post(post)
return post
def make_thread(self, title, content):
"""Créé un nouveau fil de discussion."""
post = Post(self, "aujourd'hui", content)
return Thread(title, "aujourd'hui", post)
def __str__(self):
"""représentation de l'utilisateur."""
return self.username
class Moderator(User):
"""Utilisateur modérateur."""
def edit(self, post, content):
"""Modifie un message."""
post.content = content
def delete(self, thread, post):
"""Supprime un message."""
index = thread.posts.index(post)
del thread.posts[index]
class Post:
"""Message."""
def __init__(self, user, time_posted, content):
"""Initialise l'utilisateur, la date et le contenu."""
self.user = user
self.time_posted = time_posted
self.content = content
def display(self):
"""Affiche le message."""
print(f"Message posté par {self.user} le {self.time_posted}:")
print(self.content)
class FilePost(Post):
"""Message comportant un fichier."""
def __init__(self, user, time_posted, content, file):
"""Initialise le fichier."""
super().__init__(user, time_posted, content)
self.file = file
def display(self):
"""Affiche le contenu et le fichier."""
super().display()
print("pièce jointe:")
self.file.display()
class Thread:
"""Fil de discussions."""
def __init__(self, title, time_posted, post):
"""Initialise le titre, la date et les posts.
Attention ici: on commence par un seul post, celui du sujet.
Les réponses à ce post ne pourrons s'ajouter qu'ultérieurement.
En effet, on ne créé pas directement un nouveau fil avec des réponses. ;)
"""
self.title = title
self.time_posted = time_posted
self.posts = [post]
def display(self):
"""Affiche le fil de discussion."""
print("----- THREAD -----")
print(f"titre: {self.title}, date: {self.time_posted}")
print()
for post in self.posts:
post.display()
print()
print("------------------")
def add_post(self, post):
"""Ajoute un post."""
self.posts.append(post)