def delete(id): db = get_db() db.execute('DELETE FROM tag WHERE post_id = ?', (id, )) db.commit() db.execute('UPDATE post' ' SET dirname = "trash"' ' WHERE id = ?', (id, )) db.commit() return redirect(url_for('diarybook.index'))
def create(): if (request.method == 'POST'): title = request.form['title'] body = request.form['body'] dirname = request.form['dirname'] print(dirname) tags = request.form['tags'] error = None if not title: error = 'Title is required.' if dirname == "trash": error = 'dirname is not allow use "trash"' if not dirname: dirname = "auto" if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO post (title, body, author_id, dirname, tags)' ' VALUES (?, ?, ?, ?, ?)', (title, body, g.user['id'], dirname.lower(), tags.lower())) db.commit() post_id = db.execute( 'SELECT last_insert_rowid() newid').fetchone()[0] print("post_id:", post_id) taglist = set(tags.lower().replace(", ", ",").split(",")) for tag in taglist: print("tag: ", tag) db.execute('INSERT INTO tag (post_id, name)' ' VALUES (?, ?)', (post_id, tag)) db.commit() return redirect(url_for('diarybook.index')) return render_template('diarybook/create.html')
def register(): if (request.method == 'POST'): username = request.form['username'] password = request.form['password'] headimge = "default.jpg" db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.execute('SELECT id' ' FROM user' ' WHERE username = ?', (username, )).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: db.execute( 'INSERT INTO user (username, password, headimge) VALUES (?, ?, ?)', (username, generate_password_hash(password), headimge)) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() db.commit() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' # if request.form['username'] == '0': # error = None if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) print("error: ", error) flash(error) return render_template('auth/login.html')
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def searchtag(name): db = get_db() posts = db.execute( 'SELECT p.id, title, body, created, author_id, tags' ' FROM post p JOIN tag t ON p.id = t.post_id' ' WHERE t.name = ?', (name, )).fetchall() db.commit() return render_template('search/searchtag.html', posts=posts, name=name)
def trash(): db = get_db() posts = db.execute( 'SELECT id, title, body, created, author_id, tags' ' FROM post WHERE dirname = "trash" AND author_id = ?', (g.user['id'], )).fetchall() db.commit() return render_template('search/dirdetail.html', posts=posts, dirname="trash")
def tags(): db = get_db() id = g.user['id'] if id is None: return redirect('index') posts = db.execute( 'SELECT DISTINCT name' ' FROM tag t JOIN post p ON t.post_id = p.id' ' WHERE p.author_id = ?', (g.user['id'], )).fetchall() db.commit() for post in posts: print("tag: ", post['name']) return render_template('search/tags.html', posts=posts)
def directory(): db = get_db() id = g.user['id'] if id is None: return redirect('index') posts = db.execute( 'SELECT DISTINCT dirname' ' FROM post' ' WHERE author_id = ?' ' AND dirname != "trash"', (id, )).fetchall() db.commit() return render_template('search/directory.html', posts=posts)
def get_post(id, check_author=True): db = get_db() post = db.execute( 'SELECT p.id, title, body, created, author_id, username, dirname, tags' ' FROM post p JOIN user u ON p.author_id = u.id' ' WHERE p.id = ?', (id, )).fetchone() db.commit() if (post is None): abort(404, "Post id {0} dosn't exist.".format(id)) if (check_author and post['author_id'] != g.user['id']): abort(403) return post
def timeline(): db = get_db() if (request.method == 'POST'): yy = request.form['sy'] if not yy: yy = '1970' mm = request.form['sm'] if not mm: mm = '01' dd = request.form['sd'] if not dd: dd = '01' st = '-'.join([yy, mm, dd]) yy = request.form['ey'] mm = request.form['em'] dd = request.form['ed'] ed = '-'.join([yy, mm, dd]) if ((not yy) or (not mm) or (not dd)): ed = time.strftime('%Y-%m-%d', time.localtime(time.time())) print(st) print(ed) id = g.user['id'] posts = db.execute( 'SELECT id, title, body, created, author_id, dirname' ' FROM post' ' WHERE author_id = ?' ' AND date(created) >= date(?)' ' AND date(created) <= date(?)' ' AND dirname != "trash"' ' ORDER BY created DESC', (id, st, ed)).fetchall() db.commit() return render_template('diarybook/index.html', posts=posts) else: posts = db.execute('SELECT id, title, body, created, author_id' ' FROM post' ' WHERE author_id = 0').fetchall() db.commit() return render_template('diarybook/timeline.html', posts=posts)
def index(): db = get_db() if (g.user): posts = db.execute( 'SELECT p.id, title, body, created, author_id, username' ' FROM post p JOIN user u ON p.author_id = u.id' ' WHERE u.id = ? AND dirname != "trash"' ' ORDER BY created DESC', (g.user['id'], )).fetchall() db.commit() else: posts = db.execute('SELECT id, title, body, created, author_id' ' FROM post' ' WHERE author_id = 0').fetchall() db.commit() print("show posts") for post in posts: for item in post: print(item) print("show posts end") return render_template('diarybook/index.html', posts=posts)
def update(id): post = get_post(id) if (request.method == 'POST'): title = request.form['title'] body = request.form['body'] dirname = request.form['dirname'] tags = request.form['tags'] error = None print("show request.form") for item in request.form: print(item) print("show request.form end") if not title: error = "Title is required" if dirname == "trash": error = 'dirname is not allow use "trash"' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE post SET title = ?, body = ?, dirname = ?, tags = ?' ' WHERE id = ?', (title, body, dirname.lower(), tags.lower(), id)) db.commit() db.execute('DELETE FROM tag WHERE post_id = ?', (id, )) db.commit() taglist = set(tags.lower().replace(", ", ",").split(",")) for tag in taglist: print("tag: ", tag) db.execute('INSERT INTO tag (post_id, name)' ' VALUES (?, ?)', (id, tag)) db.commit() return redirect(url_for('diarybook.index')) return render_template('diarybook/update.html', post=post)
def detail(id): db = get_db() post = db.execute( 'SELECT id, title, body, created, author_id, dirname' ' FROM post' ' WHERE id = ?', (id, )).fetchone() db.commit() tags = db.execute('SELECT name FROM tag WHERE post_id = ?', (id, )).fetchall() db.commit() if (tags): print("show tags:") for item in tags: print(item['name']) if (post): for item in post: print(type(item), item) if (post[4] != g.user['id']): return redirect(url_for('diarybook.index')) return render_template('diarybook/detail.html', post=post, tags=tags) return redirect(url_for('diarybook.index'))
def update(id): print("id = ", id) db = get_db() post = db.execute('SELECT username, password' ' FROM user' ' WHERE id = ?', (id, )).fetchone() db.commit() if (request.method == 'POST'): username = request.form['username'] used = False check = db.execute('SELECT id FROM user WHERE username = ?', (username, )).fetchall() for row in check: for item in row: if (item != id): used = True password = request.form['password'] error = None if not username: error = "username is required" if not password: error = "password is required" if used: error = "username is used by others" if error is not None: flash(error) else: print("username = "******"psw = ", password) db.execute( 'UPDATE user' ' SET username = ?, password = ?' ' WHERE id = ?', (username, generate_password_hash(password), id)) db.commit() return redirect(url_for('diarybook.index')) return render_template('auth/update.html', post=post)