def register(): context = {} reg_form = RegistrationForm() if reg_form.validate_on_submit(): email = reg_form.email.data password = reg_form.password.data user = User.create(email=email, password=password) login_user(user) is_disabled = False if "EMAIL_CONFIRMATION_DISABLED" in current_app.config: is_disabled = current_app.config["EMAIL_CONFIRMATION_DISABLED"] if is_disabled is True: user.is_email_confirmed = True user.email_confirm_date = datetime.datetime.now() user.update() else: token = user.generate_confirmation_token() template = "auth/emails/activate_user" subject = "Please confirm your email" context.update({"token": token, "user": user}) send_async_email(email, subject, template, **context) flash( notify_success("A confirmation email has been sent via email.") ) return redirect(url_for("dashboard.index")) context["form"] = reg_form return render_template("auth/register.html", **context)
def roles_add(): if request.method == "POST": if not Role.query.filter(Role.name == request.form["name"]).first(): role = Role(name=request.form["name"]) role.save() flash(notify_success("Role successfully added")) return redirect(url_for("appadmin.roles")) flash(notify_warning("Role already exists")) return redirect(url_for("appadmin.roles"))
def roles_delete(role_id): role = Role.get_by_id(role_id) if role is None: flash(notify_warning("Unable to delete. Invalid role id")) return redirect(url_for("appadmin.roles")) role.delete() flash(notify_success("Role successfully deleted")) return redirect(url_for("appadmin.roles"))
def logout(): logout_user() flash(notify_success("Successfully logged out")) if "next" not in request.args: next_url = url_for("www.index") else: if request.args.get("next") == "": next_url = url_for("www.index") else: next_url = get_safe_redirect(request.args.get("next")) return redirect(next_url)
def resend(): if current_user.is_email_confirmed: return redirect(url_for("dashboard.index")) token = current_user.generate_confirmation_token() template = "auth/emails/activate_user" subject = "Please confirm your email" context = {"token": token, "user": current_user} send_async_email(current_user.email, subject, template, **context) flash(notify_success("A new confirmation email has been sent.")) return redirect(url_for("auth.unconfirmed"))
def confirm(token): if current_user.is_email_confirmed: flash(notify_warning("Account already confirmed.")) return redirect(url_for("dashboard.index")) if current_user.confirm_token(token): flash(notify_success("You have confirmed your account. Thanks!")) return redirect(url_for("dashboard.index")) flash(notify_warning("The confirmation link is invalid/expired.")) return redirect(url_for("auth.unconfirmed"))
def roles_update(): if request.method == "POST": role = Role.get_by_id(request.form["role_id"]) if role is None: flash(notify_warning("Unable to update. Role does not exist")) return redirect(url_for("appadmin.roles")) role.name = request.form["role_name"] role.update() flash(notify_success("Role successfully updated")) return redirect(url_for("appadmin.roles"))
def admin_delete(id): """ **Delete a User** :param id: id of the user :type id: int """ user = User.query.get(id) if user is None: flash(notify_warning("Unable to delete. Invalid user id")) return redirect("/appadmin") user.delete() flash(notify_success("User successfully deleted")) return redirect("/appadmin")
def index(): context = {} for folder in os.listdir( os.path.join(current_app.config["BASE_DIR"], "modules")): if folder.startswith("__"): continue elif folder.startswith("box__"): for sub_folder in os.listdir( os.path.join(current_app.config["BASE_DIR"], "modules", folder)): if sub_folder in ["dashboard"]: continue if sub_folder.startswith("__"): # ignore __pycache__ continue elif sub_folder.endswith(".json"): # box_info.json continue with open( os.path.join( current_app.config["BASE_DIR"], "modules", folder, sub_folder, "info.json", )) as f: module_info = json.load(f) all_info[sub_folder] = module_info else: if folder not in ["dashboard"]: with open( os.path.join( current_app.config["BASE_DIR"], "modules", folder, "info.json", )) as f: module_info = json.load(f) all_info[folder] = module_info context["all_info"] = all_info flash(notify_success("Notif test")) return render_template("dashboard/index.html", **context)
def admin_update(): """ **Update a User record** """ id = request.form["id"] password = request.form["password"] email = request.form["email"] first_name = request.form["first_name"] last_name = request.form["last_name"] is_admin = request.form.get("is_admin") if is_admin: is_admin = True else: is_admin = False user = User.query.get(id) if user is None: flash(notify_warning("Unable to update. User does not exist.")) return redirect("/admin") user.is_admin = is_admin user.email = email user.first_name = first_name user.last_name = last_name user.roles[:] = [] if password.strip(): user.password = password for key in request.form: if key.startswith("role_"): role_id = key.split("_")[1] role = Role.get_by_id(role_id) user.roles.append(role) user.update() flash(notify_success("User successfully updated")) return redirect("/appadmin")