This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
178 lines (159 loc) · 6.57 KB
/
app.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
from __future__ import print_function # In python 2.7
from StringIO import StringIO
import os
import re
from flask import Flask, render_template, request, redirect, url_for
from flask_pymongo import PyMongo
from resources import pymongo_interface, read_csv
from bson.objectid import ObjectId
"""
TODO:
1) csv upload
- for every input entry, make a key/value and don't duplicate on email
2) make an internal save and a save method 3) add a part of the app to add travel reimbursement amount
4) modal?
"""
app = Flask(__name__)
app.debug = True
# testing locally
app.config['MONGO_URI'] = "mongodb://tamuhack17:[email protected]:13826/tamuhack_app"
app.config['MONGO_DBNAME'] = "tamuhack_app"
mongo = PyMongo(app)
REQUIRED_FIELDS = ["email", "first_name", "last_name"]
ADDITIONAL_FIELDS = ["checked_in", "additional", "travel_reimbursement"]
database = pymongo_interface.PyMongoHandler(
mongo, REQUIRED_FIELDS, ADDITIONAL_FIELDS)
@app.route('/')
def home_page():
return render_template('home.html')
@app.route('/profile', methods=['GET', 'POST'])
def profile():
person_id = request.args.get('id', None)
print(person_id)
if person_id:
unique_person = database.get_applicant_by_id(person_id)
return render_template('profile.html', entry=unique_person)
return redirect(url_for("participants", msg="A specific person wasn't requested"))
@app.route('/checkin', methods=["GET", "POST"])
def update():
if request.method == 'POST':
query = request.form.get('query', "")
page = request.form.get('page', None)
person_id = request.form.get('id')
action = request.form.get('action', '')
reimbursement = request.form.get('reimbursement', '')
additional = request.form.get('additional', '')
if person_id:
if action == "checkin":
database.checkin_applicant(person_id=person_id)
elif action == "uncheck":
database.uncheck_applicant(person_id=person_id)
database.update_applicant_info(
person_id=person_id, info_str=additional)
database.update_applicant_reimbursement(
person_id=person_id, reimbursement_str=reimbursement)
if not page:
return redirect(url_for('participants', q=query))
return redirect(url_for('participants', q=query, page=page))
else:
person_id = request.args.get('id', "")
applicant = database.get_applicant_by_id(ObjectId(person_id))
return render_template('checkin-info.html', id=(person_id), checked_in=applicant["checked_in"])
@app.route("/modify", methods=['GET', 'POST'])
def modify():
if request.method == 'POST':
num_uploads, num_repeats = 0, 0
submit_val = request.form.get("submit", "")
save_dict = {
"first_name": request.form.get("fname_add", ""),
"last_name": request.form.get("lname_add", ""),
"email": request.form.get("email_add", ""),
"gender": request.form.get("gender", ""),
"school_year": request.form.get("school_year", ""),
"shirt_size": request.form.get("shirt_size", ""),
"school": request.form.get("school", "")
}
if save_dict["first_name"] and save_dict["last_name"] and save_dict["email"] and submit_val:
if submit_val == "add+checkin":
save_dict["checked_in"] = "true"
saved_data = database.save([save_dict])
num_uploads, num_repeats = saved_data["uploads"], saved_data["repeats"]
output_str = "Successfully Uploaded " + \
str(num_uploads) + " document(s) with " + \
str(num_repeats) + " repeat(s)"
return redirect(url_for('participants', msg=output_str))
elif request.method == 'GET':
return render_template('add_delete.html', count=database.checked_count())
def _pagination_ellipsis(currentPage, nrOfPages):
delta = 2
t_range = []
rangeWithDots = []
t_range.append(1)
l = None
if (nrOfPages <= 1):
return t_range
for i in xrange(currentPage - delta, currentPage + delta + 1):
if (i < nrOfPages and i > 1):
t_range.append(i)
t_range.append(nrOfPages)
for i in t_range:
if l:
if (i - l == 2):
rangeWithDots.append(l + 1)
elif (i - l != 1):
rangeWithDots.append('...')
rangeWithDots.append(i)
l = i
return rangeWithDots
@app.route('/participants')
def participants():
page = int(request.args.get('page', 1))
msg = request.args.get('msg', "Displaying Search Results")
query = request.args.get('q', "")
query_string = ".*" + query + ".*"
re_q = re.compile(query_string, re.IGNORECASE)
query_phrase = {"$or": [{"first_name": re_q},
{"last_name": re_q},
{"email": re_q}]}
if not query:
query_phrase = {}
page_result = database.get_paginated_entries(
page_num=page, query_phrase=query_phrase)
context = dict(
entries=page_result["entries"],
num_pages=page_result["num_pages"],
page_num=page_result["page_num"],
pagination_ellipsis=_pagination_ellipsis(
page_result["page_num"], page_result["num_pages"]),
query=query,
page=page,
count=database.checked_count()
)
return render_template('results.html', **context)
@app.route("/upload", methods=['GET', 'POST'])
def upload():
if request.method == 'GET':
action = request.args.get('action', None)
if action == 'upload_data':
saved_data = database.save(parser.parse_data())
num_uploads, num_repeats = saved_data["uploads"], saved_data["repeats"]
output_str = "Successfully Uploaded " + str(num_uploads) + " document(s) with " + str(
num_repeats) + " repeat(s)"
params = {"msg": output_str}
return redirect(url_for("participants", **params))
elif action == 'return':
return redirect(url_for('participants'))
else:
return render_template("upload.html")
if request.method == 'POST':
input_file = request.files['data_file']
if not input_file:
return "No file"
io = StringIO(input_file.stream.read())
csv_handler = read_csv.CsvHandler(
input_data=io, input_required=REQUIRED_FIELDS)
database.save(csv_handler.generate_documents())
return redirect(url_for("participants"))
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)