-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2.py
130 lines (109 loc) · 4.59 KB
/
v2.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
import shutil
from flask import Flask, render_template, request, send_file
from werkzeug import secure_filename
import os, csv, random, zipfile
import pandas as pd
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = {'xlsx', 'xls'}
def allowed_file(filename):
print(filename.rsplit('.', 1)[1])
allowed = '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
return allowed
def write_to_csv(rounds, round_num, group_name):
file_path = "./downloads/" + group_name + "_" + round_num + ".csv"
with open(file_path, 'w') as csv_file:
fieldnames = ["Room Number", "Team 1", "vs", "Team 2"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for i in range(len(rounds)):
writer.writerow({"Room Number": rounds[i][0], "Team 1": " ".join([rounds[i][1]]), "vs": "vs",
"Team 2": " ".join([rounds[i][2]])})
def make_round(teams_a, teams_b, already_matched):
random.shuffle(teams_a)
random.shuffle(teams_b)
teams_b_used = list()
rounds = list()
room_number = 1
for i in teams_a:
for j in teams_b:
if j not in teams_b_used and j not in already_matched[i]:
if j != "No Team, Bye":
rounds.append([room_number, i, j])
room_number += 1
else:
rounds.append(["No room", i, j])
teams_b_used.append(j)
break
if len(rounds) == len(teams_a):
for round in rounds:
already_matched[round[1]].append(round[2])
return rounds
else:
return make_round(teams_a, teams_b, already_matched) #if it's stupid but it works, it ain't stupid
def do_stuff(df, num_rounds, group_name):
df.drop([df.columns[0]], axis='columns', inplace=True)
df.drop([0, 1, 2, 3], inplace=True)
df = df[df[3] == df[3]]
team_codes = df[2].tolist()
team_names = df[3].tolist()
teams = list()
for i in range(len(team_names)):
try:
if team_names[i].strip() != "":
teams.append(team_codes[i].strip() + " " + team_names[i].strip())
except:
error_msg = "Exception has occured trying to strip " + str(team_names[i]) + " or " + str(team_codes[i])
print(error_msg)
teams = list(set(teams))
random.shuffle(teams)
if len(teams) % 2 != 0:
teams.append("No Team, Bye")
half_teams = int(len(teams) / 2)
teams_a = teams[:half_teams]
teams_b = teams[half_teams:]
already_matched = dict()
for i in teams_a:
already_matched[i] = list()
for i in range(num_rounds):
rounds = make_round(teams_a, teams_b, already_matched)
write_to_csv(rounds, "round_" + str(i + 1), group_name)
def generate_rounds(filename, num_rounds):
if os.path.exists("./downloads"):
shutil.rmtree("./downloads")
os.mkdir("./downloads")
filepath = './uploads/' + str(filename)
xls = pd.ExcelFile(filepath)
df1 = pd.read_excel(xls, 'Lions Registration', header=None)
df2 = pd.read_excel(xls, 'Cubs Registration', header=None)
do_stuff(df1, num_rounds, "lions")
do_stuff(df2, num_rounds, "cubs")
@app.route('/')
def login():
print("hello world")
return render_template('index.html')
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
num_rounds = int(request.form["num_rounds"])
if num_rounds < 1:
raise Exception("invalid number of rounds entered")
print(num_rounds)
if f and allowed_file(f.filename):
curpath = os.path.abspath(os.curdir)
print("Current path is: %s", (curpath))
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
generate_rounds(filename, num_rounds)
zipf = zipfile.ZipFile('Rounds.zip', 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk('./downloads/'):
for file in files:
zipf.write('./downloads/' + file)
zipf.close()
return send_file('Rounds.zip', mimetype='zip', attachment_filename='Rounds.zip', as_attachment=True)
else:
raise Exception("Something went wrong. please contact a dev")
if __name__ == '__main__':
app.run(debug=True)