def user_storage_fees(): form = StorageFeesSearch() client_list = User.find_user(username_val=current_user.username, retval=USER_CLIENT_USERKEY) form.clients.choices = [(client, client) for client in client_list] if form.validate_on_submit(): chosen_method = form.sort_methods.data chosen_client = form.clients.data data_dict = { SORTMETHOD_KEY: chosen_method, DESIGNER_USERINVKEY: current_user.username, CLIENT_USERINVKEY: chosen_client } json_dict = json.dumps(data_dict) return redirect(url_for("user.user_show_fees", data=json_dict)) return render_template("user/storage-fees.html", form=form, clients=client_list)
def user_search(): client_list = User.find_user(username_val=current_user.username, retval=USER_CLIENT_USERKEY) form = UserSearch() form.client.choices = [(client, client) for client in client_list] form.client.choices.insert(0, (NULLVALUE[0], NULLVALUE[0])) if form.validate_on_submit(): tag_num = form.tag_num.data shipment_num = form.shipment_num.data client = form.client.data data = { TAG_NUM_USERINVKEY: tag_num, SHIPMENT_NUM_USERINVKEY: shipment_num, DESIGNER_USERINVKEY: current_user.username, CLIENT_USERINVKEY: client } data_dict = search_method(data) json_dict = json.dumps(data_dict) return redirect(url_for("user.user_view", data=json_dict)) return render_template("user/search.html", form=form)
def admin_storage_fees(): designer_list = MetaOps.find_one(DESIGNERS_METAKEY) form = StorageFees() form.designer.choices = [(designer, designer) for designer in designer_list] client_list = User.find_user(username_val=designer_list[0], retval=USER_CLIENT_USERKEY) client_list.insert(0, NULLVALUE[0]) form.client.choices = [(client, client) for client in client_list] if form.validate_on_submit(): designer = form.designer.data client = form.client.data findsearch_key = search_method({ DESIGNER_USERINVKEY: designer, CLIENT_USERINVKEY: client }) data = json.dumps(findsearch_key) return redirect(url_for("admin.admin_show_fees", data=data)) return render_template("admin/storage-fees.html", form=form)
def admin_user_password(userid): form = UserPasswordForm() if form.validate_on_submit(): admin_password = form.admin_password.data new_user_password = form.new_user_password.data currentuser_password: str = User.find_user( username_val=current_user.username, retval=USER_PASSWORD_USERKEY) if User.check_pass(currentuser_password, admin_password): User.update_val((USER_PASSWORD_USERKEY, new_user_password), user_id=userid) return redirect(url_for("admin.admin_manage_users")) else: form.admin_password.errors = "Current Admin Password was incorrect!!!" return render_template("admin/change-user-password.html", form=form) return render_template("admin/change-user-password.html", form=form)
def login(): """checks to see if the user is already authenticated or not. If not the user will input their username and password and if it matches they will be stored in flask-login so they can be authenticated. It also checks to see which role the user is and directs them to the appropriate homepage""" if current_user.is_authenticated: user = User.find_user(username_val=current_user.username) path = User.check_roles(user) return redirect(path) form = LoginForm() if form.validate_on_submit() and request.method == "POST": raw_username = request.form.get("username") username = strip_text(raw_username, toStr=True) user = User.find_user(username_val=username) raw_password = request.form.get("password") password = strip_text(raw_password, toStr=True) if user and User.check_pass(user[USER_PASSWORD], password): user_obj = User(username=user[USERNAME], password=user[USER_PASSWORD], email=[USER_EMAIL], roles=user[USER_ROLES], _id=user[USER_ID]) login_user(user_obj) newpath = User.check_roles(user) print(newpath) return redirect(newpath) else: error = "Username or Password was incorrect." return render_template('auth/login.html', title='Sign In', form=form, error=error) return render_template('auth/login.html', title='Sign In', form=form)
def load_user(username): """Flask will try to load a user before every request by calling get_id method from the User class on it and feeding the return value to this function. If the username returned from Flask is valid the user will be loaded.""" user = User.find_user(username_val=username) if not user: return None return User(username=user[USERNAME], password=user[USER_PASSWORD], email=[USER_EMAIL], roles=user[USER_ROLES], _id=user[USER_ID])
def super_employee_edit(): """""" json_data = request.args["data"] database_data = AllInvOps.find_all(json.loads(json_data)) form = EditForm() form.choices.choices = database_data designer_list = MetaOps.find_one(DESIGNERS_METAKEY) form.designer.choices = [(designer, designer) for designer in designer_list] client_list = User.find_user(username_val=designer_list[0], retval=USER_CLIENT_USERKEY) form.client.choices = [(client, client) for client in client_list] if form.validate_on_submit(): if form.move.data: designer = form.movetto_field.data client = form.client.data data = request.form.getlist("inv-data") tagnum_list = strip_text(data, turnto_int=True) AllInvOps.update_all(tagnum_list, mainkey=TAG_NUM_USERINVKEY, update_keys=(DESIGNER_USERINVKEY, CLIENT_USERINVKEY), update_vals=(designer, client)) return redirect(url_for("super_employee.super_employee_search")) if form.delete.data: data = request.form.getlist("inv-data") tagnum_list = strip_text(data, turnto_int=True) AllInvOps.delete_all(tagnum_list, keytodel=TAG_NUM_USERINVKEY) return redirect(url_for("super_employee.super_employee_search")) return render_template("super_employee/edit.html", form=form, dbkeys=userinv_keys)
def chosen_designer_super_employee(designer): """""" client_list = User.find_user(username_val=designer, retval=CLIENT_USERINVKEY) if client_list is None: return jsonify({"clients": [NULLVALUE[0]]}) else: client_list.insert(0, NULLVALUE[0]) return jsonify({"clients": client_list})
def chosen_designer(designer): """This func works with the admin_search function and the javascript n search.html. In search.html, when a designerfrom the designer dropdown list is selected the javascript in search.html picks up thedesigner and fetches this url /admin/search + designer. With the designer name this func is able to query the database and grab the clients associated with it. This func then jsons the db response and returns it so the js can grab it unjson it and add the client names to the selectfield.""" client_list = User.find_user(username_val=designer, retval=USER_CLIENT_USERKEY) if client_list is None: return jsonify({"clients": [NULLVALUE[0]]}) else: client_list.insert(0, NULLVALUE[0]) return jsonify({"clients": client_list})
def admin_create_user(): form = CreateUser() message = None if form.validate_on_submit(): username = form.username.data if User.find_user(username_val=username) == None: password = form.password.data email = form.email.data clients = form.known_clients.data client_list = clients.strip().upper().split(",") User.create_user(username, password, email, client_list) message = "User was Successfully Created." return render_template("/admin/create-user.html", form=form, message=message) else: message = "Username Already Exists." return render_template("/admin/create-user.html", form=form, message=message) return render_template("/admin/create-user.html", form=form, message=message)
def admin_create_worker(): form = CreateWorker() message = None if form.validate_on_submit(): username = form.username.data if User.find_user(username_val=username) == None: password = form.password.data email = form.email.data role = form.roles.data User.create_worker(username, password, email, role) message = "Worker Created Successfully." return render_template("admin/create-worker.html", form=form, message=message) else: message = "Username Already Exists." return render_template("admin/create-worker.html", form=form, message=message) return render_template("admin/create-worker.html", form=form, message=message)