~mvngu/igraph/doc

« back to all changes in this revision

Viewing changes to nexus/cgi-bin/url_helper.py

  • Committer: Gabor Csardi
  • Date: 2012-02-21 04:05:12 UTC
  • mfrom: (2640.1.1 0.6-main)
  • Revision ID: csardi.gabor@gmail.com-20120221040512-n2orudv0a2cdw3g1
Merged Nexus branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Helper functions for generating URLs and links.
 
3
"""
 
4
 
 
5
# List of symbols to be exported from this module
 
6
__all__ = [
 
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"
 
9
]
 
10
 
 
11
from web import websafe
 
12
 
 
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
 
16
    is used."""
 
17
    if caption is None:
 
18
        caption = url
 
19
    return '<a href="%s">%s</a>' % (websafe(url), websafe(caption))
 
20
 
 
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
 
25
    the caption."""
 
26
    if caption is None:
 
27
        caption = dataset.name
 
28
    if bullet:
 
29
        caption = u"%s%s" % (bullet, caption)
 
30
    return link_to(url_for_dataset(base, dataset.id), caption)
 
31
 
 
32
def link_to_licence(base, licence, caption=None, bullet=""):
 
33
    if caption is None:
 
34
        caption = ""
 
35
    if bullet:
 
36
        caption = u"%s%s" % (bullet, caption)
 
37
    return link_to(url_for_licence(base, licence), caption)
 
38
 
 
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)
 
43
 
 
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."""
 
48
    if caption is None:
 
49
        caption = tag.tag
 
50
    caption = websafe(caption)
 
51
    return link_to(url_for_tag(base, tag), caption)
 
52
 
 
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):
 
58
        dataset = dataset.id
 
59
    return "%s/api/dataset_info?id=%s&format=%s" % (base, dataset, format)
 
60
 
 
61
def url_for_licence(base, licence, format="html"):
 
62
    return getbase() + "/api/licence?id=%s&format=%s" % (base, 
 
63
                                                         licence, format)
 
64
 
 
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"):
 
70
        tag = tag.tag
 
71
    return "%s/api/dataset_info?tag=%s&format=%s" % (base, tag, format)
 
72