Skip to content

Commit c773030

Browse files
committed
works better
1 parent 7268358 commit c773030

8 files changed

+97
-42
lines changed

__pycache__/app.cpython-37.pyc

218 Bytes
Binary file not shown.

app.py

+90-37
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
# List of days of the week
4343
daysoftheweek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
4444

45+
# course = [people emails]
46+
'''duplicates = {
47+
48+
}'''
49+
4550
# dictionary that serves as a converter between times and indices
4651
time_to_index = {
4752
"12:00am": 0,
@@ -316,13 +321,32 @@ def prefs():
316321
return render_template("prefs.html", locations=locations, daysoftheweek = daysoftheweek, time_to_index = time_to_index)
317322

318323
def matchemail(people, locations, timeblock, day, uniqueCourse, matched):
324+
#https://www.programiz.com/python-programming/methods/string/count
325+
places = {
326+
"Cabot Library": 0,
327+
"Dorm Room": 0,
328+
"Lamont Library": 0,
329+
"Smith Center": 0,
330+
"Widener Library": 0
331+
}
332+
for key in places:
333+
places[key] = locations.count(key)
334+
locations = places
319335
#people as a list
320336
#locations as a dictionary
321337
#uniquecourse as a integer valiue
322338
#matched as a boolean
323339
# day is one day because we are only matching for one day per week
324340
# configuration of mail
325-
341+
'''print("UHH", duplicates)'''
342+
#duplicates[uniqueCourse] = people
343+
#noduplicates = True
344+
'''for person in people:
345+
if person in duplicates[uniqueCourse]:
346+
print("HMMMMMMMMMMMMMMMMMMMM")
347+
noduplicates = False
348+
break
349+
if noduplicates: '''
326350
print("LOOOOOOOOOOOOK")
327351
print(people)
328352
count = len(people)
@@ -381,14 +405,14 @@ def matchemail(people, locations, timeblock, day, uniqueCourse, matched):
381405
t = t + timetext
382406
t = t+ locationtext
383407
msg.body(t)
384-
with app.app_context():
385-
mail.send(msg)
408+
#with app.app_context():
409+
#mail.send(msg)
386410
return 'Sent'
387411

388412
if matched:
389413
locationtext = ""
390414
for entry in locations:
391-
locationtext = locationtext + (locations[entry]/count) + " of people prefer " + entry + ". "
415+
locationtext = locationtext + str(locations[entry]/count) + " of people prefer " + entry + ". "
392416
names = ""
393417
for person in people:
394418
if names == "":
@@ -410,10 +434,14 @@ def matchemail(people, locations, timeblock, day, uniqueCourse, matched):
410434
t = t.replace('course', uniqueCourse)
411435
t = t.replace('day', day)
412436
timestring = ""
413-
for x in timeblock:
414-
timestring += x +","
415-
print(timeblock)
416-
timestring = timestring[0: len(timestring)-1]
437+
if len(timeblock) == 2 and timeblock[0] == timeblock[1]:
438+
timestring = timeblock[0]
439+
else:
440+
timestring = timeblock[0]+"-"+timeblock[len(timeblock)-1]
441+
442+
#for x in timeblock:
443+
# timestring += x +","
444+
#timestring = timestring[0: len(timestring)-1]
417445
t = t.replace("time", timestring)
418446
t = t + locationtext
419447
print(names, uniqueCourse, day, timestring)
@@ -422,7 +450,7 @@ def matchemail(people, locations, timeblock, day, uniqueCourse, matched):
422450
with app.app_context():
423451
mail.send(msg)
424452
return 'Sent'
425-
#Code for generating and confirming token taken from https://realpython.com/handling-email-confirmation-in-flask/#generate-confirmation-token
453+
# Code for generating and confirming token taken from https://realpython.com/handling-email-confirmation-in-flask/#generate-confirmation-token
426454
def generate_confirmation_token(email):
427455
serializer = URLSafeTimedSerializer('SECRET_KEY')
428456
return serializer.dumps(email, salt='SECURITY_PASSWORD_SALT')
@@ -472,10 +500,11 @@ def verify(email, firstname):
472500

473501
def match():
474502
# Creates copy of preferences table called prefs to save preferences information
475-
# db.execute("SELECT * INTO prefs FROM preferences;")
476-
# db.execute("ALTER TABLE prefs ADD matched bit")
477-
#db.execute("UPDATE prefs SET matched = ?", 0)
503+
# db.execute("SELECT * INTO prefs FROM preferences;")
504+
# db.execute("ALTER TABLE prefs ADD matched bit")
505+
# db.execute("UPDATE prefs SET matched = ?", 0)
478506
# Runs timematch on every unique course in the prefs table
507+
# duplicates = {}
479508
uniqueCourses = db.execute("SELECT DISTINCT course FROM prefs;")
480509
for uniqueCourse in uniqueCourses:
481510
for day in daysoftheweek:
@@ -529,7 +558,7 @@ def timematch(uniqueCourse, day):
529558
stackedTimelines[time_to_index[time]][1]= time
530559

531560
stackedTimelines = sorted(stackedTimelines, key=lambda x: x[0], reverse = True)
532-
561+
533562
# now stackedtimelines has the number of people who want a specific course at each time block
534563
# https://careerkarma.com/blog/python-sort-a-dictionary-by-value/
535564
#sortedStackedTimelines = sorted(stackedTimelines.items(), key=lambda x: x[1], reverse=True)
@@ -539,9 +568,11 @@ def timematch(uniqueCourse, day):
539568
# stackedTimelinesValues = stackedTimelines.values()
540569

541570
#https://www.kite.com/python/answers/how-to-sort-a-multidimensional-list-by-column-in-python
542-
for key in range(len(stackedTimelines)):
571+
key = 0
572+
duplicates = []
573+
while key < len(stackedTimelines):
543574
max_value = stackedTimelines[key][0]
544-
if max_value == 0:
575+
if max_value == 0 or max_value == 1:
545576
break
546577
# finds 30 minute blocks with max people
547578
# https://www.programiz.com/python-programming/methods/list/index
@@ -560,22 +591,23 @@ def timematch(uniqueCourse, day):
560591
am_timeblock.append(max_time)
561592
else:
562593
pm_timeblock.append(max_time)
563-
for left in range(time_to_index[max_time]-1, -1, -1):
564-
key2 = index_to_time[left]
565-
peopledict2 = db.execute("SELECT email FROM prefs WHERE course = ? AND times LIKE ? AND day = ?;", uniqueCourse, "%"+key2+"%", day)
566-
people2 = []
567-
for x in peopledict2:
568-
people2.append(x["email"])
594+
#for left in range(time_to_index[max_time]-1, -1, -1):
595+
# key2 = index_to_time[left]
596+
# peopledict2 = db.execute("SELECT email FROM prefs WHERE course = ? AND times LIKE ? AND day = ?;", uniqueCourse, "%"+key2+"%", day)
597+
# people2 = []
598+
#for x in peopledict2:
599+
# people2.append(x["email"])
569600
#https://thispointer.com/python-check-if-a-list-contains-all-the-elements-of-another-list/#:~:text=Check%20if%20list1%20contains%20all%20elements%20of%20list2%20using%20all()&text=Python%20all()%20function%20checks,if%20element%20exists%20in%20list1.
570-
contained = all(elem in people2 for elem in people)
571-
if not contained:
572-
break
573-
else:
574-
if key[len(key2)-2] == "am":
575-
am_timeblock.append(key2)
576-
else:
577-
pm_timeblock.append(key2)
601+
# contained = all(elem in people2 for elem in people)
602+
# if not contained:
603+
# break
604+
# else:
605+
# if key2[len(key2)-2] == "am":
606+
# am_timeblock.append(key2)
607+
# else:
608+
# pm_timeblock.append(key2)
578609
max_index = time_to_index[max_time]
610+
count = 0
579611
for right in range(max_index, len(stackedTimelines)):
580612
key2 = index_to_time[right]
581613
peopledict2 = db.execute("SELECT email FROM prefs WHERE course = ? AND times LIKE ? AND day = ?;", uniqueCourse, "%"+key2+"%", day)
@@ -587,14 +619,29 @@ def timematch(uniqueCourse, day):
587619
if not contained:
588620
break
589621
else:
622+
count += 1
590623
if key2[len(key2)-2] == "am":
591624
am_timeblock.append(key2)
592625
else:
593626
pm_timeblock.append(key2)
594627
am_timeblock.sort()
595628
pm_timeblock.sort()
596629
timeblock = am_timeblock+pm_timeblock
597-
grouped_people = grouper(uniqueCourse, timeblock, people, day)
630+
#contained = any(elem in people for elem in duplicates)
631+
for person in people:
632+
if person in duplicates:
633+
break
634+
if not person in duplicates:
635+
636+
#if not contained:
637+
grouper(uniqueCourse, timeblock, people, day)
638+
for r in people:
639+
duplicates.append(r)
640+
print("DUPLICATES: ", duplicates)
641+
print("COUNT: ", count)
642+
key += 1
643+
key += count
644+
print("KEY: ", key)
598645
# Handle left over people
599646

600647

@@ -603,12 +650,18 @@ def timematch(uniqueCourse, day):
603650
def grouper(uniqueCourse, timeblock, emails, day):
604651
timestring = ""
605652
locationstring = ""
606-
for time in timeblock:
653+
for i in range(1, len(timeblock)):
654+
time = timeblock[i]
607655
timestring += time + ","
608-
course_entries = db.execute("SELECT * FROM prefs WHERE course = ? and times LIKE ? AND day = ?", uniqueCourse, "%"+timestring+"%", day)
609-
for course_entry in course_entries:
610-
locationstring += course_entry["locations"] + ","
656+
timestring = timestring[0: len(timestring)-1]
657+
course_entries = db.execute("SELECT * FROM prefs WHERE course = ? AND times LIKE ? AND day = ?", uniqueCourse, "%"+timestring+"%", day)
658+
course_locations = db.execute("SELECT locations FROM prefs WHERE course = ? AND times LIKE ? AND day = ?", uniqueCourse, "%"+timestring+"%", day)
659+
locationstring = ""
660+
for x in course_locations:
661+
locationstring+=x["locations"]
611662
number_of_people = len(course_entries)
663+
#if number_of_people == 1:
664+
# matchemail(emails, locationstring, timeblock, day, uniqueCourse, False)
612665
if number_of_people <= 3:
613666
matchemail(emails, locationstring, timeblock, day, uniqueCourse, True)
614667
elif number_of_people <=6 and number_of_people >=4:
@@ -626,10 +679,10 @@ def grouper(uniqueCourse, timeblock, emails, day):
626679
matchemail(emails[2:4], locationstring, timeblock, day, uniqueCourse, True)
627680
elif number_of_people == 5:
628681
matchemail(emails[0:2], locationstring, timeblock, day, uniqueCourse, True)
629-
matchemail(emails[3:5], locationstring, timeblock, day, uniqueCourse, True)
682+
matchemail(emails[2:5], locationstring, timeblock, day, uniqueCourse, True)
630683
else:
631684
matchemail(emails[0:3], locationstring, timeblock, day, uniqueCourse, True)
632-
matchemail(emails[4:6], locationstring, timeblock, day, uniqueCourse, True)
685+
matchemail(emails[3:6], locationstring, timeblock, day, uniqueCourse, True)
633686
else:
634687
matchemail(emails, locationstring, timeblock, day, uniqueCourse, True)
635688
db.execute("UPDATE prefs SET matched = 1 WHERE course = ? AND times LIKE ? AND day = ?", uniqueCourse, "%"+timestring+"%", day)
@@ -737,7 +790,7 @@ def grouper(uniqueCourse, timeblock, emails, day):
737790
def sizeCount(course_entries, size):
738791
count = 0
739792
for course_entry in course_entries:
740-
if course_entry["size"] == size:
793+
if course_entry["size"] == size:
741794
count += 1
742795
return count
743796

56 Bytes
Binary file not shown.
-59 Bytes
Binary file not shown.

templates/courses.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ <h2>Course ID {{ course }}</h2>
2222
<button id="add1">Add Resource</button>
2323
<div id="addResource" style="display:none">
2424
<form action="/courses" method="post">
25+
<input id="resourceCourse" name="resourceCourse" type="hidden" value={{course}}>
2526
<label for="resourceLink">Link to resource:</label>
2627
<input id="resourceLink" name="resourceLink" placeholder="Put Link Here" type="url">
2728
<label for="resourceTitle">Title of resource:</label>
@@ -38,7 +39,7 @@ <h2>Course ID {{ course }}</h2>
3839
document.getElementById('add1').style.display = 'none'
3940
});
4041
document.querySelector('#add2').addEventListener('click', function(){
41-
db.execute("INSERT INTO resources VALUES (?, ?, ?)", course, resourceLink, resourceTitle)
42+
location.href = "./courses.html";
4243
});
4344
</script>
4445
{% endfor %}

templates/prefs.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<label for="size">Preferred Group Size:</label>
3131
<select name="size" id="size">
3232
<option disabled selected>Group Size</option>
33-
<option value="small">Small (2-3)</option>
34-
<option value="medium">Medium (4-6)</option>
35-
<option value="large">Large (7+)</option>
33+
<option value="s">Small (2-3)</option>
34+
<option value="m">Medium (4-6)</option>
35+
<option value="l">Large (7+)</option>
3636
</select>
3737
{% endblock %}

test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@
6363
matrix.append(row)
6464
stackedTimelines =[[0, 0], [0,0]]
6565
stackedTimelines = stackedTimelines + stackedTimelines
66+
print(stackedTimelines)
6667
for time in timelist:
6768
index = time_to_index[time]
6869
print(index)
6970
# stackedTimelines[index][0] += 1
7071
# stackedTimelines[index][1]=time
7172
stackedTimelines[0][0] +=1
72-
print(stackedTimelines)
73+
print(len(stackedTimelines))

users.db

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)