Stop using pylons.h.
[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 config, 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 from floof.lib import file_storage as storage
22
23 def get_object_or_404(model, **kw):
24 from pylons.controllers.util import abort
25 """
26 Returns object, or raises a 404 Not Found is object is not in db.
27 Uses elixir-specific `get_by()` convenience function (see elixir source:
28 http://elixir.ematia.de/trac/browser/elixir/trunk/elixir/entity.py#L1082)
29 Example: user = get_object_or_404(model.User, id = 1)
30 """
31 obj = model.get_by(**kw)
32 if obj is None:
33 abort(404)
34 return obj
35
36 def get_comment_owner_url(**route):
37 """Given a view route, returns the owner_url route parameter for generating
38 comment URLs.
39 """
40 if 'owner_url' in route:
41 # We're already in a comments page. Reuse the existing owner URL
42 return route['owner_url']
43
44 # url() returns URLs beginning with a slash. We just need to strip it.
45 return url(**route)[1:]
46
47 def storage_url(prefix, identifier):
48 """Returns a URL for the given object-in-storage."""
49 return storage.get_path(prefix, identifier)