Skip to content

Commit 5c18047

Browse files
committed
user/update WIP
1 parent 6f076b1 commit 5c18047

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/server/api/user_api.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def check_username():
319319
return jsonify(user_exists)
320320

321321
@user_api.route("/api/admin/user/update", methods=["POST"])
322-
@jwt_ops.admin_required
322+
#@jwt_ops.admin_required #TODO: remove comment
323323
def user_update():
324324
"""Update existing user record
325325
"""
@@ -333,18 +333,47 @@ def user_update():
333333

334334
# We should get 1+ values to update
335335

336-
update_dict = {"username": username}
336+
update_dict = {}
337337

338338
# Need to be a bit defensive here & select what we want instead of taking what we're given
339-
for key in ["full_name", "password", "role"]:
339+
for key in ["full_name", "active", "role", "password"]:
340340
try:
341341
val = post_dict[key]
342342
update_dict[key] = val
343343
except:
344344
pass
345345

346-
#TODO: WIP
347346
print(update_dict)
347+
348+
# If updating password, need to hash first
349+
350+
351+
# We have a variable number of columns to update.
352+
# We could generate a text query on the fly, but this seems the perfect place to use the ORM
353+
# and let it handle the update for us.
354+
355+
356+
# with engine.connect() as connection:
357+
358+
# for k,v in update_dict.items(): #TODO: Make less awful
359+
# s = text( """UPDATE pdp_users SET :key=:val where username=:u """ )
360+
# s = s.bindparams(u=username, key=k, val=v)
361+
# result = connection.execute(s)
362+
363+
from sqlalchemy import update
364+
from sqlalchemy.orm import Session
365+
366+
with Session(engine) as session:
367+
368+
PU = Table("pdp_users", metadata, autoload=True, autoload_with=engine)
369+
# pr = Table("pdp_user_roles", metadata, autoload=True, autoload_with=engine)
370+
371+
372+
stmt = update(PU).where(PU.username == username).values(update_dict).\
373+
execution_options(synchronize_session="fetch")
374+
375+
result = session.execute(stmt)
376+
348377

349378
return jsonify("Work in process")
350379

0 commit comments

Comments
 (0)