From 53078b3f387216b21bf8ab27636511f4373dc9f6 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 19 Feb 2016 13:29:28 +0100 Subject: * code standards improved --- init_db.py | 16 +++++++++++----- rcal/db.py | 30 +++++++++++++++++++++++------- rcal/model.py | 40 +++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/init_db.py b/init_db.py index f6d2b92..da3e641 100644 --- a/init_db.py +++ b/init_db.py @@ -1,10 +1,16 @@ +import sys + from rcal.db import Session from rcal.model import Base -import sys -session = Session() -if len(sys.argv) > 1 and sys.argv[1] == 'force': - Base.metadata.drop_all(session.get_bind()) +def main(): + session = Session.create() + + if len(sys.argv) > 1 and sys.argv[1] == 'force': + Base.metadata.drop_all(session.get_bind()) + + Base.metadata.create_all(session.get_bind()) -Base.metadata.create_all(session.get_bind()) +if __name__ == '__main__': + main() diff --git a/rcal/db.py b/rcal/db.py index 3e74c4e..dffe98f 100644 --- a/rcal/db.py +++ b/rcal/db.py @@ -1,11 +1,27 @@ +import json +from os import path + from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -import json, __main__ -from os import path -config = json.load( - open(path.join(path.dirname(__main__.__file__), 'conf', 'db.json'))) -engine = create_engine( - "{0[type]}://{0[user]}:{0[pass]}@{0[host]}/{0[name]}?{0[opts]}".format(config)) -Session = sessionmaker(bind=engine) +class Session(object): + + engine = None + + def __init__(self): + config = json.load( + open(path.join(path.dirname( + path.realpath(__file__)), '..', 'conf', 'db.json'))) + db_str = "{0[type]}://{0[user]}:{0[pass]}@{0[host]}/{0[name]}".format( + config) + self.engine = create_engine(db_str) + + def get_maker(self): + return sessionmaker(bind=self.engine) + + @staticmethod + def create(): + session = Session() + maker = session.get_maker() + return maker() diff --git a/rcal/model.py b/rcal/model.py index 014476e..2c09e6c 100644 --- a/rcal/model.py +++ b/rcal/model.py @@ -1,11 +1,13 @@ -from sqlalchemy import Column, ForeignKey, Table +# pylint: disable=too-few-public-methods, invalid-name +from sqlalchemy import Column, ForeignKey from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import backref, relationship, sessionmaker -from sqlalchemy.types import Boolean, Date, Float, Integer, String +from sqlalchemy.orm import relationship +from sqlalchemy.types import Boolean, Date, Integer, String -Base = declarative_base() +BASE = declarative_base() -class Calendar(Base): + +class Calendar(BASE): __tablename__ = 'calendars' uid = Column(String(255), primary_key=True) @@ -24,14 +26,14 @@ class Calendar(Base): 'Category', back_populates='calendars', order_by='Calendar.name') - + entries = relationship( 'Entry', back_populates='calendar', cascade="all", passive_deletes=True, order_by='Entry.begin_date') - + @staticmethod def fetch(uid, session, name=None, url=None): calendar = session.query(Calendar).filter(Calendar.uid == uid).first() @@ -45,7 +47,8 @@ class Calendar(Base): calendar.url = url return calendar -class Entry(Base): + +class Entry(BASE): __tablename__ = 'entries' id = Column(Integer, primary_key=True) @@ -66,7 +69,7 @@ class Entry(Base): 'Calendar', back_populates='entries', order_by='Entry.begin_date') - + @staticmethod def fetch(uid, session): entry = session.query(Entry).filter(Entry.uid == uid).first() @@ -75,11 +78,12 @@ class Entry(Base): session.add(entry) return entry -class Category(Base): + +class Category(BASE): __tablename__ = 'categories' id = Column(Integer, primary_key=True) - name = Column(String(255)) + name = Column(String(255), index=True) calendars = relationship( 'Calendar', @@ -87,5 +91,15 @@ class Category(Base): cascade="all", passive_deletes=True, order_by='Calendar.name') - -__all__ = ['Calendar', 'Entry', 'Category'] + + @staticmethod + def fetch(name, session): + category = session.query(Category).filter( + Category.name == name).first() + if not category: + category = Category() + category.name = name + session.add(category) + return category + +__all__ = ('Calendar', 'Entry', 'Category') -- cgit v1.2.3