Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- from flask import (
- Blueprint, flash, g, redirect, render_template, request, session, url_for
- )
- from flaskr.auth import login_required
- from flaskr.db import get_db
- bp = Blueprint('edit', __name__, url_prefix='/edit')
- def get_content(id, check_author=True):
- post = get_db().execute(
- 'SELECT a.id, header, body, created, author_id, username'
- ' FROM aboutone a JOIN user u ON a.author_id = u.id'
- ' WHERE a.id = ?',
- (id,)
- ).fetchone()
- return post
- @bp.route('/home', methods=('POST', 'GET'))
- @login_required
- def edit_home():
- db = get_db()
- posts = db.execute(
- 'SELECT p.id, header, body, created, author_id, username'
- ' FROM post p JOIN user u ON p.author_id = u.id'
- ' ORDER BY created DESC'
- ).fetchall()
- print(posts)
- if not posts:
- print('test')
- return render_template('edit/create_home.html')
- else:
- print('test1')
- if request.method == 'POST':
- header = request.form['header']
- body = request.form['body']
- error = None
- if not header:
- error = 'Please add some content to the header!'
- elif not body:
- error = 'Please add some content to the body!'
- if error is not None:
- flash(error)
- else:
- db = get_db()
- db.execute(
- 'UPDATE post SET header = ?, body = ?'
- ' WHERE id = ?',
- (header, body, id)
- )
- db.commit()
- return render_template('edit/home.html', posts=posts)
- @bp.route('/create_home', methods=('POST', 'GET'))
- @login_required
- def create_home():
- db = get_db()
- post_header = []
- post_body = []
- post_content = {}
- if request.method == 'POST':
- i = 1
- for header in request.form['header']:
- name = print(header, i)
- name.replace(' ', '')
- post_header.extend(name)
- i += 1
- i = 1
- for body in request.form['body']:
- name = print(body, i)
- name.replace(' ', '')
- post_body.extend(name)
- i += 1
- for header, body in post_header, post_body:
- post_content[header] = body
- db = get_db()
- for header in post_content:
- body = post_content[header]
- db.execute(
- 'INSERT INTO post (header, body, id)'
- ' VALUES (?, ?, ?)',
- (header, body, g.user['id'])
- )
- db.commit()
- return render_template('edit/create_home.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement