dankventskalender migrate to flask
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

163 行
4.7KB

  1. import os
  2. from flask import (
  3. Blueprint, render_template,
  4. flash, request, url_for, redirect, abort, 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', 'webm', 'mp4', 'bmp'}
  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, nickname, file, chosen, type'
  16. ' FROM post p'
  17. ' ORDER BY created DESC'
  18. ).fetchall()
  19. # noinspection PyUnresolvedReferences
  20. return render_template('calender/index.html', posts=posts)
  21. @bp.route('/admin')
  22. @login_required
  23. def admin():
  24. db = get_db()
  25. posts = db.execute(
  26. 'SELECT p.id, title, body, created, nickname, file, chosen, type'
  27. ' FROM post p'
  28. ' ORDER BY created DESC'
  29. ).fetchall()
  30. # noinspection PyUnresolvedReferences
  31. return render_template('calender/admin.html', posts=posts)
  32. @bp.route('/create', methods=('GET', 'POST'))
  33. # @login_required
  34. def create():
  35. if request.method == 'POST':
  36. title = request.form['title']
  37. nickname = request.form['nickname']
  38. body = request.form['body']
  39. # check if the post request has the file part
  40. if 'file' not in request.files:
  41. flash('No file part')
  42. return redirect(request.url)
  43. file = request.files['file']
  44. # if user does not select file, browser also
  45. # submit an empty part without filename
  46. if file.filename == '':
  47. flash('No selected file')
  48. return redirect(request.url)
  49. error = None
  50. if not title:
  51. error = 'Title is required.'
  52. if error is not None:
  53. flash(error)
  54. else:
  55. if file and allowed_file(file.filename):
  56. file_type = check_file_type(file.filename)
  57. filename = secure_filename(file.filename)
  58. file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
  59. db = get_db()
  60. db.execute(
  61. 'INSERT INTO post (title, body, nickname, file, chosen, type)'
  62. ' VALUES (?, ?, ?, ?, ?, ?)',
  63. (title, body, nickname, url_for('calender.uploaded_file',
  64. filename=filename), False, file_type)
  65. )
  66. db.commit()
  67. # return redirect(url_for('calender.uploaded_file',filename=filename))
  68. return redirect(url_for('calender.index'))
  69. return render_template('calender/create.html')
  70. def get_post(id, check_author=True):
  71. post = get_db().execute(
  72. 'SELECT p.id, title, body, created, nickname, file, chosen, type'
  73. ' FROM post p'
  74. ' WHERE p.id = ?',
  75. (id,)
  76. ).fetchone()
  77. if post is None:
  78. abort(404, "Post id {0} doesn't exist.".format(id))
  79. # if check_author and post['author_id'] != g.user['id']:
  80. # abort(403)
  81. return post
  82. @bp.route('/<int:id>/update', methods=('GET', 'POST'))
  83. @login_required
  84. def update(id):
  85. post = get_post(id)
  86. if request.method == 'POST':
  87. title = request.form['title']
  88. nickname = request.form['nickname']
  89. body = request.form['body']
  90. if request.form.get('chosen') is None:
  91. chosen = 0
  92. else:
  93. chosen = 1
  94. error = None
  95. if not title:
  96. error = 'Title is required.'
  97. if error is not None:
  98. flash(error)
  99. else:
  100. db = get_db()
  101. db.execute(
  102. 'UPDATE post SET title = ?, body = ?, nickname = ?, chosen = ?'
  103. ' WHERE id = ?',
  104. (title, body, nickname, chosen, id)
  105. )
  106. db.commit()
  107. return redirect(url_for('calender.index'))
  108. return render_template('calender/update.html', post=post)
  109. @bp.route('/<int:id>/delete', methods=('POST',))
  110. @login_required
  111. def delete(id):
  112. get_post(id)
  113. db = get_db()
  114. db.execute('DELETE FROM post WHERE id = ?', (id,))
  115. db.commit()
  116. return redirect(url_for('calender.index'))
  117. def allowed_file(filename):
  118. return '.' in filename and \
  119. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  120. def check_file_type(filename):
  121. if filename.rsplit('.', 1)[1].lower() in ['webm', 'mp4']:
  122. return 'video'
  123. else:
  124. return 'image'
  125. @bp.route('/uploads/<filename>')
  126. def uploaded_file(filename):
  127. return send_from_directory(current_app.config['UPLOAD_FOLDER'],
  128. filename)