author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Sun, 19 Oct 2008 16:17:32 +0000 | |
changeset 390 | d12c95ade374 |
parent 372 | 8595c1129c74 |
child 395 | c2cbf8ebe100 |
permissions | -rw-r--r-- |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
2 |
# |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
4 |
# |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
8 |
# |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
10 |
# |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
16 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
17 |
"""Page properties, used to generate sidebar menus, urlpatterns, etc. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
18 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
19 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
22 |
] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
23 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
24 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
25 |
import copy |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
26 |
import re |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
27 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
28 |
from django.conf.urls import defaults |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
29 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
30 |
from python25src import urllib |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
31 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
32 |
from soc.logic import menu |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
33 |
from soc.logic.no_overwrite_sorted_dict import NoOverwriteSortedDict |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
34 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
class Url: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
"""The components of a Django URL pattern. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
38 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
39 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
40 |
def __init__(self, regex, view, kwargs=None, name=None, prefix=''): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
41 |
"""Collects Django urlpatterns info into a simple object. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
42 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
43 |
The arguments to this constructor correspond directly to the items in |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
44 |
the urlpatterns tuple, which also correspond to the parameters of |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
45 |
django.conf.urls.defaults.url(). |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
46 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
47 |
Args: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
48 |
regex: a Django URL regex pattern |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
49 |
view: a Django view, either a string or a callable |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
50 |
kwargs: optional dict of extra arguments passed to the view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
51 |
function as keyword arguments, which is copy.deepcopy()'d; |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
52 |
default is None, which supplies an empty dict {} |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
53 |
name: optional name of the view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
54 |
prefix: optional view prefix |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
self.regex = regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
57 |
self.view = view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
58 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
59 |
if kwargs: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
60 |
self.kwargs = copy.deepcopy(kwargs) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
61 |
else: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
62 |
self.kwargs = {} |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
63 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
64 |
self.name = name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
65 |
self.prefix = prefix |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
66 |
|
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
67 |
def makeDjangoUrl(self, **extra_kwargs): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
68 |
"""Returns a Django url() used by urlpatterns, or None if not a view. |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
69 |
""" |
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
70 |
if not self.view: |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
71 |
return None |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
72 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
73 |
kwargs = copy.deepcopy(self.kwargs) |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
74 |
kwargs.update(extra_kwargs) |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
75 |
return defaults.url(self.regex, self.view, kwargs=kwargs, |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
76 |
name=self.name, prefix=self.prefix) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
77 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
78 |
_STR_FMT = '''%(indent)sregex: %(regex)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
79 |
%(indent)sview: %(view)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
80 |
%(indent)skwargs: %(kwargs)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
81 |
%(indent)sname: %(name)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
82 |
%(indent)sprefix: %(prefix)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
83 |
''' |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
84 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
85 |
def asIndentedStr(self, indent=''): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
86 |
"""Returns an indented string representation useful for logging. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
87 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
88 |
Args: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
89 |
indent: an indentation string that is prepended to each line present |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
90 |
in the multi-line string returned by this method. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
91 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
92 |
return self._STR_FMT % {'indent': indent, 'regex': self.regex, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
93 |
'view': self.view, 'kwargs': self.kwargs, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
94 |
'name': self.name, 'prefix': self.prefix} |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
95 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
96 |
def __str__(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
97 |
"""Returns a string representation useful for logging. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
98 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
99 |
return self.asIndentedStr() |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
100 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
101 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
102 |
class Page: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
103 |
"""An abstraction that combines a Django view with sidebar menu info. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
104 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
105 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
106 |
def __init__(self, url, long_name, short_name=None, selected=False, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
107 |
annotation=None, parent=None, link_url=None, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
108 |
in_breadcrumb=True, force_in_menu=False): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
109 |
"""Initializes the menu item attributes from supplied arguments. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
110 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
111 |
Args: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
112 |
url: a Url object |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
113 |
long_name: title of the Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
114 |
short_name: optional menu item and breadcrumb name; default is |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
115 |
None, in which case long_name is used |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
116 |
selected: Boolean indicating if this menu item is selected; |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
117 |
default is False |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
118 |
annotation: optional annotation associated with the menu item; |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
119 |
default is None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
120 |
parent: optional Page that is the logical "parent" of this page; |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
121 |
used to construct hierarchical menus and breadcrumb trails |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
122 |
link_url: optional alternate URL link; if supplied, it is returned |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
123 |
by makeLinkUrl(); default is None, and makeLinkUrl() attempts to |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
124 |
create a URL link from url.regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
125 |
in_breadcrumb: if True, the Page appears in breadcrumb trails; |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
126 |
default is True |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
127 |
force_in_menu: if True, the Page appears in menus even if it does |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
128 |
not have a usable link_url; default is False, which excludes |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
129 |
the Page if makeLinkUrl() returns None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
130 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
131 |
self.url = url |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
132 |
self.long_name = long_name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
133 |
self.annotation = annotation |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
134 |
self.in_breadcrumb = in_breadcrumb |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
135 |
self.force_in_menu = force_in_menu |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
136 |
self.link_url = link_url |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
137 |
self.selected = selected |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
138 |
self.parent = parent |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
139 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
140 |
# create ordered, unique mappings of URLs and view names to Pages |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
141 |
self.child_by_urls = NoOverwriteSortedDict() |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
142 |
self.child_by_views = NoOverwriteSortedDict() |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
143 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
144 |
if not short_name: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
145 |
short_name = long_name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
146 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
147 |
self.short_name = short_name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
148 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
149 |
if parent: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
150 |
# tell parent Page about parent <- child relationship |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
151 |
parent.addChild(self) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
152 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
153 |
# TODO(tlarsen): build some sort of global Page dictionary to detect |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
154 |
# collisions sooner and to make global queries from URLs to pages |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
155 |
# and views to Pages possible (without requiring a recursive search) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
156 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
157 |
def getChildren(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
158 |
"""Returns an iterator over any child Pages |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
159 |
""" |
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
160 |
for page in self.child_by_views.itervalues(): |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
161 |
yield page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
162 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
163 |
children = property(getChildren) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
164 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
165 |
def getChild(self, url=None, regex=None, view=None, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
166 |
name=None, prefix=None, request_path=None): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
167 |
"""Returns a child Page if one can be identified; or None otherwise. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
168 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
169 |
All of the parameters to this method are optional, but at least one |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
170 |
must be supplied to return anything other than None. The parameters |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
171 |
are tried in the order they are listed in the "Args:" section, and |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
172 |
this method exits on the first "match". |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
173 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
174 |
Args: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
175 |
url: a Url object, used to overwrite the regex, view, name, and |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
176 |
prefix parameters if present; default is None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
177 |
regex: a regex pattern string, used to return the associated |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
178 |
child Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
179 |
view: a view string, used to return the associated child Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
180 |
name: a name string, used to return the associated child Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
181 |
prefix: (currently unused, see TODO below in code) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
182 |
request_path: optional HTTP request path string (request.path) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
183 |
with no query arguments |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
184 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
185 |
# this code is yucky; there is probably a better way... |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
186 |
if url: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
187 |
regex = url.regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
188 |
view = url.view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
189 |
name = url.name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
190 |
prefix = url.prefix |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
191 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
192 |
if regex in self.child_by_urls: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
193 |
# regex supplied and Page found, so return that Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
194 |
return self.child_by_urls[regex][0] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
195 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
196 |
# TODO(tlarsen): make this work correctly with prefixes |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
197 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
198 |
if view in self.child_views: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
199 |
# view supplied and Page found, so return that Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
200 |
return self.child_by_views[view] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
201 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
202 |
if name in self.child_views: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
203 |
# name supplied and Page found, so return that Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
204 |
return self.child_by_views[name] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
205 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
206 |
if request_path.startswith('/'): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
207 |
request_path = request_path[1:] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
208 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
209 |
# attempt to match the HTTP request path with a Django URL pattern |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
210 |
for pattern, (page, regex) in self.child_by_urls: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
211 |
if regex.match(request_path): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
212 |
return page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
213 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
214 |
return None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
215 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
216 |
def addChild(self, page): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
217 |
"""Adds a unique Page as a child Page of this parent. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
218 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
219 |
Raises: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
220 |
ValueError if page.url.regex is not a string. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
221 |
ValueError if page.url.view is not a string. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
222 |
ValueError if page.url.name is supplied but is not a string. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
223 |
KeyError if page.url.regex is already associated with another Page. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
224 |
KeyError if page.url.view/name is already associated with another Page. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
225 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
226 |
# TODO(tlarsen): see also TODO in __init__() about global Page dictionary |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
227 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
228 |
url = page.url |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
229 |
|
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
230 |
if url.regex: |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
231 |
if not isinstance(url.regex, basestring): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
232 |
raise ValueError('"regex" must be a string, not a compiled regex') |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
233 |
|
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
234 |
# TODO(tlarsen): see if Django has some way exposed in its API to get |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
235 |
# the view name from the request path matched against urlpatterns; |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
236 |
# if so, there would be no need for child_by_urls, because the |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
237 |
# request path could be converted for us by Django into a view/name, |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
238 |
# and we could just use child_by_views with that string instead |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
239 |
self.child_by_urls[url.regex] = (page, re.compile(url.regex)) |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
240 |
# else: NonUrl does not get indexed by regex, because it has none |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
241 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
242 |
# TODO(tlarsen): make this work correctly if url has a prefix |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
243 |
# (not sure how to make this work with include() views...) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
244 |
if url.name: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
245 |
if not isinstance(url.name, basestring): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
246 |
raise ValueError('"name" must be a string if it is supplied') |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
247 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
248 |
view = url.name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
249 |
elif isinstance(url.view, basestring): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
250 |
view = url.view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
251 |
else: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
252 |
raise ValueError('"view" must be a string if "name" is not supplied') |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
253 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
254 |
self.child_by_views[view] = page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
255 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
256 |
def delChild(self, url=None, regex=None, view=None, name=None, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
257 |
prefix=None): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
258 |
"""Removes a child Page if one can be identified. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
259 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
260 |
All of the parameters to this method are optional, but at least one |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
261 |
must be supplied in order to remove a child Page. The parameters |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
262 |
are tried in the order they are listed in the "Args:" section, and |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
263 |
this method uses the first "match". |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
264 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
265 |
Args: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
266 |
url: a Url object, used to overwrite the regex, view, name, and |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
267 |
prefix parameters if present; default is None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
268 |
regex: a regex pattern string, used to remove the associated |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
269 |
child Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
270 |
view: a view string, used to remove the associated child Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
271 |
name: a name string, used to remove the associated child Page |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
272 |
prefix: (currently unused, see TODO below in code) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
273 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
274 |
Raises: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
275 |
KeyError if the child Page could not be definitively identified in |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
276 |
order to delete it. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
277 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
278 |
# this code is yucky; there is probably a better way... |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
279 |
if url: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
280 |
regex = url.regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
281 |
view = url.view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
282 |
name = url.name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
283 |
prefix = url.prefix |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
284 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
285 |
# try to find page by regex, view, or name, in turn |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
286 |
if regex in self.child_by_urls: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
287 |
url = self.child_by_urls[regex][0].url |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
288 |
view = url.view |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
289 |
name = url.name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
290 |
prefix = url.prefix |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
291 |
elif view in self.child_views: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
292 |
# TODO(tlarsen): make this work correctly with prefixes |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
293 |
regex = self.child_by_views[view].url.regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
294 |
elif name in self.child_views: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
295 |
regex = self.child_by_views[name].url.regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
296 |
|
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
297 |
if regex: |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
298 |
# regex must refer to an existing Page at this point |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
299 |
del self.child_urls[regex] |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
300 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
301 |
if not isinstance(view, basestring): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
302 |
# use name if view is callable() or None, etc. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
303 |
view = name |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
304 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
305 |
# TODO(tlarsen): make this work correctly with prefixes |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
306 |
del self.child_by_views[view] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
307 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
308 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
309 |
def makeLinkUrl(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
310 |
"""Makes a URL link suitable for <A HREF> use. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
311 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
312 |
Returns: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
313 |
self.link_url if link_url was supplied to the __init__() constructor |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
314 |
and it is a non-False value |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
315 |
-OR- |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
316 |
a suitable URL extracted from the url.regex, if possible |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
317 |
-OR- |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
318 |
None if url.regex contains quotable characters that have not already |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
319 |
been quoted (that is, % is left untouched, so quote suspect |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
320 |
characters in url.regex that would otherwise be quoted) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
321 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
322 |
if self.link_url: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
323 |
return self.link_url |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
324 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
325 |
link = self.url.regex |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
326 |
|
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
327 |
if not link: |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
328 |
return None |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
329 |
|
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
330 |
if link.startswith('^'): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
331 |
link = link[1:] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
332 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
333 |
if link.endswith('$'): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
334 |
link = link[:-1] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
335 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
336 |
if not link.startswith('/'): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
337 |
link = '/' + link |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
338 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
339 |
# path separators and already-quoted characters are OK |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
340 |
if link != urllib.quote(link, safe='/%'): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
341 |
return None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
342 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
343 |
return link |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
344 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
345 |
def makeMenuItem(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
346 |
"""Returns a menu.MenuItem for the Page (and any child Pages). |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
347 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
348 |
child_items = [] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
349 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
350 |
for child in self.children: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
351 |
child_item = child.makeMenuItem() |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
352 |
if child_item: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
353 |
child_items.append(child_item) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
354 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
355 |
if child_items: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
356 |
sub_menu = menu.Menu(items=child_items) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
357 |
else: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
358 |
sub_menu = None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
359 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
360 |
link_url = self.makeLinkUrl() |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
361 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
362 |
if (not sub_menu) and (not link_url) and (not self.force_in_menu): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
363 |
# no sub-menu, no valid link URL, and not forced to be in menu |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
364 |
return None |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
365 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
366 |
return menu.MenuItem( |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
367 |
self.short_name, value=link_url, sub_menu=sub_menu) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
368 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
369 |
def makeDjangoUrl(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
370 |
"""Returns the Django url() for the underlying self.url. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
371 |
""" |
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
372 |
return self.url.makeDjangoUrl(page=self) |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
373 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
374 |
def makeDjangoUrls(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
375 |
"""Returns an ordered mapping of unique Django url() objects. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
376 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
377 |
Raises: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
378 |
KeyError if more than one Page has the same urlpattern. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
379 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
380 |
TODO(tlarsen): this really needs to be detected earlier via a |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
381 |
global Page dictionary |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
382 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
383 |
return self._makeDjangoUrlsDict().values() |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
384 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
385 |
def _makeDjangoUrlsDict(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
386 |
"""Returns an ordered mapping of unique Django url() objects. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
387 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
388 |
Used to implement makeDjangoUrls(). See that method for details. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
389 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
390 |
urlpatterns = NoOverwriteSortedDict() |
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
391 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
392 |
django_url = self.makeDjangoUrl() |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
393 |
|
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
394 |
if django_url: |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
395 |
urlpatterns[self.url.regex] = django_url |
281
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
396 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
397 |
for child in self.children: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
398 |
urlpatterns.update(child._makeDjangoUrlsDict()) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
399 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
400 |
return urlpatterns |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
401 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
402 |
_STR_FMT = '''%(indent)slong_name: %(long_name)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
403 |
%(indent)sshort_name: %(short_name)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
404 |
%(indent)sselected: %(selected)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
405 |
%(indent)sannotation: %(annotation)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
406 |
%(indent)surl: %(url)s |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
407 |
''' |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
408 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
409 |
def asIndentedStr(self, indent=''): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
410 |
"""Returns an indented string representation useful for logging. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
411 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
412 |
Args: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
413 |
indent: an indentation string that is prepended to each line present |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
414 |
in the multi-line string returned by this method. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
415 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
416 |
strings = [ |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
417 |
self._STR_FMT % {'indent': indent, 'long_name': self.long_name, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
418 |
'short_name': self.short_name, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
419 |
'selected': self.selected, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
420 |
'annotation': self.annotation, |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
421 |
'url': self.url.asIndentedStr(indent + ' ')}] |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
422 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
423 |
for child in self.children: |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
424 |
strings.extend(child.asIndentedStr(indent + ' ')) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
425 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
426 |
return ''.join(strings) |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
427 |
|
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
428 |
def __str__(self): |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
429 |
"""Returns a string representation useful for logging. |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
430 |
""" |
7caa8951cbc9
Url and Page classes used to encapsulate the "site map" information (URL
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
431 |
return self.asIndentedStr() |
372
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
432 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
433 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
434 |
class NonUrl(Url): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
435 |
"""Placeholder for when a site-map entry is not a linkable URL. |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
436 |
""" |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
437 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
438 |
def __init__(self, name): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
439 |
"""Creates a non-linkable Url placeholder. |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
440 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
441 |
Args: |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
442 |
name: name of the non-view placeholder |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
443 |
""" |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
444 |
Url.__init__(self, None, None, name=name) |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
445 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
446 |
def makeDjangoUrl(self, **extra_kwargs): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
447 |
"""Always returns None, since NonUrl is never a Django view. |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
448 |
""" |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
449 |
return None |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
450 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
451 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
452 |
class NonPage(Page): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
453 |
"""Placeholder for when a site-map entry is not a displayable page. |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
454 |
""" |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
455 |
|
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
456 |
def __init__(self, non_url_name, long_name, **page_kwargs): |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
457 |
""" |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
458 |
""" |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
459 |
non_url = NonUrl(non_url_name) |
8595c1129c74
Formalize the concept of a NonPage that can appear in the site-map, useful for
Todd Larsen <tlarsen@google.com>
parents:
281
diff
changeset
|
460 |
Page.__init__(self, non_url, long_name, **page_kwargs) |