diff --git a/oilarchive/controllers.py b/oilarchive/controllers.py index 9a3996b..52eb4c2 100644 --- a/oilarchive/controllers.py +++ b/oilarchive/controllers.py @@ -1,46 +1,76 @@ +# -*- Mode: Python; -*- +# -*- coding: utf-8 -*- +# vi:si:et:sw=2:sts=2:ts=2 + from turbogears import controllers, expose -# from model import * +from model import * from turbogears import identity, redirect from cherrypy import request, response -# from oilarchive import json # import logging # log = logging.getLogger("oilarchive.controllers") +# from oilarchive import json +import oilcache + + +class View: + @expose(template=".templates.view") + def view(self, item): + return dict(item = item) + + def icon(self, item): + response.headerMap['Content-Type'] = "image/png" + return oilcache.icon(item) + + @expose() + def default(self, id, *args, **kw): + try: + item = ArchiveItem.byHash(id) + if not args: + return self.view(item) + elif args[0] == 'icon.png': + return self.icon(item) + elif args[0] == 'json': + return item.json + + except: + raise redirect("/") + + class Root(controllers.RootController): - @expose(template="oilarchive.templates.welcome") - # @identity.require(identity.in_group("admin")) - def index(self): - import time - # log.debug("Happy TurboGears Controller Responding For Duty") - return dict(now=time.ctime()) + @expose(template=".templates.welcome") + # @identity.require(identity.in_group("admin")) + def index(self): + import time + # log.debug("Happy TurboGears Controller Responding For Duty") + return dict(now=time.ctime()) - @expose(template="oilarchive.templates.login") - def login(self, forward_url=None, previous_url=None, *args, **kw): + @expose(template=".templates.login") + def login(self, forward_url=None, previous_url=None, *args, **kw): + if not identity.current.anonymous \ + and identity.was_login_attempted() \ + and not identity.get_identity_errors(): + raise redirect(forward_url) - if not identity.current.anonymous \ - and identity.was_login_attempted() \ - and not identity.get_identity_errors(): - raise redirect(forward_url) + forward_url=None + previous_url= request.path - forward_url=None - previous_url= request.path + if identity.was_login_attempted(): + msg=_("The credentials you supplied were not correct or " + "did not grant access to this resource.") + elif identity.get_identity_errors(): + msg=_("You must provide your credentials before accessing " + "this resource.") + else: + msg=_("Please log in.") + forward_url= request.headers.get("Referer", "/") + + response.status=403 + return dict(message=msg, previous_url=previous_url, logging_in=True, + original_parameters=request.params, + forward_url=forward_url) - if identity.was_login_attempted(): - msg=_("The credentials you supplied were not correct or " - "did not grant access to this resource.") - elif identity.get_identity_errors(): - msg=_("You must provide your credentials before accessing " - "this resource.") - else: - msg=_("Please log in.") - forward_url= request.headers.get("Referer", "/") - - response.status=403 - return dict(message=msg, previous_url=previous_url, logging_in=True, - original_parameters=request.params, - forward_url=forward_url) - - @expose() - def logout(self): - identity.current.logout() - raise redirect("/") + @expose() + def logout(self): + identity.current.logout() + raise redirect("/") diff --git a/oilarchive/json.py b/oilarchive/json.py index d62d43d..f7a5b55 100644 --- a/oilarchive/json.py +++ b/oilarchive/json.py @@ -1,3 +1,6 @@ +# -*- Mode: Python; -*- +# -*- coding: utf-8 -*- +# vi:si:et:sw=2:sts=2:ts=2 # A JSON-based API(view) for your app. # Most rules would look like: # diff --git a/oilarchive/model.py b/oilarchive/model.py index 16fdb70..edac434 100644 --- a/oilarchive/model.py +++ b/oilarchive/model.py @@ -1,10 +1,52 @@ +# -*- Mode: Python; -*- +# -*- coding: utf-8 -*- +# vi:si:et:sw=2:sts=2:ts=2 + from datetime import datetime + from turbogears.database import PackageHub from sqlobject import * from turbogears import identity hub = PackageHub("oilarchive") __connection__ = hub + + +class ArchiveItem(SQLObject): + hash = UnicodeCol(alternateId = True) + title = UnicodeCol() + description = UnicodeCol() + author = UnicodeCol() + text = UnicodeCol() #Fulltext + url = UnicodeCol() + downloadURL = UnicodeCol() + icon = UnicodeCol() + releaseDate = DateTimeCol() + pubDate = DateTimeCol() + size = IntCol() + rights = IntCol() #-> int: 0 (free) - 5 (unfree) + archiveName = UnicodeCol() + archiveType = UnicodeCol() + + def _get_year(self): + return self.releaseDate.strftime('%Y') + + def _get_json(self): + return dict( + title = self.title, + description = self.description, + author = self.author, + url = self.url, + icon = '/view/%s/icon' % self.hash, + releaseDate = self.releaseDate, + pubDate = self.pubDate, + size = self.size, + ) + +def Archive(SQLObject): + archiveName = UnicodeCol() + url = UnicodeCol() + archiveType = UnicodeCol() # identity models. class Visit(SQLObject): diff --git a/oilarchive/oilcache.py b/oilarchive/oilcache.py new file mode 100644 index 0000000..4b0f60e --- /dev/null +++ b/oilarchive/oilcache.py @@ -0,0 +1,35 @@ +# -*- Mode: Python; -*- +# -*- coding: utf-8 -*- +# vi:si:et:sw=2:sts=2:ts=2 + +import os +from os.path import abspath, exists, join + +from scrapeit.utils import read_url + +cache_root = join(abspath(__file__), 'cache') + + +def load_file(f_name): + f = open(f_name) + data = f.read() + f.close() + return data + +def save_file(f_name, data): + f = open(f_name, 'w') + f.write(data) + f.close() + +def icon(item): + icon_root = join(cache_root, 'icon') + if not exists(icon_root): + os.makedirs(icon_root) + icon = join(icon_root, "%s.png" % item.hash) + if exists(icon): + data = laod_file(icon) + else: + data = read_url(item.icon) + save_file(icon, data) + return data + diff --git a/oilarchive/release.py b/oilarchive/release.py index b735895..0d81ae1 100644 --- a/oilarchive/release.py +++ b/oilarchive/release.py @@ -2,11 +2,11 @@ version = "1.0" -# description = "Your plan to rule the world" -# long_description = "More description about your plan" -# author = "Your Name Here" -# email = "YourEmail@YourDomain" -# copyright = "Vintage 2006 - a good year indeed" +description="Oil21 Archive", +author="Oil21", +author_email="info@oil21.org", +url="http://oil21.org", +copyright = "no copyright 2007" # if it's open source, you might want to specify these # url = "http://yourcool.site/" diff --git a/oilarchive/templates/view.kid b/oilarchive/templates/view.kid new file mode 100644 index 0000000..ecca92f --- /dev/null +++ b/oilarchive/templates/view.kid @@ -0,0 +1,17 @@ + + +
+ +