Ejemplo n.º 1
0
def create():
	form = CreatePost()
	if form.validate_on_submit():

		file = request.files['file']
		if file and allowed_file(file.filename):
			filename = secure_filename(file.filename)
			file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

		
		post = Post(title = form.title.data, body=form.body.data, image_path = filename)
		user = User.query.get(current_user.get_id())
		user.posts.append(post)
		db.session.add(user)

		for tag in form.tags.data.split(' '):
			if Tag.query.filter(Tag.body == tag).first():
				add_tag = Tag.query.filter(Tag.body == tag).first()
				post.tags.append(add_tag)
			else:
				add_tag = Tag(body=tag)
				post.tags.append(add_tag)
				db.session.add(add_tag)
		db.session.add(post)
		db.session.commit()
		return redirect(url_for('post.index'))
	return render_template('post/create.html', title='Create Post', form=form)
Ejemplo n.º 2
0
def post_by_id(post_id):
    post = Post.query.get(post_id)
    if request.method == "GET":
        return post.to_dict()
    elif request.method == "PUT":
        form = CreatePost()
        if form.validate_on_submit():
            form.populate_obj(post)
            db.session.commit()

            if "images" in request.files:
                images = request.files.getlist("images")
                for image in images:
                    if allowed_file(image.filename):
                        image.filename = get_unique_filename(image.filename)
                        image_url = upload_file_to_s3(image, Config.S3_BUCKET)
                        image = PostsImage(post_id=post.id,
                                           image_url=image_url)
                        db.session.add(image)
                db.session.commit()
            return post.to_dict()
        return {"errors": validation_errors_to_error_messages(form.errors)}
    elif request.method == "DELETE":
        post.title = "[DELETED]"
        post.body = "[DELETED]"
        posts_images = PostsImage.query.filter(
            PostsImage.post_id == post_id).all()
        for image in posts_images:
            db.session.delete(image)
        db.session.commit()

        return post.to_dict()
    return post.to_dict()
Ejemplo n.º 3
0
def index():
    if not current_user.is_anonymous:
        user = current_user

        page = request.args.get('page', 1, type=int)
        posts = user.friends_posts().paginate(page, app.config['POST_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

        post_form = CreatePost()

        if post_form.validate_on_submit():
            media_id = upload_media(post_form)

            post = Post(body=post_form.text.data,
                        photo_id=media_id,
                        user_id=current_user.id)

            db.session.add(post)
            db.session.commit()

            return redirect(url_for('index'))
        return render_template('index.html', title='Homepage', posts=posts.items, post_form=post_form,
                               next_url=next_url, prev_url=prev_url)
    return render_template('index.html', title='Homepage')
Ejemplo n.º 4
0
def user(username):
    user = User.query.filter_by(username=username).first_or_404()
    profile = user.profile
    post_form = CreatePost()

    if post_form.validate_on_submit():
        media_id = upload_media(post_form)

        post = Post(body=post_form.text.data,
                    photo_id=media_id,
                    user_id=current_user.id)

        db.session.add(post)
        db.session.commit()

        return redirect(url_for('user', username=username))

    page = request.args.get('page', 1, type=int)
    posts = user.posts.filter_by(is_deleted=False).order_by(
        Post.timestamp.desc()).paginate(page, app.config['POST_PER_PAGE'], False)
    next_url = url_for('user', username=user.username, page=posts.next_num) if posts.has_next else None
    prev_url = url_for('user', username=user.username, page=posts.prev_num) if posts.has_prev else None

    return render_template('profile.html', user=user, profile=profile, posts=posts.items, post_form=post_form,
                           next_url=next_url, prev_url=prev_url)
Ejemplo n.º 5
0
def make_post():
	form = CreatePost()
	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('Post Published')
		return redirect(url_for('index'))
	return render_template('make_post.html', title = 'Create Post', form = form)
Ejemplo n.º 6
0
def update(id):
	form = CreatePost()
	post = Post.query.get(id)
	if form.validate_on_submit():
		post.title = form.title.data
		post.body = form.body.data
		db.session.commit()
		flash('Your changes have been saved.')
		return redirect(url_for('post.index'))
	elif request.method == "GET":
		form.title.data = post.title
		form.body.data = post.body
	return render_template('post/update.html', title='Update Post', form = form, post=post )
Ejemplo n.º 7
0
def add_post():
    form = CreatePost()

    if not form.validate_on_submit():
        flash(list(form.errors.values())[0])
        return redirect(url_for('admin_bp.create_post'))
    Post(author=current_user.get_username(),
         title=request.form.get('title'),
         content=re.sub(r'\s+', '', request.form.get('content')),
         date_time=Utilities.post_timestamp(),
         status=request.form.get('status'),
         category=Category().custom_query(
             'id', request.form.get('category')).category_name).add_post()

    flash('Your post has been created.')
    return redirect(url_for('admin_bp.posts'))
Ejemplo n.º 8
0
def account():
    create_form = CreatePost()

    if create_form.validate_on_submit():
        sql = text(
            "insert into post(user_id, title, content, date, views) values (\'{}\', \'{}\', \'{}\', \'{}\', \'{}\')"
            .format(current_user.id, create_form.title.data,
                    create_form.content.data, datetime.datetime.utcnow(), 0))
        result = db.engine.execute(sql)
        return redirect(url_for('index'))
    is_auth = False
    if current_user.is_authenticated:
        is_auth = True
    return render_template("account.html",
                           is_auth=is_auth,
                           create_form=create_form)
Ejemplo n.º 9
0
def sell():
    form = CreatePost()
    # Get all data from the form, process it and send it to the db
    if form.validate_on_submit():
        new_post = PostData(item=form.item.data,
                            stock=form.stock.data,
                            content=form.content.data,
                            author=current_user.username)
        if form.img.data:
            new_post.img = save_img(form.img.data)
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('user_posts'))
    else:
        # Get post from db and order it by most voted posts in descending order
        feed_posts = PostData.query.order_by(PostData.likes.desc()).all()
        return render_template("sell.html", form=form, feed=feed_posts)
Ejemplo n.º 10
0
def create_post():
    form = CreatePost()
    if form.validate_on_submit():
        post = Post()
        form.populate_obj(post)
        db.session.add(post)
        db.session.commit()

        if "images" in request.files:
            images = request.files.getlist("images")
            for image in images:
                if allowed_file(image.filename):
                    image.filename = get_unique_filename(image.filename)
                    image_url = upload_file_to_s3(image, Config.S3_BUCKET)
                    image = PostsImage(post_id=post.id, image_url=image_url)
                    db.session.add(image)
            db.session.commit()
        return post.to_dict()
    return {"errors": validation_errors_to_error_messages(form.errors)}