Beispiel #1
0
from flask.ext.login import LoginManager, current_user
from flask.ext.login import login_user, login_required, logout_user
from flask.ext.sqlalchemy import SQLAlchemy

from sched import config, filters
from sched.forms import AppointmentForm, LoginForm
from sched.models import Appointment, Base, User

app = Flask(__name__)
app.config.from_object(config)

# Use Flask-SQLAlchemy for its engine and session configuration. Load the
# extension, giving it the app object, and override its default Model class
# with the pure SQLAlchemy declarative Base class.
db = SQLAlchemy(app)
db.Model = Base

# Use Flask-Login to track the current user in Flask's session.
login_manager = LoginManager()
login_manager.setup_app(app)
login_manager.login_view = 'login'
login_manager.login_message = 'Please log in to see your appointments.'


@login_manager.user_loader
def load_user(user_id):
    """Hook for Flask-Login to load a User instance from a user ID."""
    return db.session.query(User).get(user_id)


# Load custom Jinja filters from the `filters` module.
Beispiel #2
0
 def configure(self, binder):
   db = SQLAlchemy(self._app)
   db.Model = Base
   binder.bind(SQLAlchemy, to=db, scope=singleton)
Beispiel #3
0
class Jokes(Base):
    """A Joke Item."""
    __tablename__ = 'jokes'

    id = Column(Integer, primary_key=True)
    joke = Column(Text, nullable=False)
    categories = Column(Text)

if __name__ == '__main__':
    import re
    import os
    from flask.ext.sqlalchemy import SQLAlchemy
    from app import app

    db = SQLAlchemy(app)
    db.Model = Base

    static_txt_dir = os.path.join(
        os.path.dirname(
            os.path.abspath(__file__)), 'static/txt')

    for txt_file in os.listdir(static_txt_dir):
        category = txt_file.split('_')[0].title()
        
        for line in open(
            static_txt_dir + '/' + txt_file, 'r'):
            db.session.add(Jokes(
                joke = line.strip('\n'),
                categories = category))
            db.session.commit()
Beispiel #4
0
import os
import sys
from flask import Flask, render_template, url_for
from flask_bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin import Admin
from sqlalchemy.ext.declarative import declarative_base
app = Flask(__name__)
Bootstrap(app)
app.config.from_object('config')
db = SQLAlchemy(app)
db.Model = declarative_base()

########################
# Configure Secret Key #
########################
def install_secret_key(app, filename='secret_key'):
    """Configure the SECRET_KEY from a file
    in the instance directory.

    If the file does not exist, print instructions
    to create it from a shell with a random key,
    then exit.
    """
    filename = os.path.join(app.instance_path, filename)

    try:
        app.config['SECRET_KEY'] = open(filename, 'rb').read()
    except IOError:
        print('Error: No secret key. Create it with:')
        full_path = os.path.dirname(filename)