dankventskalender migrate to flask
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

160 rindas
4.6KB

  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. chosen = request.form['chosen']
  91. error = None
  92. if not title:
  93. error = 'Title is required.'
  94. if error is not None:
  95. flash(error)
  96. else:
  97. db = get_db()
  98. db.execute(
  99. 'UPDATE post SET title = ?, body = ?, nickname = ?, chosen = ?'
  100. ' WHERE id = ?',
  101. (title, body, nickname, chosen, id)
  102. )
  103. db.commit()
  104. return redirect(url_for('calender.index'))
  105. return render_template('calender/update.html', post=post)
  106. @bp.route('/<int:id>/delete', methods=('POST',))
  107. @login_required
  108. def delete(id):
  109. get_post(id)
  110. db = get_db()
  111. db.execute('DELETE FROM post WHERE id = ?', (id,))
  112. db.commit()
  113. return redirect(url_for('calender.index'))
  114. def allowed_file(filename):
  115. return '.' in filename and \
  116. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  117. def check_file_type(filename):
  118. if filename.rsplit('.', 1)[1].lower() in ['webm', 'mp4']:
  119. return 'video'
  120. else:
  121. return 'image'
  122. @bp.route('/uploads/<filename>')
  123. def uploaded_file(filename):
  124. return send_from_directory(current_app.config['UPLOAD_FOLDER'],
  125. filename)