PageRenderTime 82ms CodeModel.GetById 67ms RepoModel.GetById 1ms app.codeStats 0ms

/shabti/templates/microsite/+package+/controllers/tabber.py_tmpl

https://bitbucket.org/gawel/shabti
Unknown | 70 lines | 63 code | 7 blank | 0 comment | 0 complexity | baaccb81b8d1f810c055b67e4b2d8b17 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from pylons import url, cache, session, tmpl_context as c
  4. from pylons.controllers.util import abort, redirect_to
  5. from pylons.i18n import set_lang, get_lang, _, ungettext, N_
  6. from {{package}}.lib.base import BaseController, render
  7. log = logging.getLogger(__name__)
  8. from {{package}} import model as model
  9. from {{package}}.lib.helpers import dcmeta, link_to
  10. from webhelpers.markdown import markdown
  11. class TabberController(BaseController):
  12. def __before__(self):
  13. c.dcmeta = dcmeta(title="Tabber demo")
  14. def tabbedindex(self):
  15. def author_link(author):
  16. try:
  17. return link_to(author.name,
  18. url(controller="tabber",
  19. action='show',
  20. id=author.id))
  21. except Exception:
  22. return _("UNRECORDED")
  23. def get_tabberdata():
  24. sq = model.Session.query(model.Author)
  25. authors = sq.order_by(model.Author.family_name.asc(),
  26. model.Author.given_name.asc())
  27. ntabs = 7
  28. sep = authors.count()/ntabs
  29. tabgroups = [dict(title = "%s - %s" % \
  30. (tgp[0].family_name, tgp[-1].family_name),
  31. group = [author_link(tg) for tg in tgp])
  32. for tgp in [[author
  33. for author in authors.offset(sep*i).limit(sep)]
  34. for i in range(0, ntabs)]]
  35. sq = model.Session.query(model.Page)
  36. rubric = sq.filter_by(slug = u'tabber').one()
  37. divstyle = "min-height:%sem;margin-right:4em;" % int(sep / 3)
  38. # Insert a front page
  39. tabgroups.insert(0,
  40. dict(title=_("Intro"),
  41. group=['''<div style="%s">%s</div>''' % \
  42. (divstyle,
  43. markdown(rubric.content,
  44. # encoding='utf8',
  45. safe_mode = False)),
  46. '''<div><!-- Spacerdiv --></div>''',
  47. '''<div><!-- Spacerdiv --></div>''']))
  48. return tabgroups
  49. c.pre_js = ""
  50. c.title = c.dcmeta.title = _("Tabbed list of items.")
  51. c.dcmeta.description = _("Tabbed list of items (exercise).")
  52. # Cache the result in memory for an hour, recalculate if expired
  53. appcache = cache.get_cache('shabti')
  54. if appcache.has_key('authors_tabgroups'):
  55. c.tabgroups = appcache.get_value('authors_tabgroups')
  56. else:
  57. c.tabgroups = appcache.get_value('authors_tabgroups',
  58. createfunc=get_tabberdata,
  59. type="memory",
  60. expiretime=3600)
  61. # c.tabgroups = get_tabberdata()
  62. return render('widgets/tabindex.mako',
  63. cache_type='memory', cache_expire=1200)