示例#1
0
def update_post(post_id):
    """
    To update the post
    :param post_id: unique number by post
    :return: if update post submitted, redirect posts.post and post.id.
    Render the create_post.html, title, from, legend, year_dict, month_dict,
    side_posts
    """
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    # if update post is submitted, redirected to posts.post
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('posts.post', post_id=post.id))
    # To get the title and content to show it on the form
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post',
                           year_dict=year_dict,
                           month_dict=month_dict,
                           side_posts=side_posts)
示例#2
0
def new_post():
    """
    To post the new blogs on the website
    :return: render create_post.html, title, form, legend, year_dict,
    month_dict, side_posts
    """
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('main.home'))
    # to get the number of posts by year and month for submenu
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post',
                           year_dict=year_dict,
                           month_dict=month_dict,
                           side_posts=side_posts)
示例#3
0
def account():
    """
    To update the account information
    :return: if the form is submitted, redirect to users.account, else
    render account.html, title, image_file, form, year_dict, month_dict,
    side_posts with the current user and email filled on the form
    """
    form = UpdateAccountForm()
    if form.validate_on_submit():
        # if the picture is updated
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            current_user.image_file = picture_file
        current_user.username = form.username.data
        current_user.email = form.email.data
        db.session.commit()
        flash('Your account has been updated!', 'success')
        return redirect(url_for('users.account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email
    # to show the image data on the web site
    image_file = url_for('static',
                         filename='profile_pics/' + current_user.image_file)
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('account.html', title='Account',
                           image_file=image_file, form=form,
                           year_dict=year_dict, month_dict=month_dict,
                           side_posts=side_posts)
示例#4
0
def login():
    """
    To login
    :return: if authenticated, redirected to homepage
    if login form is submitted, and url contains next, redirect to next page,
    otherwise to homepage
    At default render login.html, title, form, year_dict, month_dict,
    side_posts
    """
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        # to check the password
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            flash('You have been logged in!', 'success')
            return redirect(next_page) if next_page else redirect(
                url_for('main.home'))
        else:
            flash('Login Unsuccessful. Please check username and password',
                  'danger')
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('login.html', title='Login', form=form,
                           year_dict=year_dict, month_dict=month_dict,
                           side_posts=side_posts)
示例#5
0
def register():
    """
    To register the account for new users
    :return: if authenticated, redirect to homepage.
    if registration is submitted, redirect to login page
    At default, render registration page, title, form, year_dict, month_dict,
    side_posts
    """
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.
                                                        data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created!', 'success')
        return redirect(url_for('users.login'))
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('register.html', title='Register', form=form,
                           year_dict=year_dict, month_dict=month_dict,
                           side_posts=side_posts)
示例#6
0
def update_password(username):
    """
    To update the password with authenticated user
    :param username: current login user
    :return: if form is submitted, redirect to users.account
    At default, render update_password.html, title, image_file, form, year_dict
    , month_dict, side_posts
    """
    form = UpdatePasswordForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=username).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.oldpassword.data):
            hashed_password = bcrypt.generate_password_hash(
                form.password.data).decode('utf-8')
            current_user.password = hashed_password
            db.session.commit()
            flash('Your password has been updated!', 'success')
            return redirect(url_for('users.account'))
    image_file = url_for('static',
                         filename='profile_pics/' + current_user.image_file)
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('update_password.html', title='Update Password',
                           image_file=image_file, form=form,
                           year_dict=year_dict, month_dict=month_dict,
                           side_posts=side_posts)
示例#7
0
def about():
    """
    To show about of blog site
    :return: render about.html, title, year_dict, month_dict, side_posts
    """
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('about.html', title='About', year_dict=year_dict,
                           month_dict=month_dict, side_posts=side_posts)
示例#8
0
def home():
    """
    To show teh posts on the homepage
    :return: render home.html, posts, year_dict, month_dict, side_posts
    """
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page,
                                                                  per_page=5)
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('home.html', posts=posts, year_dict=year_dict,
                           month_dict=month_dict, side_posts=side_posts)
示例#9
0
def post(post_id):
    """
    To show the each posts
    :param post_id: unique number by post
    :return: render post.html, title, post, year_dict, month_dict, side_posts
    """
    post = Post.query.get_or_404(post_id)
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('post.html',
                           title=post.title,
                           post=post,
                           year_dict=year_dict,
                           month_dict=month_dict,
                           side_posts=side_posts)
示例#10
0
def user_posts(username):
    """
    To show the posts by username
    :param username: username registered on the db
    :return: render user_posts.html, posts, user, year_dict, month_dict,
    side_posts
    """
    page = request.args.get('page', 1, type=int)
    user = User.query.filter_by(username=username).first_or_404()
    posts = Post.query.filter_by(author=user)\
        .order_by(Post.date_posted.desc())\
        .paginate(page=page, per_page=5)
    year_dict, month_dict = get_postdates()
    side_posts = Post.query.order_by(Post.date_posted.desc()).limit(5)
    return render_template('user_posts.html', posts=posts, user=user,
                           year_dict=year_dict, month_dict=month_dict,
                           side_posts=side_posts)