Exemple #1
0
def index():
    page = request.args.get('page', 1, type=int)
    form = NewPostForm()
    posts = current_user.followed_posts().paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('index', page=posts.prev_num) \
        if posts.has_prev else None
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('index'))
    return render_template('index.html',
                           title='Home',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Exemple #2
0
def add_post():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post(body=form.body.data, user_id=current_user.id)
        db.session.add(post)
        db.session.commit()
        print(form.body.data)
    return render_template('add_post.html', form=form)
Exemple #3
0
def new_post():
    new_post_form = NewPostForm()
    if new_post_form.validate_on_submit():
        if new_post_form.tweet.data:
            image = new_post_form.image.data
            add_post(UserInfo.get_current_user_userid(),
                     new_post_form.text.data, image)
            return redirect(url_for("profile"))
    return render_current_user_page(new_post_form=new_post_form)
Exemple #4
0
def dashboard():
    form = NewPostForm()
    stats = Stats.query.filter_by(day_comments = date.today()).first() #Query for stats of the day
    if form.validate_on_submit(): #Adding new post
        new_post = Post(title=form.title.data, description=form.description.data,
            body=form.body.data, user_id=current_user.id)
        db.session.add(new_post)
        db.session.commit()
        flash("New post has been posted")
        return redirect(url_for('dashboard'))
    return render_template('dashboard.html', title='Dashboard', form=form, stats = stats)
Exemple #5
0
def create_post():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    body=form.body.data,
                    user_id=current_user.id)
        db.session.add(post)
        db.session.commit()
        flash("New post!!!", "success")
        return redirect(url_for("show_post", post_id=post.id))
    return render_template("/posts/create.html", form=form)
def new_post():
    form = NewPostForm()
    if form.validate_on_submit():
        new_post = Post(title=form.title.data,
                        body=form.body.data,
                        user_id=current_user.id)
        db.session.add(new_post)
        db.session.commit()
        flash('Created new post')
        return redirect(url_for('index'))
    return render_template('new_post.html', title='New Post', form=form)
Exemple #7
0
def new_post():
    form = NewPostForm()
    # validate form data
    if form.validate_on_submit():
        post = Post(title=form.title.data, body=form.body.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Congratulations, you have created a new post!')
        return redirect(url_for('index'))
    # render form
    return render_template('new_post.html', title='New Post', form=form)
Exemple #8
0
def createPost():
    form = NewPostForm()
    if request.method == 'GET':
        form.category.default = 'Academics'
        form.process()
    elif form.validate_on_submit():
        flash('New post for category {}'.format(form.category.data))
        my_post = Post(title=form.title.data, category=form.category.data, body=form.body.data, author=current_user)
        db.session.add(my_post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('category'))
    return render_template('createPost.html', title='New post', form=form)
Exemple #9
0
def new_post():
    form = NewPostForm()
    if form.validate_on_submit():
        blogpost = Post(title=form.title.data,
                        subtitle=form.subtitle.data,
                        author_id=current_user.id,
                        content=form.content.data,
                        date_posted=datetime.now())
        db.session.add(blogpost)
        db.session.commit()
        return redirect(url_for('app.index'))

    return render_template("newpost.html", form=form)
Exemple #10
0
def edit_post(post_id):
    login_form = LoginForm()
    edit = Post.query.filter_by(id=post_id).first()
    edit_post_form = NewPostForm(obj=edit)
    edit_post_form.populate_obj(edit)
    if edit_post_form.validate_on_submit():
        edit.title = edit_post_form.title.data
        edit.body = edit_post_form.body.data
        db.session.commit()
    return render_template("edit_post.html",
                           title="Edit Post",
                           login_form=login_form,
                           edit_post_form=edit_post_form,
                           category=get_category())
Exemple #11
0
def new():
    form = NewPostForm(request.form, prefix="post")
    if form.has_been_submitted(request):
        if form.validate_on_submit():
            new_post = Post(user=current_user, title=form.title.data, content=form.content.data)
            status = new_post.save()
            if status:
                flash("Successfully posted the article")
                return redirect(url_for('blog.index', user_slug=current_user.blog_slug))
            else:
                flash("Something went wrong...", category="error")

    return render_template("new_post.html", form=form,
                           syntax_highlighter_css=generate_syntax_highlighter_css(current_user))
Exemple #12
0
def edit_post(postid):
	if current_user.role != 3:
		abort(404)
	form = NewPostForm()
	post = Blogpost.query.filter_by(id = postid).first_or_404()
	if form.validate_on_submit():
		post.title = form.title.data
		post.content = form.main_text.data
		post.is_in_slider = form.is_in_slider.data
		db.session.commit()
		return redirect(url_for('post', postid = postid))
	elif request.method == 'GET':
		form.title.data = post.title
		form.main_image.data = post.main_image_path
		form.main_text.data = post.content
		form.is_in_slider.data = post.is_in_slider

	return render_template('edit_post.html', post = post, form = form)
Exemple #13
0
def make_post():
    user_id = current_user.get_id()
    # user_id = 1
    user = User.query.get(user_id)
    form = NewPostForm()
    form['user_id'].data = user_id
    form['csrf_token'].data = request.cookies['csrf_token']

    if form.validate_on_submit():
        post = Post(user_id=form.data['user_id'],
                    title=form.data['title'],
                    content=form.data['content'],
                    created_on=datetime.datetime.now(),
                    last_modified=datetime.datetime.now())
        db.session.add(post)
        db.session.commit()
        return post.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}
