Mercurial > hg > Feedworm
annotate backend/sqlalchemy/Mapping.py @ 155:a05719a6175e
move common functionality into an abstract backend class, have both backends inherit from it. Implement enough of the couchdb backend that reading feeds (and marking feed entries as read) is possible
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Sat, 27 Aug 2011 08:52:03 +0200 |
parents | 04a730f9d07d |
children | bb3c851b18b1 |
rev | line source |
---|---|
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
1 |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
2 from Feed import Feed |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
3 from FeedEntry import FeedEntry |
43
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
4 from Preference import Preference |
47
a8442c3487b5
add an option to Feed that allows loading an entry's link right away instead of displaying a feed's summary
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
43
diff
changeset
|
5 from sqlalchemy import Boolean |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
6 from sqlalchemy import Column |
5
bfd47f55d85b
add the updated date of the feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
4
diff
changeset
|
7 from sqlalchemy import DateTime |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
8 from sqlalchemy import ForeignKey |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
9 from sqlalchemy import Integer |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
10 from sqlalchemy import MetaData |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
11 from sqlalchemy import String |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
12 from sqlalchemy import Table |
30
ee1432a91141
map the potentially long columns as Text (CLOB) not as String (VARCHAR)
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
29
diff
changeset
|
13 from sqlalchemy import Text |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
14 from sqlalchemy.orm import mapper |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
15 from sqlalchemy.orm import relation |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
16 |
34
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
17 mappingDefined = False |
68
525a52169f60
Make FeedEntry table object available in Mapping to use it to expire old, read feed entries.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
54
diff
changeset
|
18 feedEntryTable = None |
34
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
19 |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
20 def createMapping(engine): |
34
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
21 """ Make sure the mapping is defined only once. This is not really needed for the feed updater |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
22 or the GUI app but comes in handy when working interactively with the system. """ |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
23 global mappingDefined |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
24 if not mappingDefined: |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
25 _createMapping(engine) |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
26 mappingDefined = True |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
27 |
5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
30
diff
changeset
|
28 def _createMapping(engine): |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
29 metadata = MetaData(engine) |
68
525a52169f60
Make FeedEntry table object available in Mapping to use it to expire old, read feed entries.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
54
diff
changeset
|
30 metadata.bind = engine |
102
25fef7c29c5b
remove all feed entries when removing a feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
79
diff
changeset
|
31 |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
32 feedTable = Table("feed", metadata, |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
33 Column("pk", Integer, primary_key=True), |
7 | 34 Column("title", String(255), nullable=False), |
35
aaec263f07ca
Feeds manage the point in time when the next update should happen. FeedUpdater only updates feeds that are due.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
34
diff
changeset
|
35 Column("rss_url", String(255), nullable=False), |
79
d11c3f71ac40
Make update interval editable via the feed's settings dialog.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
77
diff
changeset
|
36 # update interval is specified in minutes |
35
aaec263f07ca
Feeds manage the point in time when the next update should happen. FeedUpdater only updates feeds that are due.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
34
diff
changeset
|
37 Column("update_interval", Integer, nullable=False), |
47
a8442c3487b5
add an option to Feed that allows loading an entry's link right away instead of displaying a feed's summary
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
43
diff
changeset
|
38 Column("next_update", DateTime, nullable=False), |
102
25fef7c29c5b
remove all feed entries when removing a feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
79
diff
changeset
|
39 # when displaying an entry of this feed, do not display the summary but rather load |
47
a8442c3487b5
add an option to Feed that allows loading an entry's link right away instead of displaying a feed's summary
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
43
diff
changeset
|
40 # the link directly |
77
d292ab61ed6f
Add another setting to feed: when opening a feed entry in browser, you can force opening it in an external browser now. This is because some sites just crash the QWebView.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
76
diff
changeset
|
41 Column("auto_load_entry_link", Boolean, nullable=False), |
d292ab61ed6f
Add another setting to feed: when opening a feed entry in browser, you can force opening it in an external browser now. This is because some sites just crash the QWebView.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
76
diff
changeset
|
42 # this is actually a hack: when opening some sites in the QWebView it just crashes. |
d292ab61ed6f
Add another setting to feed: when opening a feed entry in browser, you can force opening it in an external browser now. This is because some sites just crash the QWebView.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
76
diff
changeset
|
43 # This setting forces to open an entry's link in the external browser |
d292ab61ed6f
Add another setting to feed: when opening a feed entry in browser, you can force opening it in an external browser now. This is because some sites just crash the QWebView.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
76
diff
changeset
|
44 Column("always_open_in_browser", Boolean, nullable=False) |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
45 ) |
102
25fef7c29c5b
remove all feed entries when removing a feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
79
diff
changeset
|
46 |
68
525a52169f60
Make FeedEntry table object available in Mapping to use it to expire old, read feed entries.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
54
diff
changeset
|
47 global feedEntryTable |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
48 feedEntryTable = Table("feed_entry", metadata, |
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
49 Column("pk", Integer, primary_key=True), |
6
87317ba41816
add a creation date for the feed entry
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
5
diff
changeset
|
50 Column("create_timestamp", DateTime, nullable=False), |
29
74481aa49974
add a read flag for feed entries so they can be marked read
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
7
diff
changeset
|
51 Column("read", Integer, nullable=False), |
6
87317ba41816
add a creation date for the feed entry
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
5
diff
changeset
|
52 |
76
f909550683b0
some links/ids can be very long. give the link/id column more space
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
68
diff
changeset
|
53 Column("id", String(512), nullable=False), |
f909550683b0
some links/ids can be very long. give the link/id column more space
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
68
diff
changeset
|
54 Column("link", String(512), nullable=False), |
30
ee1432a91141
map the potentially long columns as Text (CLOB) not as String (VARCHAR)
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
29
diff
changeset
|
55 Column("title", Text, nullable=False), |
ee1432a91141
map the potentially long columns as Text (CLOB) not as String (VARCHAR)
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
29
diff
changeset
|
56 Column("summary", Text, nullable=False), |
5
bfd47f55d85b
add the updated date of the feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
4
diff
changeset
|
57 Column("updated", DateTime), |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
58 Column("feed_id", Integer, ForeignKey("feed.pk")) |
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
59 ) |
102
25fef7c29c5b
remove all feed entries when removing a feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
79
diff
changeset
|
60 |
43
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
61 preferencesTable = Table("preference", metadata, |
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
62 Column("pk", Integer, primary_key=True), |
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
63 Column("key", String(255), nullable=False), |
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
64 Column("value", String(255), nullable=False) |
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
65 ) |
2
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
66 |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
67 metadata.create_all() |
8a624ee48a74
First skeleton for sqlalchemy: define the mapping and create the first feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
68 |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
69 mapper(FeedEntry, feedEntryTable) |
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
70 mapper(Feed, feedTable, |
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
71 properties = { |
102
25fef7c29c5b
remove all feed entries when removing a feed
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
79
diff
changeset
|
72 "entries" : relation(FeedEntry, backref="feed", lazy=True, cascade="delete, delete-orphan") |
4
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
73 } |
e0199f383442
retrieve a feed for the given URL, store entries as feed_entry rows into the database
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
2
diff
changeset
|
74 ) |
43
12ed8b5fa08c
first system preference: configure app to stat maximized.
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
35
diff
changeset
|
75 mapper(Preference, preferencesTable) |