Mercurial > hg > Feedworm
diff backend/sqlalchemy/Mapping.py @ 119:04a730f9d07d backend
move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Sun, 21 Aug 2011 03:55:16 +0200 |
parents | Mapping.py@25fef7c29c5b |
children | bb3c851b18b1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/sqlalchemy/Mapping.py Sun Aug 21 03:55:16 2011 +0200 @@ -0,0 +1,75 @@ + +from Feed import Feed +from FeedEntry import FeedEntry +from Preference import Preference +from sqlalchemy import Boolean +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import ForeignKey +from sqlalchemy import Integer +from sqlalchemy import MetaData +from sqlalchemy import String +from sqlalchemy import Table +from sqlalchemy import Text +from sqlalchemy.orm import mapper +from sqlalchemy.orm import relation + +mappingDefined = False +feedEntryTable = None + +def createMapping(engine): + """ Make sure the mapping is defined only once. This is not really needed for the feed updater + or the GUI app but comes in handy when working interactively with the system. """ + global mappingDefined + if not mappingDefined: + _createMapping(engine) + mappingDefined = True + +def _createMapping(engine): + metadata = MetaData(engine) + metadata.bind = engine + + feedTable = Table("feed", metadata, + Column("pk", Integer, primary_key=True), + Column("title", String(255), nullable=False), + Column("rss_url", String(255), nullable=False), + # update interval is specified in minutes + Column("update_interval", Integer, nullable=False), + Column("next_update", DateTime, nullable=False), + # when displaying an entry of this feed, do not display the summary but rather load + # the link directly + Column("auto_load_entry_link", Boolean, nullable=False), + # this is actually a hack: when opening some sites in the QWebView it just crashes. + # This setting forces to open an entry's link in the external browser + Column("always_open_in_browser", Boolean, nullable=False) + ) + + global feedEntryTable + feedEntryTable = Table("feed_entry", metadata, + Column("pk", Integer, primary_key=True), + Column("create_timestamp", DateTime, nullable=False), + Column("read", Integer, nullable=False), + + Column("id", String(512), nullable=False), + Column("link", String(512), nullable=False), + Column("title", Text, nullable=False), + Column("summary", Text, nullable=False), + Column("updated", DateTime), + Column("feed_id", Integer, ForeignKey("feed.pk")) + ) + + preferencesTable = Table("preference", metadata, + Column("pk", Integer, primary_key=True), + Column("key", String(255), nullable=False), + Column("value", String(255), nullable=False) + ) + + metadata.create_all() + + mapper(FeedEntry, feedEntryTable) + mapper(Feed, feedTable, + properties = { + "entries" : relation(FeedEntry, backref="feed", lazy=True, cascade="delete, delete-orphan") + } + ) + mapper(Preference, preferencesTable)