Advertisement
Guest User

edit.py

a guest
Jun 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. import functools
  2. from flask import (
  3.         Blueprint, flash, g, redirect, render_template, request, session, url_for
  4.     )
  5. from flaskr.auth import login_required
  6. from flaskr.db import get_db
  7.    
  8. bp = Blueprint('edit', __name__, url_prefix='/edit')
  9.  
  10. def get_content(id, check_author=True):
  11.     post = get_db().execute(
  12.         'SELECT a.id, header, body, created, author_id, username'
  13.         ' FROM aboutone a JOIN user u ON a.author_id = u.id'
  14.         ' WHERE a.id = ?',
  15.         (id,)
  16.     ).fetchone()
  17.        
  18.     return post
  19.  
  20. @bp.route('/home', methods=('POST', 'GET'))
  21. @login_required
  22. def edit_home():
  23.     db = get_db()
  24.     posts = db.execute(
  25.         'SELECT p.id, header, body, created, author_id, username'
  26.         ' FROM post p JOIN user u ON p.author_id = u.id'
  27.         ' ORDER BY created DESC'
  28.     ).fetchall()
  29.     print(posts)
  30.     if not posts:
  31.         print('test')
  32.         return render_template('edit/create_home.html')
  33.     else:
  34.         print('test1')
  35.         if request.method == 'POST':
  36.             header = request.form['header']
  37.             body = request.form['body']
  38.             error = None
  39.            
  40.             if not header:
  41.                 error = 'Please add some content to the header!'
  42.             elif not body:
  43.                 error = 'Please add some content to the body!'
  44.                
  45.             if error is not None:
  46.                 flash(error)
  47.             else:
  48.                 db = get_db()
  49.                 db.execute(
  50.                     'UPDATE post SET header = ?, body = ?'
  51.                     ' WHERE id = ?',
  52.                     (header, body, id)
  53.                 )
  54.                 db.commit()
  55.            
  56.         return render_template('edit/home.html', posts=posts)
  57.        
  58. @bp.route('/create_home', methods=('POST', 'GET'))
  59. @login_required
  60. def create_home():
  61.     db = get_db()
  62.     post_header = []
  63.     post_body = []
  64.     post_content = {}
  65.     if request.method == 'POST':
  66.         i = 1
  67.         for header in request.form['header']:
  68.             name = print(header, i)
  69.             name.replace(' ', '')
  70.             post_header.extend(name)
  71.             i += 1
  72.         i = 1
  73.         for body in request.form['body']:
  74.             name = print(body, i)
  75.             name.replace(' ', '')
  76.             post_body.extend(name)
  77.             i += 1
  78.        
  79.         for header, body in post_header, post_body:
  80.             post_content[header] = body
  81.            
  82.         db = get_db()
  83.         for header in post_content:
  84.             body = post_content[header]
  85.             db.execute(
  86.                 'INSERT INTO post (header, body, id)'
  87.                 ' VALUES (?, ?, ?)',
  88.                 (header, body, g.user['id'])
  89.             )
  90.             db.commit()
  91.            
  92.     return render_template('edit/create_home.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement