Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from flask import Flask, render_template, redirect
- from flask_wtf import FlaskForm
- from wtforms.validators import DataRequired, length
- from wtforms import TextAreaField
- from flask_session.__init__ import Session
- import sqlite3
- import os
- import logging
- app = Flask(__name__)
- app.debug = False
- logging.basicConfig(level=logging.DEBUG)
- app.secret_key = 'hdsHjHjadhJK.Jh'
- SESSION_TYPE = 'filesystem'
- app.config.from_object(__name__)
- Session(app)
- curDir = os.path.abspath(os.path.dirname(__file__))
- class noteForm(FlaskForm):
- note = TextAreaField('Poznámka', validators=[DataRequired(), length(max=250)])
- @app.route('/', methods=['GET', 'POST'])
- def addNote():
- """Zobrazí folrmulář a vloží poznámku."""
- form = noteForm()
- noteText = form.note.data
- app.logger.info(noteText)
- if form.validate_on_submit():
- conn = sqlite3.connect(os.path.join(curDir,'notes.db'))
- c = conn.cursor()
- c.execute("INSERT INTO note(body) VALUES (?)", (noteText,))
- conn.commit()
- conn.close()
- return redirect('/notes')
- return render_template('addNote.html', form=form)
- @app.route('/notes')
- def showNotes():
- """Zobrazí všechny poznamky."""
- conn = sqlite3.connect(os.path.join(curDir,'notes.db'))
- c = conn.cursor()
- notes = c.execute("SELECT rowid, body, kdy FROM note").fetchall()
- #note = c.execute(f"SELECT body FROM note").fetchone()
- conn.close()
- return render_template('showNotes.html', notes=notes)
- # <int:poznamka_id> definuje, že v URL bude na konci integer s id (rowid) poznámky
- # Viz.: https://www.tutorialspoint.com/flask/flask_variable_rules.htm
- @app.route('/del/<int:noteID>')
- def deleteNote(noteID):
- """Smaže vybranou poznámku"""
- conn = sqlite3.connect(os.path.join(curDir,'notes.db'))
- c = conn.cursor()
- # Aby nedošlo k útoku SQL injection na vaší aplikaci! Viz. nahoře.
- c.execute("DELETE FROM note WHERE rowid=?", (noteID,))
- conn.commit()
- conn.close()
- return redirect('/notes')
- if __name__ == '__main__':
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement