def add_admin(): user_api = UserApi() role_api = RoleApi() admin_role = role_api.get_by_role('administrator') if app.config['DEBUG'] is True: password = '******' else: password = ''.join(SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(10)) user_data = { 'email': '*****@*****.**', 'password': password, 'roles': [ admin_role.id ] } try: user_api.get_by_user('*****@*****.**') except DatabaseItemDoesNotExist: admin = user_api.create(user_data) return { 'user': admin, 'password': user_data['password'] }
def v_login(): form = LoginForm() a_user = UserApi() if request.method == 'POST' and form.validate_on_submit(): print(form) try: user = a_user.get_by_user(form.email.data) except DatabaseItemDoesNotExist: # User does not exist flash(_('Invalid username or password.')) else: if user.verify_password(form.password.data): login_user(user, remember=form.remember_me.data) next_url = request.args.get('next') return redirect(next_url or url_for('site.v_index')) else: flash(_('Invalid username or password.')) ## # next=request.args.get('next') must be embedded in the <form action='admin/login?next=next'>, or # otherwise the POST request (when you submit the form) will not include the "next" bit. return render_template('admin/login.html', form=form, next=request.args.get('next'))