From 2a6ec2987ce1649d5efb25ed2955fec96d80b743 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 3 Apr 2007 10:02:29 +0000 Subject: [PATCH] - expand urls, to allow relative urls in html / icon / download urls - new fuction to clean up archives that remove items --- oilarchive/model.py | 32 +++++++++++++++++++++++++++++++- oilarchive/oilspider.py | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/oilarchive/model.py b/oilarchive/model.py index 5a908c5..503599b 100644 --- a/oilarchive/model.py +++ b/oilarchive/model.py @@ -79,6 +79,17 @@ class ArchiveItem(SQLObject): def _get_year(self): return self.relDate.strftime('%Y') + + #expand urls in case they are relative to the archive + def _get_archiveUrl(self): + return self.archive.full_url(self._SO_get_archiveUrl()) + + def _get_downloadUrl(self): + return self.archive.full_url(self._SO_get_downloadUrl()) + + def _get_icon(self): + return self.archive.full_url(self._SO_get_icon()) + def _get_json(self): result = jsonify_sqlobject(self) result['relDate'] = self.relDate.strftime('%s') @@ -105,7 +116,7 @@ class ArchiveItem(SQLObject): for key in data: setattr(self, key, data[key]) self.updateHashID() - + def updateHashID(self): salt = '%s/%s/%s' % (self.archive.archiveName, self.author, self.title) self.hashID = md5.new(salt).hexdigest() @@ -134,6 +145,14 @@ class Archive(SQLObject): def data_url(self, id): return self._query_url({'id': id}) + def full_url(self, url): + if url.find('://') > 0: + return url + if url.startswith('/'): + url = "%s/%s" % (self.archiveUrl.split('/')[0], url) + else: + url = "%s/%s" % (self.archiveUrl, url) + def update(self): result = simplejson.loads(read_url(self.update_url)) items = result.get('items', []) @@ -147,6 +166,17 @@ class Archive(SQLObject): else: q[0].update(data) + ''' + get list of all items from archive and remove those from ArchiveItem that + are no longer in the list + ''' + def cleanUp(self): + url = self._query_url({'modDate': -1}) + result = simplejson.loads(read_url(url)) + archiveItems = result.get('items', []) + archivedItems = [i.archiveItemId for i in ArchiveItem.select(ArchiveItem.q.archiveID == self.id)] + removeItems = filter(lambda i: i not in archiveItems, archivedItems) + for i in removeItems: ArchiveItem.delete(i) class SortName(SQLObject): name =UnicodeCol(length=1000, alternateID=True) diff --git a/oilarchive/oilspider.py b/oilarchive/oilspider.py index ed8e64c..2e2c97a 100644 --- a/oilarchive/oilspider.py +++ b/oilarchive/oilspider.py @@ -26,6 +26,7 @@ def jsonImportArchiveItem(archive, archiveItemId, json_array): json_array = jsonLoadArchiveItem(json_array) salt = '%s/%s/%s' % (archive.archiveName, json_array['author'], json_array['title']) hashID = md5.new(salt).hexdigest() + i = model.ArchiveItem( archiveID=archive.id, hashId = hashID, @@ -46,4 +47,3 @@ def jsonImportArchiveItem(archive, archiveItemId, json_array): itemType=json_array['itemType'], icon= json_array['icon'] ) -