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

View File

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

View File

@ -2,9 +2,11 @@
#coding: utf-8
from sqlalchemy import Column, Integer, String, Boolean, Enum
from sqlalchemy.orm import relationship, backref
from models import Base
class Feed(Base):
__tablename__ = 'feed'
@ -19,6 +21,8 @@ class Feed(Base):
html2textcontent = Column(Boolean)
html2textignoreimages = 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):
self.url = url
@ -36,7 +40,7 @@ class Feed(Base):
else:
title = '<unknown>'
last = '<never>'
entries = len(self.entry)
entries = len(self.entries)
url = self.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):
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
from sqlalchemy import Column, Integer, ForeignKey, String, DateTime
from sqlalchemy.orm import relation, backref
from datetime import datetime, timedelta
from random import randint
@ -13,7 +12,6 @@ class Feedinfo(Base):
id = Column(Integer, primary_key=True)
feed_id = Column(Integer, ForeignKey('feed.id'))
feed = relation("Feed", backref=backref('feedinfo', uselist=False))
title = Column(String(255))
link = Column(String(255))
subtitle = Column(String(255))