dankventskalender migrate to flask
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.0KB

  1. import os
  2. from flask import (
  3. Blueprint, render_template,
  4. flash, request, url_for, redirect, abort, g, send_from_directory, current_app)
  5. from werkzeug.utils import secure_filename
  6. from calender.auth import login_required
  7. from calender.db import get_db
  8. bp = Blueprint('calender', __name__)
  9. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
  10. #Todo admin interface for managing submit, comments, submission date
  11. @bp.route('/')
  12. def index():
  13. db = get_db()
  14. posts = db.execute(
  15. 'SELECT p.id, title, body, created, author_id, username, nickname, file'
  16. ' FROM post p JOIN user u ON p.author_id = u.id'
  17. ' ORDER BY created DESC'
  18. ).fetchall()
  19. # noinspection PyUnresolvedReferences
  20. return render_template('calender/index.html', posts=posts)
  21. @bp.route('/create', methods=('GET', 'POST'))
  22. @login_required
  23. def create():
  24. if request.method == 'POST':
  25. title = request.form['title']
  26. nickname = request.form['nickname']
  27. body = request.form['body']
  28. # check if the post request has the file part
  29. if 'file' not in request.files:
  30. flash('No file part')
  31. return redirect(request.url)
  32. file = request.files['file']
  33. # if user does not select file, browser also
  34. # submit an empty part without filename
  35. if file.filename == '':
  36. flash('No selected file')
  37. return redirect(request.url)
  38. error = None
  39. if not title:
  40. error = 'Title is required.'
  41. if error is not None:
  42. flash(error)
  43. else:
  44. if file and allowed_file(file.filename):
  45. filename = secure_filename(file.filename)
  46. file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
  47. db = get_db()
  48. db.execute(
  49. 'INSERT INTO post (title, body, author_id, nickname, file)'
  50. ' VALUES (?, ?, ?, ?, ?)',
  51. (title, body, g.user['id'], nickname, url_for('calender.uploaded_file',
  52. filename=filename))
  53. )
  54. db.commit()
  55. # return redirect(url_for('calender.uploaded_file',filename=filename))
  56. return redirect(url_for('calender.index'))
  57. return render_template('calender/create.html')
  58. def get_post(id, check_author=True):
  59. post = get_db().execute(
  60. 'SELECT p.id, title, body, created, author_id, username, nickname, file'
  61. ' FROM post p JOIN user u ON p.author_id = u.id'
  62. ' WHERE p.id = ?',
  63. (id,)
  64. ).fetchone()
  65. if post is None:
  66. abort(404, "Post id {0} doesn't exist.".format(id))
  67. if check_author and post['author_id'] != g.user['id']:
  68. abort(403)
  69. return post
  70. @bp.route('/<int:id>/update', methods=('GET', 'POST'))
  71. @login_required
  72. def update(id):
  73. post = get_post(id)
  74. if request.method == 'POST':
  75. title = request.form['title']
  76. body = request.form['body']
  77. error = None
  78. if not title:
  79. error = 'Title is required.'
  80. if error is not None:
  81. flash(error)
  82. else:
  83. db = get_db()
  84. db.execute(
  85. 'UPDATE post SET title = ?, body = ?'
  86. ' WHERE id = ?',
  87. (title, body, id)
  88. )
  89. db.commit()
  90. return redirect(url_for('calender.index'))
  91. return render_template('calender/update.html', post=post)
  92. @bp.route('/<int:id>/delete', methods=('POST',))
  93. @login_required
  94. def delete(id):
  95. get_post(id)
  96. db = get_db()
  97. db.execute('DELETE FROM post WHERE id = ?', (id,))
  98. db.commit()
  99. return redirect(url_for('calender.index'))
  100. def allowed_file(filename):
  101. return '.' in filename and \
  102. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  103. @bp.route('/uploads/<filename>')
  104. def uploaded_file(filename):
  105. return send_from_directory(current_app.config['UPLOAD_FOLDER'],
  106. filename)