refactoring of data model

This commit is contained in:
Ronald Schaten 2013-04-07 13:34:36 +02:00
parent 0c6cb0c7f9
commit ea14005ac1
4 changed files with 31 additions and 34 deletions

View File

@ -175,7 +175,7 @@ def process_feed_entry(session, feed, entry):
thisentry.fullpage = hp.unescape(thisentry.fullpage) thisentry.fullpage = hp.unescape(thisentry.fullpage)
if thisentry.readability: if thisentry.readability:
thisentry.readability = hp.unescape(thisentry.readability) thisentry.readability = hp.unescape(thisentry.readability)
feed.entry.append(thisentry) feed.entries.append(thisentry)
session.commit() session.commit()
return 1 return 1
@ -226,7 +226,7 @@ def list_all_feeds(session):
for feed in allfeeds: for feed in allfeeds:
print feed print feed
totalfeeds += 1 totalfeeds += 1
totalentries += len(feed.entry) totalentries += len(feed.entries)
print 'TOTAL: %d entries in %d feeds.' % (totalentries, totalfeeds) print 'TOTAL: %d entries in %d feeds.' % (totalentries, totalfeeds)
def fetch_all_feeds(session): def fetch_all_feeds(session):
@ -243,51 +243,35 @@ def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
ok = raw_input(prompt) ok = raw_input(prompt)
if ok in ('y', 'ye', 'yes'): if ok in ('y', 'ye', 'yes'):
return True return True
if ok in ('n', 'no', 'nop', 'nope'): if ok in ('n', 'no'):
return False return False
retries = retries - 1 retries = retries - 1
if retries < 0: if retries < 0:
return False return False
print complaint print complaint
def delete_feed(session, feed_id): def feed_ask_delete(session, feed_id):
feed = session.query(Feed).\ feed = session.query(Feed).\
filter(Feed.id == feed_id).\ filter(Feed.id == feed_id).\
first() first()
print feed print feed
if ask_ok('> Do you really want to delete feed %d? ' % feed_id): if ask_ok('> Do you really want to delete feed %d? ' % feed.id):
print 'deleting...' print 'deleting...'
entries = session.query(Entry).\
filter(Entry.feed_id == feed_id).\
all()
for entry in entries:
session.delete(entry)
feedinfo = session.query(Feedinfo).\
filter(Feedinfo.feed_id == feed_id).\
first()
session.delete(feedinfo)
session.delete(feed) session.delete(feed)
print '... done.' print '... done.'
def reset_feed(session, feed_id): def feed_ask_reset(session, feed_id):
feed = session.query(Feed).\ feed = session.query(Feed).\
filter(Feed.id == feed_id).\ filter(Feed.id == feed_id).\
first() first()
print feed print feed
if ask_ok('> Do you really want to reset feed %d? ' % feed_id): if ask_ok('> Do you really want to reset feed %d? ' % feed.id):
print 'resetting...' print 'resetting...'
entries = session.query(Entry).\ feed.reset()
filter(Entry.feed_id == feed_id).\
all()
for entry in entries:
session.delete(entry)
feedinfo = session.query(Feedinfo).\
filter(Feedinfo.feed_id == feed_id).\
first()
session.delete(feedinfo)
print '... done.' print '... done.'
if __name__ == '__main__':
def main():
if (sys.stdout.encoding is None): if (sys.stdout.encoding is None):
streamWriter = codecs.lookup('utf-8')[-1] streamWriter = codecs.lookup('utf-8')[-1]
sys.stdout = streamWriter(sys.stdout) sys.stdout = streamWriter(sys.stdout)
@ -335,10 +319,13 @@ if __name__ == '__main__':
if args.list: if args.list:
list_all_feeds(session) list_all_feeds(session)
if args.delete: if args.delete:
delete_feed(session, args.delete) feed_ask_delete(session, args.delete)
if args.reset: if args.reset:
reset_feed(session, args.reset) feed_ask_reset(session, args.reset)
if not (args.fetch or args.single or args.daily or args.list or args.delete or args.reset): if not (args.fetch or args.single or args.daily or args.list or args.delete or args.reset):
parser.print_help() parser.print_help()
session.commit() session.commit()
if __name__ == '__main__':
main()

