flask-note/README.org
Andros Fenollosa 75c2873ff3 Fix multiuser
2016-09-18 13:03:57 +02:00

2.4 KiB

PyConES16: Flask-note (Clone of Evernote with Flask and Markdown)

Install

Flask

mkdir flask-note
cd flask-note
pip install virtualenv
virtualenv ENV
source ENV/bin/activate
pip install Flask
pip install markdown

SQLite

OS X
brew install sqlite
Ubuntu
sudo apt-get update
sudo apt-get install sqlite3 libsqlite3-dev

Flask_sqlalchemy

pip install flask_sqlalchemy

Create Flask

Create app.py

from flask import Flask
app = Flask(__name__)

def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if 'user_id' not in session:
            session.clear()
            return redirect(url_for('index'))
        return f(*args, **kwargs)
    return decorated_function

@app.route('/')
def hello():
    return 'Hello PyConES!'

if __name__ == "__main__":
    app.run()
python app.py

Open in your browser

http://127.0.0.1:5000/

Create Database

Create database.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), unique=True)
    password = db.Column(db.String(32))

    def __init__(self, email, password):
        self.email = email
        self.password = password

    def __repr__(self):
        return '<User {email}>'.format(email=self.email)


class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), unique=True)
    text = db.Column(db.Text())
    user_id = db.Column(db.Integer)

    def __init__(self, title, text, user_id):
        self.title = title
        self.text = text
        self.user_id = user_id

    def __repr__(self):
        return '<Note {title}>'.format(title=self.title)
python
from database import db
db.create_all()
exit()
sqlite3 database.sqlite
INSERT INTO user VALUES (NULL, 'py@con.es', '2016');
.exit