From 8ab81012141319ae1dec5952e11a9de7a637e8a6 Mon Sep 17 00:00:00 2001 From: ngolp Date: Thu, 31 Oct 2024 13:41:12 -0400 Subject: [PATCH] basic implementation of github issue #28 (modifying user details right from admin dashboard). Went with first name, last name, school, and phone number, since the user can change the shirt size and special accomodations on their own. --- goathacks/admin/__init__.py | 40 ++++++++++++++++++++++++++++++++++ goathacks/templates/admin.html | 36 ++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/goathacks/admin/__init__.py b/goathacks/admin/__init__.py index 08ddf33..ebecfc8 100644 --- a/goathacks/admin/__init__.py +++ b/goathacks/admin/__init__.py @@ -4,6 +4,8 @@ from flask_mail import Message from goathacks.models import User +from sqlalchemy.exc import IntegrityError + bp = Blueprint("admin", __name__, url_prefix="/admin") from goathacks import db, mail as app_mail @@ -221,6 +223,44 @@ def hackers(): users = User.query.all() return User.create_json_output(users) +@bp.route("/updateHacker", methods=["POST"]) +@login_required +def updateHacker(): + if not current_user.is_admin: + return redirect(url_for("dashboard.home")) + + # get params from json + hacker_id = request.json['hacker_id'] + change_field = request.json['change_field'] + new_val = request.json['new_val'] + + # find the user in db + user = User.query.filter_by(id=hacker_id).one() + if user is None: + return {"status": "error", "msg": "user not found"} + + + # update the hacker depending on change_field + match change_field: + case "first_name": + user.first_name = new_val + case "last_name": + user.last_name = new_val + case "school": + user.school = new_val + case "phone": + user.phone = new_val + + try: + db.session.commit() + except IntegrityError as err: + db.session.rollback() + flash("Could not update user information for user " + hacker_id) + return {"status": "error"} + + + return {"status": "success"} + import json import csv from io import StringIO diff --git a/goathacks/templates/admin.html b/goathacks/templates/admin.html index ff5a99b..a5ef1fd 100644 --- a/goathacks/templates/admin.html +++ b/goathacks/templates/admin.html @@ -21,7 +21,7 @@ data-bs-toggle="dropdown">Search @@ -93,11 +93,20 @@ {{ hacker.id }} {{ hacker.last_login }} {{ hacker.email }} - {{ hacker.first_name + ' ' + hacker.last_name }} - {{ hacker.phone }} + +
+ + +
+ + + + {{ hacker.shirt_size }} {{ hacker.accomodations }} - {{ hacker.school }} + + + {% endfor %} @@ -130,6 +139,25 @@ } } } + + function updateHacker(id, change_field, new_val) { + //tell backend to update a specific field for a hacker + const headers = [ + ["Content-Type", "application/json"], + ]; + + let body = { + "hacker_id": id.substr(0, id.indexOf('-')), + "change_field": change_field, + "new_val": new_val, + } + + //send the post request, and report the error if there is one + fetch('/admin/updateHacker', {method: 'POST', body: JSON.stringify(body), headers: headers}).catch((err) => { + window.alert("Error updating user - see console for details"); + console.log(err); + }) + } {% endblock %}