Exemple #14
0
def new():
    form = NewPostForm(request.form, prefix="post")
    if form.has_been_submitted(request):
        if form.validate_on_submit():
            new_post = Post(user=current_user,
                            title=form.title.data,
                            content=form.content.data)
            status = new_post.save()
            if status:
                flash("Successfully posted the article")
                return redirect(
                    url_for('blog.index', user_slug=current_user.blog_slug))
            else:
                flash("Something went wrong...", category="error")

    return render_template(
        "new_post.html",
        form=form,
        syntax_highlighter_css=generate_syntax_highlighter_css(current_user))
Exemple #15
0
def index():
    form = NewPostForm()
    posts = Post.query.all()[::-1]
    users = User.query.all()
    if form.validate_on_submit():
        userid = current_user.get_id()
        post = Post(body=form.postbody.data,
                    timestamp=datetime.utcnow(),
                    user_id=userid)
        db.session.add(post)
        db.session.commit()
        flash('New post successful.')
        return redirect(url_for('index'))

    return render_template('index.html',
                           title='Home',
                           posts=posts,
                           users=users,
                           form=form)
Exemple #16
0
def new_post():
    form = NewPostForm()
    if form.validate_on_submit():
        userid = current_user.get_id()
        post = Post(body=form.postbody.data,
                    timestamp=datetime.utcnow(),
                    user_id=userid)
        if form.attachment.data:
            f = form.attachment.data
            name_append = ''.join([str(randrange(9)) for n in range(9)])
            filename = str(
                current_user.id) + '_' + name_append + '_' + secure_filename(
                    f.filename)
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            post.attachment = filename
            flash(f'Uploaded {filename}.')
        db.session.add(post)
        db.session.commit()
        flash('New post successful.')
        return redirect(url_for('index'))
    return render_template('new_post.html', title='New Post', form=form)
Exemple #17
0
def getpost(post_id):
    post = Post.query.get(post_id)
    comments = PostComment.query.filter_by(post_id=post.id)
    op = User.query.get(post.user_id)
    form = NewPostForm()
    if form.validate_on_submit():
        userid = current_user.get_id()
        comment = PostComment(body=form.postbody.data,
                              timestamp=datetime.utcnow(),
                              user_id=userid,
                              post_id=post.id)
        db.session.add(comment)
        db.session.commit()
        flash('Comment posted.')
        return redirect(request.referrer)
    else:
        return render_template('getpost.html',
                               title=f"{post.username}'s post.",
                               form=form,
                               post=post,
                               comments=comments)
Exemple #18
0
def edit_post_image(postid):
	if current_user.role != 3:
		abort(404)

	post = Blogpost.query.filter_by(id = int(postid)).first_or_404()

	form = NewPostForm()

	if form.validate_on_submit():
		file = form.main_image.data
		if file == None:
			filename = "post_default.png"
		else:
			filename = secure_filename(file.filename)
			file.save(os.path.join(app.config['POST_IMAGE_SAVE_PATH'], filename))
	
		post.main_image_path = filename
		db.session.commit()
		return redirect(url_for('post', postid = post.id))	

	return render_template('edit_post_image.html', form = form, post = post)
Exemple #19
0
def create_post():
    login_form = LoginForm()
    new_post_form = NewPostForm()
    if new_post_form.validate_on_submit():
        if current_user.is_authenticated:
            selected_category = Category.query.filter_by(
                name=str(new_post_form.category.data)).first()
            if new_post_form.image.data is not None:
                filename = hashlib.md5(
                    str(new_post_form.image.data.filename).encode(
                        'utf-8')).hexdigest()
                file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
                new_post_form.image.data.save(file_path)
                post = Post(title=new_post_form.title.data,
                            body=new_post_form.body.data,
                            user_email=current_user.email,
                            category=selected_category.id,
                            image=filename,
                            price=new_post_form.price.data)
            else:
                post = Post(title=new_post_form.title.data,
                            body=new_post_form.body.data,
                            user_email=current_user.email,
                            category=selected_category.id,
                            price=new_post_form.price.data)
            db.session.add(post)
            db.session.commit()
            flash(
                'Your post has been made! Please wait at least 24 hours for it to go live.'
            )
            return redirect(url_for('index'))
        else:
            return redirect("login")
    return render_template("create_post.html",
                           title="Create Post",
                           login_form=login_form,
                           new_post_form=new_post_form,
                           category=get_category())
Exemple #20
0
def index():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your new post has been submitted')
        return redirect(url_for('index'))

    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('index',
                       page=posts.prev_num) if posts.has_prev else None

    return render_template('index.html',
                           title='Home',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Exemple #21
0
def new_post():
	if current_user.role != 3:
		abort(404)
	form = NewPostForm()
	if form.validate_on_submit():
		file = form.main_image.data
		print(file)
		if file == None:
			filename = "post_default.png"
			print('yeah')
		else:
			filename = secure_filename(file.filename)
			file.save(os.path.join(app.config['POST_IMAGE_SAVE_PATH'], filename))
		post = Blogpost(
			title = form.title.data,
			content = form.main_text.data,
			is_in_slider = form.is_in_slider.data,
			main_image_path = filename
			)
		print(post.main_image_path)
		db.session.add(post)
		db.session.commit()
		return redirect(url_for('posts'))
	return render_template('new_post.html', form = form)