stc

a simple time card webapp
git clone _git@git.brennen.work:stc.git
Log | Files | Refs | README

commit 02c4bef41d8ede117d790febdea04ce3533c4251
parent 6bb5e0f65705b12f1a7e904ba3acdc605a5cf388
Author: Youth Employment Program Production <youthemployment22@gmail.com>
Date:   Tue, 11 Jun 2024 10:33:24 -0600

Bugfix: branch choices in forms. Bugfix: role choices. Feature:Update User route

Diffstat:
Mapp/routes.py | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 89 insertions(+), 9 deletions(-)

diff --git a/app/routes.py b/app/routes.py @@ -189,6 +189,76 @@ def load_user(username): user_obj.password_hash = u['password_hash'] return user_obj +@app.route('/update/<update_t>/<username>',methods=['GET','POST']) +@login_required +def update_user(update_t,username): #username=current_user): + form = ChangeUserForm() + form.role.choices = mongo.db.permissions_collection.find({},{'label':1})#get_available_roles() + + form.branch.choices = [("",'Select Branch'),("Global","Global")] + for branch in get_available_branches(): + form.branch.choices.append(branch) + + user = fetch_user_from_username(username) + match update_t: + case "fname": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'fname':form.fname.data,'username':form.fname.data.lower()+user['mname'].lower()+user['lname'].lower()}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "mname": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'mname':form.mname.data,'username':user['fname'].lower()+form.mname.data.lower()+user['lname'].lower()}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "lname": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'lname':form.lname.data,'username':user['fname'].lower()+user['mname'].lower()+form.lname.data.lower()}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "birthday": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'birthday':form.birthday.data.strftime('%Y-%m-%d')}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "role": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'role':form.role.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "address": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'address':form.address.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "branch": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'branch':form.branch.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "phonenumber": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'phonenumber':form.phonenumber.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "email": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'email':form.email.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "payPeriod": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'pay_period':form.payPeriod.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + case "payValue": + if form.validate_on_submit(): + mongo.db.user_collection.update_one({'username':user['username']},{'$set':{'pay_value':form.payValue.data}}) + flash("change {}.".format(update_t)) + return redirect(url_for('activeusers')) + + return render_template("admin/users/update_user.html",user=user,form=form,update_t=update_t) + @app.route('/newpass',methods=['GET','POST']) @login_required def chgpass(): @@ -201,7 +271,6 @@ def chgpass(): return render_template('admin/users/newpass.html',form=form,ORGNAME=OrganizationName) - @app.route('/newpass/<uid>',methods=['GET','POST']) @login_required def chgpass_by_uid(uid): @@ -1142,8 +1211,11 @@ def moduser(uid): # END TMP Values form = ChangeUserForm() - form.branch.choices = availableBranches - form.branch.default = defaultBranch + + form.branch.choices = [("",'Select Branch'),("Global","Global")] + for branch in get_available_branches(): + form.branch.choices.append(branch) + form.role.choices = availableRoles form.role.default = defaultRole @@ -1182,8 +1254,12 @@ def newuser(): # END TMP Values form = NewUserForm() - form.branch.choices = availableBranches - form.branch.default = defaultBranch + + #form.branch.choices = availableBranches + form.branch.choices = [("",'Select Branch'),("Global","Global")] + for branch in get_available_branches(): + form.branch.choices.append(branch) + form.role.choices = availableRoles #form.process() @@ -1277,9 +1353,10 @@ def newagreement(): #### #### ####### TODO Need to filter out available agrements key=agreement_name:value=_id(agreement) Assign _id to agreement, and write _id(project) to agreement.projects[] +#@app.route("/admin/projects/new/<agreementid>", methods=["GET","POST"]) @app.route("/admin/projects/new", methods=["GET","POST"]) @login_required -def newproject(): +def newproject(agreementid=None): # Available Agreements. Move to fn() availableAgreements = [] for agreement in mongo.db.agreements_collection.find(): @@ -1289,7 +1366,10 @@ def newproject(): form = NewProjectForm() # TODO If statement for optional newproject('argument') if new or none return all choices, else return (agreement_id, agreement_name) for new agreement ID passed <-- What did I mean by this? OH It's a conditional to redirect to create a new agreement... this would require passing the form data to the next route as params? form.agreement.choices = availableAgreements - form.branch.choices = ['Dillon','Salmon'] + #form.branch.choices = ['Dillon','Salmon'] + form.branch.choices = [("",'Select Branch'),("Global","Global")] + for branch in get_available_branches(): + form.branch.choices.append(branch) #TODO MAKE BELOW WORK!!! Apply to other branch dependent areas #branches = [] #for branch in mongo.db.branch_collection.find(): @@ -1412,11 +1492,11 @@ def change_branch(collection,document_id): if form.validate_on_submit(): match collection: case "project": - mongo.db.projects_collection.update_one({"_id":ObjectId(document_id)},{'$set':{'branch':form.branch.data}}) + mongo.db.projects_collection.update_one({"_id":ObjectId(document_id)},{'$set':{'branch':ObjectId(form.branch.data)}}) flash("Changed Branch for Project {} to {}".format(document_id,form.branch.data)) return redirect(url_for('project',project_id=document_id)) case "user": - mongo.db.user_collection.update_one({"_id":ObjectId(document_id)},{'$set':{'branch':form.branch.data}}) + mongo.db.user_collection.update_one({"_id":ObjectId(document_id)},{'$set':{'branch':ObjectId(form.branch.data)}}) flash("Changed Branch for User {} to {}".format(document_id,form.branch.data)) return redirect(url_for('user',user_id=document_id))