This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
383 lines (342 loc) · 12.4 KB
/
project.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""MM Assigner: App that matches mentors and mentees"""
import os
import random
import streamlit as st
from pycountry import languages as LANGUAGES
from tinydb import TinyDB
class MMDatabase:
"""Class that interacts with the database"""
def __init__(self):
"""Function that initiates the database and the two tables"""
self.db = TinyDB(".mm_assigner_db.json") # pylint: disable=invalid-name
self.mentors = self.db.table("mentors")
self.mentees = self.db.table("mentees")
def add_mentee(
self,
role,
name,
email,
generally_preferred_language,
prefers_preferred_language,
): # pylint: disable=too-many-arguments
"""Function that adds a mentee to the database"""
self.mentees.insert(
{
"role": role,
"name": name,
"email": email,
"generally_preferred_language": generally_preferred_language,
"prefers_preferred_language": prefers_preferred_language,
}
)
def add_mentor(
self,
role,
name,
email,
generally_preferred_language,
prefers_preferred_language,
): # pylint: disable=too-many-arguments
"""Function that adds a mentor to the database"""
self.mentors.insert(
{
"role": role,
"name": name,
"email": email,
"generally_preferred_language": generally_preferred_language,
"prefers_preferred_language": prefers_preferred_language,
}
)
# Initialise the database
mma_db = MMDatabase()
# Create a session_state variable to hold the combinations
if "combinations_assigned" not in st.session_state:
st.session_state.combinations_assigned = []
# Create a session_state variable to hold the non-assigned people
if "combinations_non_assigned" not in st.session_state:
st.session_state.combinations_non_assigned = []
# Create a session_state variable to hold the state of the generation button
if "generation_button_disabled" not in st.session_state:
st.session_state.generation_button_disabled = True
def main():
"""Main function"""
# Set the page configuration
set_page_config()
# Define the general layout of the app
col_1, col_2, col_3 = st.columns(3)
with col_1:
col_1_content()
with col_2:
col_2_content()
with col_3:
col_3_content()
def set_page_config():
"""Sets the main page configuration"""
st.set_page_config(
page_title="MM Assigner", layout="wide", initial_sidebar_state="collapsed"
)
def col_1_content():
"""Defines the content of the first column"""
st.write("# MM Assigner")
st.write("This app allows you to assigns mentors to mentees.")
add_person()
def add_person():
"""Adds a new person"""
# Format the languages list
languages_list = [language.name for language in list(LANGUAGES)]
# Generate the form
with st.form("add_person", clear_on_submit=True):
st.write("### Add a person")
role = st.selectbox("Role", ["Mentor", "Mentee"])
name = st.text_input("Full name")
email = st.text_input("Email")
generally_preferred_language = st.selectbox(
"Generally preferred language", languages_list
)
prefers_preferred_language = st.checkbox(
label="I would rather use my preferred language"
)
person_submitted = st.form_submit_button("Add")
# Generate hook for person submitted event
if person_submitted:
if role == "Mentor":
mma_db.add_mentor(
role,
name,
email,
generally_preferred_language,
prefers_preferred_language,
)
else:
mma_db.add_mentee(
role,
name,
email,
generally_preferred_language,
prefers_preferred_language,
)
def col_2_content():
"""Defines the content of the second column"""
# Only show the assignment button when there are at least two people,
# one of each role
assigner_button_disabled = True
if len(mma_db.mentees.all()) > 0 and len(mma_db.mentors.all()) > 0:
assigner_button_disabled = False
assigner_button_clicked = st.button(
"Assign mentors & mentees", disabled=assigner_button_disabled
)
# Generate hook for assigner button clicked event
if assigner_button_clicked:
assign_mentors_mentees()
# Enable the export button in the third column
st.session_state.generation_button_disabled = False
# Display the list of people entered
st.write("## Mentors")
st.dataframe(
[
{
"Name": mentor["name"],
"E-Mail": mentor["email"],
"Preferred Language": mentor["generally_preferred_language"],
}
for mentor in mma_db.mentors.all()
]
)
st.write("## Mentees")
st.dataframe(
[
{
"Name": mentee["name"],
"E-Mail": mentee["email"],
"Preferred Language": mentee["generally_preferred_language"],
}
for mentee in mma_db.mentees.all()
]
)
def col_3_content():
"""Defines the content of the third column"""
# Show the generation button
generation_button_clicked = st.button(
"Generate notifications", disabled=st.session_state.generation_button_disabled
)
# Generate hook for generation button clicked event
if generation_button_clicked:
notify_participants()
# Display the result of the assignment
st.write("## Generated combinations")
st.dataframe(
[
{
"Mentor": mentor["name"],
"Mentee": mentee["name"],
}
for mentor, mentee in st.session_state.combinations_assigned
]
)
st.write("## Non-assigned participants")
st.dataframe(
[
{
"Name": participant["name"],
}
for participant in st.session_state.combinations_non_assigned
]
)
def filter_mentees(people_list, language):
"""Filters the mentees list depending on the chosen language"""
return [
person
for person in people_list
if person["generally_preferred_language"] == language
]
def assign_mentors_mentees():
"""Assigns mentors to mentees"""
# Get the list of mentors
mentors_list = mma_db.mentors.all()
# Get the list of mentees
mentees_list = mma_db.mentees.all()
# Reset the combinations variables
st.session_state.combinations_assigned = []
st.session_state.combinations_non_assigned = []
# Repeat indefinitely:
while True:
# If the list of mentors is empty or the list of mentees is empty:
if not mentors_list or not mentees_list:
# Leave the loop
break
# Randomly select a mentor (take it out of the mentors list)
mentor = random.choice(mentors_list)
mentors_list.remove(mentor)
# Create a local (per loop) copy of the mentees list
local_mentees_list = mentees_list.copy()
# If the mentor prefers a certain language:
if mentor["prefers_preferred_language"]:
# Filter the mentees list depending on the language in question
filtered_mentees_list = [
mentee
for mentee in local_mentees_list
if mentee["generally_preferred_language"]
== mentor["generally_preferred_language"]
]
# If the result of the filter is not empty:
if filtered_mentees_list:
# Assign the filtered list to the local copy
local_mentees_list = filtered_mentees_list
# Get a random choice from the local copy of the mentees list (take it
# out of the mentees list)
mentee = random.choice(local_mentees_list)
mentees_list.remove(mentee)
# Add the combination to the combinations variable
st.session_state.combinations_assigned.append((mentor, mentee))
# Add all the remaining people to the non-assigned people list
st.session_state.combinations_non_assigned = mentors_list + mentees_list
def generate_participant_notification(
participant_name,
participant_email,
participant_assigned,
other_participant_name,
other_participant_email,
other_participant_role,
): # pylint: disable=too-many-arguments
"""Generates the appropriate notification for every participant"""
# Generate the message subject
message_subject = ""
if participant_assigned:
message_subject = f"🎉 Congrats {participant_name}, you were assigned a {other_participant_role.lower()} 🎊" # pylint: disable=line-too-long
else:
message_subject = (
f"🔥 Hey {participant_name}, we've got news about the MM program!"
)
# Generate the message specific content
specific_message_content = ""
if participant_assigned:
specific_message_content = f"""
You were assigned {other_participant_name} as a {other_participant_role.lower()}.
You can contact them on the following e-mail address: {other_participant_email}.
"""
else:
specific_message_content = f"""
Due to a shortage of participants, you were not assigned a {other_participant_role.lower()}.
You can still however join one of your peers/friends with their {other_participant_role.lower()} if you want.
"""
# Generate the output
return f"""
+------------------+
| PARTICIPANT NAME |
+------------------+
{participant_name}
+-------------------+
| PARTICIPANT EMAIL |
+-------------------+
{participant_email}
+-----------------+
| MESSAGE SUBJECT |
+-----------------+
{message_subject}
+-----------------+
| MESSAGE CONTENT |
+-----------------+
Dear {participant_name},
{specific_message_content}
Don't hesitate to contact us if you have any question.
Kind regards,
MM Project Team
"""
def save_notification_to_file(file_name, notification):
"""Saves the specified notification to the specified file"""
# Check the output directory
output_directory = "Exported notifications"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# Save the data to the file
with open(
f"{output_directory}/{file_name}.txt", "w", encoding="utf-8"
) as output_file:
output_file.write(notification)
def notify_participants():
"""Generates the emails to send to the different participants"""
# Generate and save a notification for every participant
# in the list of combinations
for mentor, mentee in st.session_state.combinations_assigned:
# Save the mentor's notification
save_notification_to_file(
file_name=mentor["name"],
notification=generate_participant_notification(
participant_name=mentor["name"],
participant_email=mentor["email"],
participant_assigned=True,
other_participant_name=mentee["name"],
other_participant_email=mentee["email"],
other_participant_role=mentee["role"],
),
)
# Save the mentee's notification
save_notification_to_file(
file_name=mentee["name"],
notification=generate_participant_notification(
participant_name=mentee["name"],
participant_email=mentee["email"],
participant_assigned=True,
other_participant_name=mentor["name"],
other_participant_email=mentor["email"],
other_participant_role=mentor["role"],
),
)
# Generate and save a notification for every participant
# in the list of remaining people
for participant in st.session_state.combinations_non_assigned:
save_notification_to_file(
participant["name"],
generate_participant_notification(
participant_name=participant["name"],
participant_email=participant["email"],
participant_assigned=False,
other_participant_name="",
other_participant_email="",
other_participant_role="Mentor"
if participant["role"] == "Mentee"
else "Mentee",
),
)
if __name__ == "__main__":
main()