Mercurial > party-buddy-server
changeset 20:44d561c15361
use flask-sqlalchemy
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sun, 02 Jun 2013 11:58:30 +0200 |
| parents | 0d32e35d251d |
| children | 5a036517a96b |
| files | requirements.txt server/__init__.py server/configs/default.py server/configs/testing.py server/database.py server/models.py setup_db.py |
| diffstat | 7 files changed, 38 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/requirements.txt Sun Jun 02 11:32:01 2013 +0200 +++ b/requirements.txt Sun Jun 02 11:58:30 2013 +0200 @@ -2,3 +2,5 @@ numpy scipy flask-classy +flask-sqlalchemy +alembic
--- a/server/__init__.py Sun Jun 02 11:32:01 2013 +0200 +++ b/server/__init__.py Sun Jun 02 11:58:30 2013 +0200 @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import flask +from flask.ext.sqlalchemy import SQLAlchemy import os @@ -9,8 +10,9 @@ app.config.from_object('server.configs.default') if 'PARTYBUDDY_SETTINGS' in os.environ: app.config.from_envvar('PARTYBUDDY_SETTINGS') +db = SQLAlchemy(app) - +from . import models from . import api
--- a/server/configs/default.py Sun Jun 02 11:32:01 2013 +0200 +++ b/server/configs/default.py Sun Jun 02 11:58:30 2013 +0200 @@ -3,7 +3,7 @@ SECRET_KEY = 'sadasdSAdsaDsaDsaDsaDwe#@4$#trg' SALT = 'sadjsqwEdsf4#fREt5rG$#rff#fwEfREf' PERMANENT_SESSION_LIFETIME = timedelta(days=30) -DB = 'partybuddy' +SQLALCHEMY_DB_URI = 'sqlite:////tmp/partybuddy.db' TESTING = False BABEL_DEFAULT_LOCALE = 'pl'
--- a/server/configs/testing.py Sun Jun 02 11:32:01 2013 +0200 +++ b/server/configs/testing.py Sun Jun 02 11:58:30 2013 +0200 @@ -1,3 +1,4 @@ TESTING = True DB = 'partybuddy_test' +SQLALCHEMY_DB_URI = 'sqlite:///:memory:' CSRF_ENABLED = False
--- a/server/database.py Sun Jun 02 11:32:01 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -from sqlalchemy import create_engine -from sqlalchemy.orm import scoped_session, sessionmaker -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.pool import QueuePool -import pymssql - -from . import app - -engine = create_engine('postgresql+psycopg2://postgres@localhost/' + app.config['DB'], - max_overflow=20, - isolation_level='READ UNCOMMITTED') -db_session = scoped_session(sessionmaker(autocommit=False, - autoflush=False, - bind=engine)) - -Base = declarative_base() -Base.query = db_session.query_property() - -def init_db(): - # import all modules here that might define models so that - # they will be registered properly on the metadata. Otherwise - # you will have to import them first before calling init_db() - import server.models - - Base.metadata.create_all(engine) - - # then, load the Alembic configuration and generate the - # version table, "stamping" it with the most recent rev: - from alembic.config import Config - from alembic import command - alembic_cfg = Config("alembic.ini") - command.stamp(alembic_cfg, "head")
--- a/server/models.py Sun Jun 02 11:32:01 2013 +0200 +++ b/server/models.py Sun Jun 02 11:58:30 2013 +0200 @@ -1,10 +1,18 @@ -from sqlalchemy import (Column, Integer, BigInteger, - String, DateTime, Boolean, Sequence, ForeignKey, - Table, desc, Numeric, Float) +from sqlalchemy import ( + Boolean, + Column, + DateTime, + Float, + ForeignKey, + Integer, + Sequence, + String, + Table, +) from sqlalchemy.orm import relationship, backref -from .database import Base from .helpers import gen_filename from . import app +from . import db from datetime import datetime from hashlib import sha256 @@ -24,18 +32,18 @@ def __setattr__(self, name, value): self.__dict__['edition_timestamp'] = datetime.now() - Base.__setattr__(self, name, value) + db.Model.__setattr__(self, name, value) self.__dict__[name] = value def edited(self): self.edition_timestamp = datetime.now() -checkin_user = Table('checkin_user', Base.metadata, +checkin_user = Table('checkin_user', db.Model.metadata, Column('checkin_id', Integer, ForeignKey('checkins.id'), primary_key=True), Column('user_id', Integer, ForeignKey('users.id'), primary_key=True), ) -class User(Boilerplate, Base): +class User(Boilerplate, db.Model): __tablename__ = 'users' id = Column(Integer, Sequence('user_id_seq'), primary_key=True) @@ -89,39 +97,36 @@ return False -class PhoneNumber(Boilerplate, Base): +class PhoneNumber(Boilerplate, db.Model): __tablename__ = 'user_numbers' number = Column(String(20), primary_key=True) - owner_id = Column(Integer, ForeignKey('users.id')) owner = relationship("User", backref=backref('numbers', order_by=id)) -class Mac(Boilerplate, Base): +class Mac(Boilerplate, db.Model): __tablename__ = 'macs' mac = Column(String(16), primary_key=True) owner = ForeignKey(User) - owner_id = Column(Integer, ForeignKey('users.id')) owner = relationship("User", backref=backref('mac_numbers', order_by=id)) -class PanicContact(Boilerplate, Base): +class PanicContact(Boilerplate, db.Model): __tablename__ = 'panic_contacts' id = Column(Integer, Sequence('panic_contact_id_seq'), primary_key=True) name = Column(String()) number = Column(String(20)) comment = Column(String()) - owner_id = Column(Integer, ForeignKey('users.id')) owner = relationship("User", backref=backref('panic_contacts', order_by=id)) -class Checkin(Boilerplate, Base): +class Checkin(Boilerplate, db.Model): __tablename__ = 'checkins' id = Column(Integer, Sequence('checkin_id_seq'), primary_key=True) time = Column(DateTime())
--- a/setup_db.py Sun Jun 02 11:32:01 2013 +0200 +++ b/setup_db.py Sun Jun 02 11:58:30 2013 +0200 @@ -1,8 +1,15 @@ -# coding=utf-8 -from server.database import init_db, db_session -from server import app +#!/usr/bin/env python +# -*- coding: utf-8 -*- -from sqlalchemy.orm.exc import NoResultFound +from server import db + if __name__ == '__main__': - init_db() + db.create_all() + # then, load the Alembic configuration and generate the + # version table, "stamping" it with the most recent rev: + from alembic.config import Config + from alembic import command + alembic_cfg = Config("alembic.ini") + command.stamp(alembic_cfg, "head") +
