Depend on wtforms.
[zzz-floof.git] / floof / lib / helpers.py
1 # -*- coding: utf-8 -*-
2 """Helper functions
3
4 Consists of functions to typically be used within templates, but also
5 available to Controllers. This module is available to both as 'h'.
6 """
7 # Import helpers as desired, or define your own, ie:
8 # from webhelpers.html.tags import checkbox, password
9 from webhelpers import *
10 from routes import url_for, redirect_to
11 from pylons import url
12
13 # Scaffolding helper imports
14 from webhelpers.html.tags import *
15 from webhelpers.html import literal
16 from webhelpers.pylonslib import Flash
17 import sqlalchemy.types as types
18 flash = Flash()
19 # End of.
20
21 def get_object_or_404(model, **kw):
22 from pylons.controllers.util import abort
23 """
24 Returns object, or raises a 404 Not Found is object is not in db.
25 Uses elixir-specific `get_by()` convenience function (see elixir source:
26 http://elixir.ematia.de/trac/browser/elixir/trunk/elixir/entity.py#L1082)
27 Example: user = get_object_or_404(model.User, id = 1)
28 """
29 obj = model.get_by(**kw)
30 if obj is None:
31 abort(404)
32 return obj
33
34 def get_comment_owner_url(**route):
35 """Given a view route, returns the owner_url route parameter for generating
36 comment URLs.
37 """
38 if 'owner_url' in route:
39 # We're already in a comments page. Reuse the existing owner URL
40 return route['owner_url']
41
42 # url() returns URLs beginning with a slash. We just need to strip it.
43 return url(**route)[1:]