diff options
Diffstat (limited to 'rcal/model.py')
-rw-r--r-- | rcal/model.py | 40 |
1 files changed, 27 insertions, 13 deletions
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') |