@@ -319,7 +319,7 @@ def check_username():
319
319
return jsonify (user_exists )
320
320
321
321
@user_api .route ("/api/admin/user/update" , methods = ["POST" ])
322
- @jwt_ops .admin_required
322
+ # @jwt_ops.admin_required #TODO: remove comment
323
323
def user_update ():
324
324
"""Update existing user record
325
325
"""
@@ -333,18 +333,47 @@ def user_update():
333
333
334
334
# We should get 1+ values to update
335
335
336
- update_dict = {"username" : username }
336
+ update_dict = {}
337
337
338
338
# 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 " ]:
340
340
try :
341
341
val = post_dict [key ]
342
342
update_dict [key ] = val
343
343
except :
344
344
pass
345
345
346
- #TODO: WIP
347
346
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
+
348
377
349
378
return jsonify ("Work in process" )
350
379
0 commit comments