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