annotate backend/couchdb/Feed.py @ 141:6ea813cfac33

pull out common code for updating a feed into an abstract class, have the sqlalchemy backend use that class.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 24 Aug 2011 10:53:46 +0200
parents 2cd30af937fa
children 74217db92993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
139
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
1
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
2 from datetime import datetime
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
3
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
4 DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
136
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
5
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
6 class Feed(object):
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
7 @staticmethod
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
8 def all(database):
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
9 viewResults = database.view("feedtest/feeds")
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
10 return [Feed(row) for row in viewResults]
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
11
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
12 def __init__(self, row):
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
13 self.row = row
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
14
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
15 def __getattr__(self, key):
7217b060b39c implement a Feed class that can be used to query feed and that wraps view results
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
16 return self.row.value[key]
139
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
17
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
18 def needsUpdate(self):
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
19 updateDate = self._nextUpdateDate()
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
20 delta = datetime.now() - updateDate
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
21 return delta.total_seconds() > self._updateIntervalInSeconds()
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
22
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
23 def _nextUpdateDate(self):
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
24 nextUpdateString = self.next_update
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
25 return datetime.strptime(nextUpdateString, DATE_FORMAT)
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
26
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
27 def _updateIntervalInSeconds(self):
2cd30af937fa add the required methods for determining if a feed needs to be updated
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 136
diff changeset
28 return self.update_interval * 60