View File

@ -2,7 +2,6 @@
#coding: utf-8 #coding: utf-8
from sqlalchemy import Column, Integer, ForeignKey, String, Text, DateTime from sqlalchemy import Column, Integer, ForeignKey, String, Text, DateTime
from sqlalchemy.orm import relation, backref
from datetime import datetime from datetime import datetime
from time import mktime from time import mktime
import pprint import pprint
@ -14,7 +13,6 @@ class Entry(Base):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
feed_id = Column(Integer, ForeignKey('feed.id')) feed_id = Column(Integer, ForeignKey('feed.id'))
feed = relation("Feed", backref=backref('entry'))
title = Column(String(255)) title = Column(String(255))
link = Column(String(255)) link = Column(String(255))
summary = Column(Text) summary = Column(Text)
@ -34,8 +32,14 @@ class Entry(Base):
self.update(entry) self.update(entry)
self.firstfetched = datetime.now() self.firstfetched = datetime.now()
def __unicode__(self):
return u'%d -> %s' % (self.id, self.title)
def __str__(self):
return unicode(self).encode('utf-8')
def __repr__(self): def __repr__(self):
return "<Entry('%s','%s','%s')>" % (self.title, "", "") return "<Entry('%d','%s')>" % (self.id, self.title)
def update(self, entry): def update(self, entry):
if entry.has_key('title'): if entry.has_key('title'):

View File

@ -2,9 +2,11 @@
#coding: utf-8 #coding: utf-8
from sqlalchemy import Column, Integer, String, Boolean, Enum from sqlalchemy import Column, Integer, String, Boolean, Enum
from sqlalchemy.orm import relationship, backref
from models import Base from models import Base
class Feed(Base): class Feed(Base):
__tablename__ = 'feed' __tablename__ = 'feed'
@ -19,6 +21,8 @@ class Feed(Base):
html2textcontent = Column(Boolean) html2textcontent = Column(Boolean)
html2textignoreimages = Column(Boolean) html2textignoreimages = Column(Boolean)
enabled = Column(Boolean) enabled = Column(Boolean)
entries = relationship("Entry", backref=backref('feed'), cascade='all, delete, delete-orphan')
feedinfo = relationship("Feedinfo", backref=backref('feed'), cascade='all, delete, delete-orphan', uselist=False)
def __init__(self, url, daily, readability, fullpage, enabled, html2textcontent): def __init__(self, url, daily, readability, fullpage, enabled, html2textcontent):
self.url = url self.url = url
@ -36,7 +40,7 @@ class Feed(Base):
else: else:
title = '<unknown>' title = '<unknown>'
last = '<never>' last = '<never>'
entries = len(self.entry) entries = len(self.entries)
url = self.url url = self.url
return u'%3d %s (%d entries, last fetched %s)\n %s' % (id, title, entries, last, url) return u'%3d %s (%d entries, last fetched %s)\n %s' % (id, title, entries, last, url)
@ -45,3 +49,7 @@ class Feed(Base):
def __repr__(self): def __repr__(self):
return "<Feed('%d','%s')>" % (self.id, self.url) return "<Feed('%d','%s')>" % (self.id, self.url)
def reset(self):
self.entries[:] = []
self.feedinfo = None

View File

@ -2,7 +2,6 @@
#coding: utf-8 #coding: utf-8
from sqlalchemy import Column, Integer, ForeignKey, String, DateTime from sqlalchemy import Column, Integer, ForeignKey, String, DateTime
from sqlalchemy.orm import relation, backref
from datetime import datetime, timedelta from datetime import datetime, timedelta
from random import randint from random import randint
@ -13,7 +12,6 @@ class Feedinfo(Base):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
feed_id = Column(Integer, ForeignKey('feed.id')) feed_id = Column(Integer, ForeignKey('feed.id'))
feed = relation("Feed", backref=backref('feedinfo', uselist=False))
title = Column(String(255)) title = Column(String(255))
link = Column(String(255)) link = Column(String(255))
subtitle = Column(String(255)) subtitle = Column(String(255))