Skip to content

Commit

Permalink
working on adding upload/download file
Browse files Browse the repository at this point in the history
  • Loading branch information
jonager committed Sep 3, 2017
1 parent 8f29e36 commit c8de190
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
app = Flask(__name__)
app.config["DEBUG"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://lc-attendance:root@localhost:8889/lc-attendance"
ALLOWED_EXTENSIONS = set(['xlsx']) # only allows .xlsx files to be uploaded

db = SQLAlchemy(app)
app.secret_key = "y337kGcys&zP3D4"
36 changes: 36 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from models import Student, Teacher, Attendance
from hash_tools import make_hash, check_hash
import val
import pandas as pd
from io import BytesIO # built-in in python, no need to install
import xlsxwriter
from werkzeug.utils import secure_filename

# Main View
@app.route('/')
Expand Down Expand Up @@ -265,5 +269,37 @@ def editStudent():
return render_template("edit_student.html", student=student)


# Adds all the cohorts students at once into the student table
# only accepts .xlsx files
@app.route('/upload_file', methods = ['POST'])
def upload_file():

if request.method == 'POST':
# TODO add validation, in case user didn't select any file to upload
# TODO checks if only accepts .xlsx files

file = request.files['file']
file = secure_filename(file.filename) # prevents people from uploading
# malicious code

# ----------- Reads Files and pushes to student table -------------
df = pd.read_excel(file)
# df.columns is a list of all the table headings, 'First Name' and 'Last Name'
# in this case.
first_name = list(df[df.columns[0]])
last_name = list(df[df.columns[1]])
# names is a list of tupples in the form of (first_name, last_name)
names = zip(first_name,last_name)

# creates a record for row in students.xlsx into the student table.
for name in names:
student = Student(name[0].title(), name[1].title())
db.session.add(student)
db.session.commit()
return render_template("students.html")




if __name__ == "__main__":
app.run()
7 changes: 7 additions & 0 deletions templates/edit_student.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,12 @@ <h2>Edit Student</h2>
<input type="submit" value="Save Changes">
</form>

<!-- Adds all the cohorts students at once into the student table
only accepts .xlsx files -->
<form action = "/upload_file" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file" /><br><br>
<input type = "submit"/>
</form>

{% endblock %}

12 changes: 12 additions & 0 deletions templates/students.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ <h4>Add Student</h4>
</table>
<input type="submit" value="Add Student">
</form>

<!-- Adds all the cohorts students at once into the student table
only accepts .xlsx files -->
<br>
<br>
<h3>Add roster for this cohort.</h3>
<form action = "/upload_file" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file" /><br><br>
<input type = "submit"/>
</form>
<br>
<br>
{% endblock %}

0 comments on commit c8de190

Please sign in to comment.