comparison backend/couchdb/Feed.py @ 147:b290e29a94b5

use couchdb's mapping API instead of manually coding around Rows - much leaner code :-)
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 25 Aug 2011 11:05:05 +0200
parents 74217db92993
children 7d724cf2dcf7
comparison
equal deleted inserted replaced
146:8ec20377bcb0 147:b290e29a94b5
1 1
2 from couchdb.mapping import BooleanField, DateTimeField, Document, IntegerField, TextField
2 from datetime import datetime, timedelta 3 from datetime import datetime, timedelta
3 4
4 DATE_FORMAT = "%Y-%m-%d %H:%M:%S" 5 class Feed(Document):
6 doctype = TextField(default="feed")
7 title = TextField()
8 rss_url = TextField()
9 update_interval = IntegerField(default=60)
10 next_update = DateTimeField()
11 auto_load_entry_link = BooleanField(default=False)
12 always_open_in_browser = BooleanField(default=False)
5 13
6 class Feed(object):
7 @staticmethod 14 @staticmethod
8 def all(database): 15 def all(database):
9 viewResults = database.view("feedtest/feeds") 16 return Feed.view(database, "feedtest/feeds")
10 return [Feed(row) for row in viewResults]
11
12 def __init__(self, row):
13 self.row = row
14
15 def __getattr__(self, key):
16 return self.row.value[key]
17 17
18 def needsUpdate(self): 18 def needsUpdate(self):
19 updateDate = self._nextUpdateDate() 19 delta = datetime.now() - self.next_update
20 delta = datetime.now() - updateDate
21 return delta.total_seconds() > self._updateIntervalInSeconds() 20 return delta.total_seconds() > self._updateIntervalInSeconds()
22 21
23 def incrementedUpdateDate(self): 22 def incrementNextUpdateDate(self):
24 updateDate = self._nextUpdateDate()
25 updateInterval = self._updateIntervalInSeconds() 23 updateInterval = self._updateIntervalInSeconds()
26 delta = timedelta(seconds=updateInterval) 24 delta = timedelta(seconds=updateInterval)
27 return updateDate + delta 25 self.next_update = self.next_update + delta
28
29 def _nextUpdateDate(self):
30 nextUpdateString = self.next_update
31 return datetime.strptime(nextUpdateString, DATE_FORMAT)
32 26
33 def _updateIntervalInSeconds(self): 27 def _updateIntervalInSeconds(self):
34 return self.update_interval * 60 28 return self.update_interval * 60