48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
The models in use
|
|
"""
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Column, Integer, String, Text, DateTime, create_engine, Boolean
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker, scoped_session
|
|
|
|
BaseModel = declarative_base()
|
|
db_session = None
|
|
|
|
|
|
class StickyNote(BaseModel):
|
|
"""
|
|
The main (only?) table in the system
|
|
"""
|
|
__tablename__ = u'sticky_notes'
|
|
|
|
id = Column(Integer, autoincrement=True, primary_key=True)
|
|
title = Column(String(255))
|
|
source = Column(Text)
|
|
lexer = Column(String(255), default=u'text')
|
|
created = Column(DateTime, default=datetime.now, index=True)
|
|
expiry = Column(DateTime, default=None, index=True)
|
|
url = Column(String(255), index=True)
|
|
private = Column(Boolean, default=False, index=True)
|
|
|
|
|
|
def init_db(database_url):
|
|
"""
|
|
Initialise the database connection
|
|
|
|
:param database_url: The database connection URL
|
|
"""
|
|
global db_session
|
|
engine = create_engine(database_url, pool_recycle=3600)
|
|
db_session = scoped_session(sessionmaker(bind=engine))()
|
|
BaseModel.metadata.create_all(engine, checkfirst=True)
|
|
|
|
|
|
def get_session():
|
|
"""
|
|
Get the current database session
|
|
"""
|
|
return db_session
|