1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Functions for composing Model entity key names. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Todd Larsen" <tlarsen@google.com>', |
|
22 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
23 ] |
|
24 |
|
25 |
|
26 from soc.logic import path_link_name |
|
27 |
|
28 |
|
29 class Error(Exception): |
|
30 """Base class for all exceptions raised by this module.""" |
|
31 pass |
|
32 |
|
33 |
|
34 def nameDocument(scope_path, link_id=None): |
|
35 """Returns a Document key name constructed from a path and link ID. |
|
36 |
|
37 Args: |
|
38 scope_path: the first portion of the path to the Document that uniquely |
|
39 identifies it |
|
40 link_id: optional link ID to append to path (when omitted, |
|
41 scope_path is actually the entire path, with the link_id already |
|
42 appended) |
|
43 |
|
44 Raises: |
|
45 Error if scope_path and link_id produce a "False" path (None, |
|
46 empty string, etc.) |
|
47 """ |
|
48 path = [scope_path] |
|
49 |
|
50 if link_id: |
|
51 path.append(link_id) |
|
52 |
|
53 path = path_link_name.combinePath(path) |
|
54 |
|
55 if not path: |
|
56 raise Error('"path" must be non-False: "%s"' % path) |
|
57 |
|
58 return 'Document:%s' % path |
|
59 |
|
60 |
|
61 def nameSiteSettings(scope_path, link_id): |
|
62 """Returns a SiteSettings key name constructed from a supplied path. |
|
63 |
|
64 Raises: |
|
65 Error if path is "False" (None, empty string, etc.) |
|
66 """ |
|
67 |
|
68 if not scope_path: |
|
69 raise Error('"scope_path" must be non-False: "%s"' % scope_path) |
|
70 |
|
71 if not link_id: |
|
72 raise Error('"link_id" must be non-False: "%s"' % link_id) |
|
73 |
|
74 return 'SiteSettings:%s:%s' % (scope_path, link_id) |
|
75 |
|
76 |
|
77 def nameHomeSettings(scope_path, link_id): |
|
78 """Returns a HomeSettings key name constructed from a supplied path. |
|
79 |
|
80 Raises: |
|
81 Error if path is "False" (None, empty string, etc.) |
|
82 """ |
|
83 |
|
84 if not scope_path: |
|
85 raise Error('"scope_path" must be non-False: "%s"' % scope_path) |
|
86 |
|
87 if not link_id: |
|
88 raise Error('"link_id" must be non-False: "%s"' % link_id) |
|
89 |
|
90 return 'HomeSettings:%s:%s' % (scope_path, link_id) |
|
91 |
|
92 |
|
93 def nameUser(email): |
|
94 """Returns a User key name constructed from a supplied email address. |
|
95 |
|
96 Raises: |
|
97 Error if email is "False" (None, empty string, etc.) |
|
98 """ |
|
99 if not email: |
|
100 raise Error('"email" must be non-False: "%s"' % email) |
|
101 |
|
102 return 'User:%s' % email |
|
103 |
|
104 |
|
105 def nameSponsor(link_id): |
|
106 """Returns a Sponsor key name constructed from a supplied link ID. |
|
107 |
|
108 Raises: |
|
109 Error if link_id is "False" (None, empty string, etc.) |
|
110 """ |
|
111 if not link_id: |
|
112 raise Error('"link_id" must be non-False: "%s"' % link_id) |
|
113 |
|
114 return 'Group/Sponsor:%s' % link_id |
|
115 |
|
116 |
|
117 def nameSchool(sponsor_ln, program_ln, link_id): |
|
118 """Returns a School key name constructed from link IDs. |
|
119 |
|
120 Args: |
|
121 sponsor_ln: Sponsor link ID |
|
122 program_ln: Program link ID |
|
123 link_id: School link ID |
|
124 |
|
125 Raises: |
|
126 Error if sponsor_ln, program_ln, and link_id combine to produce |
|
127 a "False" path (None, empty string, etc.) |
|
128 """ |
|
129 path = path_link_name.combinePath([[sponsor_ln, program_ln], link_id]) |
|
130 |
|
131 if not path: |
|
132 raise Error('"path" must be non-False: "%s"' % path) |
|
133 |
|
134 return 'Group/School:%s' % path |
|
135 |
|
136 |
|
137 def nameOrganization(sponsor_ln, program_ln, link_id): |
|
138 """Returns a Organization key name constructed from link IDs. |
|
139 |
|
140 Args: |
|
141 sponsor_ln: Sponsor link ID |
|
142 program_ln: Program link ID |
|
143 link_id: Organization link ID |
|
144 |
|
145 Raises: |
|
146 Error if sponsor_ln, program_ln, and link_id combine to produce |
|
147 a "False" path (None, empty string, etc.) |
|
148 """ |
|
149 path = path_link_name.combinePath([[sponsor_ln, program_ln], link_id]) |
|
150 |
|
151 if not path: |
|
152 raise Error('"path" must be non-False: "%s"' % path) |
|
153 |
|
154 return 'Group/Organization:%s' % path |
|
155 |
|
156 |
|
157 def nameClub(link_id): |
|
158 """Returns a Club key name constructed from a supplied link ID. |
|
159 |
|
160 Raises: |
|
161 Error if link_id is "False" (None, empty string, etc.) |
|
162 """ |
|
163 if not link_id: |
|
164 raise Error('"link_id" must be non-False: "%s"' % link_id) |
|
165 |
|
166 return 'Group/Club:%s' % link_id |
|
167 |
|
168 |
|
169 def nameWork(link_id): |
|
170 """Placeholder for work namer. |
|
171 """ |
|
172 |
|
173 if not link_id: |
|
174 raise Error('"link_id" must be non-False: "%s"' % link_id) |
|
175 |
|
176 return 'Work:%s' % link_id |
|
177 |
|
178 |
|
179 def nameHost(sponsor_ln, user_ln): |
|
180 """Placeholder for host namer. |
|
181 """ |
|
182 |
|
183 if not sponsor_ln: |
|
184 raise Error('"sponsor_ln" must be non-False: "%s"' % sponsor_ln) |
|
185 |
|
186 if not user_ln: |
|
187 raise Error('"user_ln" must be non-False: "%s"' % user_ln) |
|
188 |
|
189 return 'Host:%s:%s' % (sponsor_ln, user_ln) |
|