Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from flask import Flask, render_template, redirect
  4. from flask_wtf import FlaskForm
  5. from wtforms.validators import DataRequired, length
  6. from wtforms import TextAreaField
  7. from flask_session.__init__ import Session
  8.  
  9. import sqlite3
  10. import os
  11. import logging
  12.  
  13.  
  14.  
  15. app = Flask(__name__)
  16. app.debug = False
  17.  
  18. logging.basicConfig(level=logging.DEBUG)
  19.  
  20. app.secret_key = 'hdsHjHjadhJK.Jh'
  21.  
  22. SESSION_TYPE = 'filesystem'
  23. app.config.from_object(__name__)
  24. Session(app)
  25.  
  26. curDir = os.path.abspath(os.path.dirname(__file__))
  27.  
  28. class noteForm(FlaskForm):
  29.     note = TextAreaField('Poznámka', validators=[DataRequired(), length(max=250)])
  30.  
  31. @app.route('/', methods=['GET', 'POST'])
  32. def addNote():
  33.     """Zobrazí folrmulář a vloží poznámku."""
  34.     form = noteForm()
  35.     noteText = form.note.data
  36.     app.logger.info(noteText)
  37.     if form.validate_on_submit():
  38.         conn = sqlite3.connect(os.path.join(curDir,'notes.db'))
  39.         c = conn.cursor()
  40.         c.execute("INSERT INTO note(body) VALUES (?)", (noteText,))
  41.         conn.commit()
  42.         conn.close()
  43.         return redirect('/notes')
  44.     return render_template('addNote.html', form=form)
  45.  
  46. @app.route('/notes')
  47. def showNotes():
  48.     """Zobrazí všechny poznamky."""
  49.     conn = sqlite3.connect(os.path.join(curDir,'notes.db'))
  50.     c = conn.cursor()
  51.     notes = c.execute("SELECT rowid, body, kdy FROM note").fetchall()
  52.     #note = c.execute(f"SELECT body FROM note").fetchone()
  53.     conn.close()
  54.     return render_template('showNotes.html', notes=notes)
  55.  
  56.  
  57.  
  58. # <int:poznamka_id> definuje, že v URL bude na konci integer s id (rowid) poznámky
  59. # Viz.: https://www.tutorialspoint.com/flask/flask_variable_rules.htm
  60. @app.route('/del/<int:noteID>')
  61. def deleteNote(noteID):
  62.     """Smaže vybranou poznámku"""
  63.     conn = sqlite3.connect(os.path.join(curDir,'notes.db'))
  64.     c = conn.cursor()
  65.     # Aby nedošlo k útoku SQL injection na vaší aplikaci! Viz. nahoře.
  66.     c.execute("DELETE FROM note WHERE rowid=?", (noteID,))
  67.     conn.commit()
  68.     conn.close()
  69.     return redirect('/notes')
  70.  
  71. if __name__ == '__main__':
  72.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement