""" this contains the server-side logic for suus.pragmagik.com """ # some config vars (should be moved to a seperate file later) class Config(object): def __init__(self): self.DEFAULT_VIEW = 'room' self.VIEWS = [('room', 'room.html', get_room_context), ('details', 'details.html', get_painting_details_context), ('news', 'static.html', get_news_context), ('about', 'static.html', get_about_context), ('help', 'static.html', get_help_context), ] self.BASEPATH = '/projects/suus.pragmagik.com' import py import sys import image quote = py.std.urllib.quote here = py.magic.autopath().dirpath() # set PYTHONPATH for Templess sys.path.insert(0, here.dirpath().strpath) from templess import templess config = None def get_config(): global config if config is None: config = Config() return config def get_page_info(name): for vname, vtemplate, vhandler in get_config().VIEWS: if vname == name: return vtemplate, vhandler def get_room_nav_context(form): rooms = [r.path for r in get_all_rooms()] for room in rooms: yield { 'name': room.purebasename, 'href': '%s/?page=room&room=%s' % (config.BASEPATH, quote(room.basename)), } def get_page_context(pagename, form): """ get the content for a page finds a template and handler for the page, builds the template with the content (templess context dict) returned by the handler, then returns a(nother templess context) dict for interpolation into the main macro note that the handlers usually don't do much: most of the logic for the application is handled on the client side """ tfilename, handler = get_page_info(pagename) template = templess.template((here.join('templates') / tfilename).read()) node = template.render(handler(form)) head = list(node.find('head'))[0] body = list(node.find('body'))[0] return { 'head': list(head), 'body': list(body), 'rooms': get_room_nav_context(form), } def index(form): """ present a page, defaulting to 'room' """ page = form.getvalue('page', get_config().DEFAULT_VIEW) thtml = here.join('templates/index.html').read() template = templess.template(thtml) context = get_page_context(page, form) yield ('\n') for chunk in template.generate(context): yield chunk # page handlers def get_all_paintings(): ret = [image.Painting(p) for p in here.join('paintings').listdir() if p.check(file=True) and not p.basename.startswith('.')] ret.sort(key=lambda p: p.path.basename) ret.sort(key=lambda p: -int(p.path.propget('page:weight').strip() or 0)) return ret def get_all_rooms(): ret = [image.Room(p) for p in here.join('rooms').listdir() if p.check(file=True) and not p.basename.startswith('.')] ret.sort(key=lambda p: p.path.basename) ret.sort(key=lambda p: -int(p.path.propget('page:weight').strip() or 0)) return ret def get_paintingmenu_context(paintings, hreftemplate, onclicktemplate=None, usehrefforroom=True): config = get_config() for p in paintings: basename = p.path.basename href = hreftemplate % { 'basepath': config.BASEPATH, 'painting': quote(basename), } hrefforroom = href if not usehrefforroom: hrefforroom = '%s/?page=room&painting=%s' % (config.BASEPATH, quote(basename),) d = { 'src': ('%s/cgi/painting.cgi?painting=%s&' 'size=70' % (config.BASEPATH, quote(basename))), 'title': p.path.propget('img:title').strip(), 'dimensions': p.path.propget('img:dimensions').strip(), 'hrefdetails': '%s/?page=details&painting=%s' % (config.BASEPATH, quote(basename)), 'href': href, 'hrefroom': hrefforroom, } if onclicktemplate: d['onclick'] = onclicktemplate % { 'painting': basename, 'title': d['title'], 'dimensions': d['dimensions'], } else: d['onclick'] = None yield d def get_paintingmenu(paintings, hreftemplate, onclicktemplate=None, usehrefforroom=True): context = { 'thumbnails': get_paintingmenu_context(paintings, hreftemplate, onclicktemplate, usehrefforroom), 'pbarstyle': 'width: %spx' % ((len(paintings) * 92) + 1), } template = templess.template(here.join('templates/paintingmenu.html')) return list(template.render(context).find('div'))[0] def get_room_context(form): paintings = get_all_paintings() rooms = get_all_rooms() config = get_config() cgipath = '%s/cgi' % (config.BASEPATH,) roombasename = form.getvalue('room', rooms[0].path.basename) paintingbasename = form.getvalue('painting', paintings[0].path.basename) room = image.Room(here.join('rooms', roombasename)) painting = image.Painting(here.join('paintings', paintingbasename)) paintingmenu = get_paintingmenu(paintings, ('%%(basepath)s/?page=room&room=%s&' 'painting=%%(painting)s' % ( roombasename,)), ('change_room(event, "%s", "%%(painting)s", ' '"%%(title)s", "%%(dimensions)s"); return false' % (roombasename,))) return {'roomsrc': '%s/room.cgi?room=%s&painting=%s' % ( cgipath, quote(roombasename), quote(paintingbasename)), 'roomtitle': 'room "%s" with painting "%s"' % ( room.path.propget('img:title').strip(), painting.path.propget('img:title').strip()), 'paintingtitle': painting.path.propget('img:title').strip(), 'paintingdimensions': painting.path.propget('img:dimensions').strip(), 'paintingmenu': paintingmenu, } def get_painting_details_context(form): paintings = get_all_paintings() config = get_config() cgipath = '%s/cgi' % (config.BASEPATH,) paintingbasename = form.getvalue('painting', paintings[0].path.basename) painting = image.Painting(here.join('paintings', paintingbasename)) paintingmenu = get_paintingmenu(paintings, ('%(basepath)s/?page=details&' 'painting=%(painting)s'), usehrefforroom=False) return { 'paintingmenu': paintingmenu, 'paintingsrc': '%s/painting.cgi?painting=%s&size=400' % ( cgipath, quote(paintingbasename),), 'onclick': ('window.open("%s/painting.cgi?painting=%s"); ' 'return false;') % (cgipath, quote(paintingbasename)), 'href': '%s/painting.cgi?painting=%s' % ( cgipath, quote(paintingbasename)), 'paintingtitle': painting.path.propget('img:title').strip(), 'paintingdimensions': painting.path.propget('img:dimensions').strip(), } def get_news_context(form): html = here.join('html/news.html').read() content = list(list(templess.template(html).tree.find('body'))[0]) return {'content': content} def get_about_context(form): html = here.join('html/about.html').read() content = list(list(templess.template(html).tree.find('body'))[0]) return {'content': content} def get_help_context(form): html = here.join('html/help.html').read() content = list(list(templess.template(html).tree.find('body'))[0]) return {'content': content}