2
Helper functions for generating URLs and links.
5
# List of symbols to be exported from this module
7
"link_to", "link_to_dataset", "link_to_dataset_id", "link_to_tag",
8
"link_to_licence", "url_for_dataset", "url_for_tag", "url_for_licence"
11
from web import websafe
13
def link_to(url, caption):
14
"""Generates a link to the given URL. `caption` is optional and may
15
specify the caption of the URL. If `caption` is ``None``, the URL itself
19
return '<a href="%s">%s</a>' % (websafe(url), websafe(caption))
21
def link_to_dataset(base, dataset, caption=None, bullet=""):
22
"""Generates a link to the given dataset. `caption` is optional and may
23
specify the caption of the URL. If `caption` is ``None``, the name of
24
the dataset is used. `bullet` specifies the bullet to be shown before
27
caption = dataset.name
29
caption = u"%s%s" % (bullet, caption)
30
return link_to(url_for_dataset(base, dataset.id), caption)
32
def link_to_licence(base, licence, caption=None, bullet=""):
36
caption = u"%s%s" % (bullet, caption)
37
return link_to(url_for_licence(base, licence), caption)
39
def link_to_dataset_id(base, dataset_id, caption):
40
"""Generates a link to the dataset with the given ID. `caption` specifies
41
the caption of the URL."""
42
return link_to(url_for_dataset(base, dataset_id), caption)
44
def link_to_tag(base, tag, caption=None):
45
"""Generates a link to the page of the given tag. `caption` is optional
46
and may specify the caption of the URL. If `caption` is ``None``, the
47
tag itself is used."""
50
caption = websafe(caption)
51
return link_to(url_for_tag(base, tag), caption)
53
def url_for_dataset(base, dataset, format="html"):
54
"""Returns the URL where a given dataset is to be found.
55
`dataset` is the ID of the dataset or the dataset itself, `format`
56
is the format of the dataset."""
57
if not isinstance(dataset, int):
59
return "%s/api/dataset_info?id=%s&format=%s" % (base, dataset, format)
61
def url_for_licence(base, licence, format="html"):
62
return getbase() + "/api/licence?id=%s&format=%s" % (base,
65
def url_for_tag(base, tag, format="html"):
66
"""Returns the URL where the list of datasets with a given tag are
67
to be found. `tag` is the tag itself or any object with a ``tag``
68
attribute). `format` is the desired format of the result."""
69
if hasattr(tag, "tag"):
71
return "%s/api/dataset_info?tag=%s&format=%s" % (base, tag, format)