Example #1
0
def edit_post(id):
    post = Post.find(id)
    if request.method == 'GET':
        return render_template('edit_post.html',
                               post=post,
                               articles=Article.all())
    elif request.method == 'POST':
        post.name = request.form['name']
        post.description = request.form['description']
        post.article = Article.find(request.form['article_id'])
        images = request.files.getlist("file")
        shutil.rmtree(post.file_path)
        letters = string.ascii_lowercase
        direc_path = random.choice(letters)
        direc = request.form['name']
        os.mkdir("static/images/" + direc +
                 User.find_by_id(session['USERNAME']) + direc_path)
        for img in images:
            img_path = 'static/images/' + direc + User.find_by_id(
                session['USERNAME']) + direc_path + "/"
            img.save(img_path + img.filename)
        post.file_path = img_path
        post.save()

        #logging.info('%s with id: %s edited post %s', User.find_by_id(session['USERNAME']), session['USERNAME'], post.id)

        return redirect(url_for('show_post', id=post.id))
Example #2
0
def new_post():
    if request.method == 'GET':
        return render_template('new_post.html', articles=Article.all())
    elif request.method == 'POST':
        letters = string.ascii_lowercase
        direc_path = random.choice(letters)
        direc = request.form['name']
        images = request.files.getlist("file")
        os.mkdir("static/images/" + direc +
                 User.find_by_id(session['USERNAME']) + direc_path)
        for img in images:
            img_path = 'static/images/' + direc + User.find_by_id(
                session['USERNAME']) + direc_path + "/"
            img.save(img_path + img.filename)
        article = Article.find(request.form['article_id'])
        values = (None, request.form['name'], request.form['description'],
                  article, img_path, session['USERNAME'])
        Post(*values).create()

        #logging.info('%s with id: %s added new post', User.find_by_id(session['USERNAME']), session['USERNAME'])

        return redirect('/')
Example #3
0
def get_articles():
    return render_template("articles.html", articles=Article.all())