From a941c1bf1e303128669f11701817e44345615e92 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Tue, 6 Dec 2022 15:01:55 -0500 Subject: [PATCH] dashboard: Upload resumes --- goathacks/dashboard/__init__.py | 42 ++++++++++++++++++++++++++++-- goathacks/dashboard/forms.py | 7 +++++ goathacks/templates/dashboard.html | 4 ++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/goathacks/dashboard/__init__.py b/goathacks/dashboard/__init__.py index ff4279f..d9ba207 100644 --- a/goathacks/dashboard/__init__.py +++ b/goathacks/dashboard/__init__.py @@ -1,5 +1,8 @@ -from flask import Blueprint, flash, render_template, request +from flask import Blueprint, current_app, flash, jsonify, render_template, request from flask_login import current_user, login_required +from werkzeug.utils import secure_filename + +import os bp = Blueprint("dashboard", __name__, url_prefix="/dashboard") @@ -10,9 +13,44 @@ from goathacks import db @login_required def home(): form = forms.ShirtAndAccomForm(request.form) + resform = forms.ResumeForm(request.form) if request.method == "POST" and form.validate(): current_user.shirt_size = request.form.get('shirt_size') current_user.accomodations = request.form.get('accomodations') db.session.commit() flash("Updated successfully") - return render_template("dashboard.html", form=form) + return render_template("dashboard.html", form=form, resform=resform) + +@bp.route("/resume", methods=["POST"]) +@login_required +def resume(): + form = forms.ResumeForm(request.form) + + """A last minute hack to let people post their resume after they've already registered""" + if request.method == 'POST': + if 'resume' not in request.files: + return "You tried to submit a resume with no file" + + resume = request.files['resume'] + if resume.filename == '': + return "You tried to submit a resume with no file" + + if resume and not allowed_file(resume.filename): + return jsonify( + {'status': 'error', 'action': 'register', + 'more_info': 'Invalid file type... Accepted types are txt pdf doc docx and rtf...'}) + + if resume and allowed_file(resume.filename): + # Good file! + filename = current_user.first_name.lower() + '_' + current_user.last_name.lower() + '_' + str( + current_user.id) + '.' + resume.filename.split('.')[-1].lower() + filename = secure_filename(filename) + resume.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) + return 'Resume uploaded! Return to dashboard' + return "Something went wrong. If this keeps happening, contact hack@wpi.edu for assistance" + + +def allowed_file(filename): + return '.' in filename and \ + filename.split('.')[-1].lower() in ['pdf', 'docx', 'doc', 'txt', + 'rtf'] diff --git a/goathacks/dashboard/forms.py b/goathacks/dashboard/forms.py index 753da09..ebfad2d 100644 --- a/goathacks/dashboard/forms.py +++ b/goathacks/dashboard/forms.py @@ -1,4 +1,5 @@ from flask_wtf import FlaskForm +from flask_wtf.file import FileField, FileRequired, FileAllowed from wtforms import RadioField, TextAreaField from wtforms.validators import DataRequired @@ -7,3 +8,9 @@ class ShirtAndAccomForm(FlaskForm): "None"], validators=[DataRequired()]) accomodations = TextAreaField("Special needs and/or Accomodations") + +class ResumeForm(FlaskForm): + resume = FileField("Resume", validators=[FileRequired(), + FileAllowed(['pdf', 'docx', 'doc', + 'txt', 'rtf'], + "Documents only!")]) diff --git a/goathacks/templates/dashboard.html b/goathacks/templates/dashboard.html index db33d51..26680c6 100644 --- a/goathacks/templates/dashboard.html +++ b/goathacks/templates/dashboard.html @@ -55,12 +55,14 @@
-
+ + {{ resform.csrf_token }}

If you'd like, add your resume to send to sponsors...

File +