def post(): form = PostForm() tags_field = request.values.get("tags_field", '') if form.validate_on_submit(): image_id = None if form.image.data: file_path, img_id = secure_image_uploader( form, file_path=BLOG_POST_IMAGES_PATH) _resize_post_image_for_home_page_and_post_page(file_path, img_id) image_id = img_id category = _get_form_category_data(form) author = Author.query.get(Session.get_session_by_id()) title, body = _get_data_from_form(form) post = Post(author=author, title=title, body=body, image=image_id, category=category) _save_tag(post, tags_field) db.session.add(post) db.session.commit() slug = _gen_slug_from_post(post) flash(constants.ARTICLE_POSTED_MSG) if is_safe_url('blog_app.article'): return redirect(url_for('blog_app.articles', slug=slug)) return render_template("blog/post.html", form=form, action="new")
def test_user_login(self): rv = self.app.post("/register", data=self.user_dict(), follow_redirects=True) with self.app as c: rv = c.post("/login", data=self.user_dict(), follow_redirects=True) assert Session.get_session_by_id() == 1
def login(): form = LoginForm() error = None if request.method == "GET" and request.args.get("next"): Session.add(key="next", value=request.args.get("next")) if form.validate_on_submit(): author = Author.query.filter_by(email=form.email.data.lower()).first() _add_user_details_to_session(author) next = Session.get("next") return url_secure_redirect( 'blog_post_app.post', next) if next else url_secure_redirect("blog_app.index") return render_template("author/login.html", form=form, error=error)
def _add_user_details_to_session(author): Session.add('id', author.id) Session.add('email', author.email) Session.add('full_name', author.full_name)
def logout(): """""" Session.clear_all() flash(constants.LOGOUT_MSG) return url_secure_redirect("author_login_app.login")
def is_user_logged_in(*args, **kwargs): if Session.get_session_by_id(): return url_secure_redirect(url_to_redirect_to="blog_app.index") return f(*args, **kwargs)
def decorated_funcion(*args, **kwargs): if Session.get_session_by_id() is None: flash(constants.LOGIN_REQUIRED_MSG) return url_secure_redirect(url_to_redirect_to="author_login_app.login", go_to_referred_url=True) return f(*args, **kwargs)