author | Daniel Diniz <ajaksu@gmail.com> |
Wed, 08 Jul 2009 10:40:46 +0200 | |
changeset 2570 | 851640749319 |
parent 2404 | 4f87bab3c6b3 |
child 2833 | 5aa891de5aa5 |
permissions | -rw-r--r-- |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
1 |
# Copyright 2009 the Melange authors. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
2 |
# |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
4 |
# you may not use this file except in compliance with the License. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
5 |
# You may obtain a copy of the License at |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
6 |
# |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
8 |
# |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
9 |
# Unless required by applicable law or agreed to in writing, software |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
12 |
# See the License for the specific language governing permissions and |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
13 |
# limitations under the License. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
14 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
15 |
"""The Melange Core module. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
16 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
17 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
18 |
__authors__ = [ |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
19 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
20 |
'"Lennard de Rijk" <ljvderijk@gmail.com>', |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
21 |
] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
22 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
23 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
24 |
from django.conf.urls import defaults |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
25 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
26 |
import settings |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
27 |
import soc.cache.sidebar |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
28 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
29 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
30 |
class Error(Exception): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
31 |
"""Error class for the callback module. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
32 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
33 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
34 |
pass |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
35 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
36 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
37 |
class APIVersionMismatch(Error): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
38 |
"""Error raised when API version mismatches. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
39 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
40 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
41 |
MISMATCH_MSG_FMT = "API mismatch, expected '%d', got '%d'." |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
42 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
43 |
def __init__(self, expected, actual): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
44 |
"""Instantiates a new exception with a customized message. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
45 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
46 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
47 |
msg = self.MISMATCH_MSG_FMT % (expected, actual) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
48 |
super(APIVersionMismatch, self).__init__(msg) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
49 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
50 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
51 |
class MissingService(Error): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
52 |
"""Error raised when a required service is missing. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
53 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
54 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
55 |
MISSING_SERVICE_FMT = "Required service '%s' is not registered, known: %s" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
56 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
57 |
def __init__(self, service, services): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
58 |
"""Instantiates a new exception with a customized message. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
59 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
60 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
61 |
msg = self.MISSING_SERVICE_FMT % (service, services) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
62 |
super(MissingService, self).__init__(msg) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
63 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
64 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
65 |
class MissingCapability(Error): |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
66 |
"""Error raised when a required capability is missing. |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
67 |
""" |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
68 |
|
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
69 |
MISSING_CAPABILITY_FMT = "Required capability '%s' " + \ |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
70 |
"is not registered, known: %s" |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
71 |
|
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
72 |
def __init__(self, capability, capabilities): |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
73 |
"""Instantiates a new exception with a customized message. |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
74 |
""" |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
75 |
|
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
76 |
msg = self.MISSING_CAPABILITY_FMT % (capability, capabilities) |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
77 |
super(MissingCapability, self).__init__(msg) |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
78 |
|
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
79 |
|
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
80 |
class NonUniqueService(Error): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
81 |
"""Error raised when a required service is missing. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
82 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
83 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
84 |
NON_UNIQUE_SERVICE_FMT = "Unique service '%s' called a 2nd time, known: %s." |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
85 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
86 |
def __init__(self, service, services): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
87 |
"""Instantiates a new exception with a customized message. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
88 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
89 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
90 |
msg = self.NON_UNIQUE_SERVICE_FMT % (service, services) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
91 |
super(NonUniqueService, self).__init__(msg) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
92 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
93 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
94 |
class Core(object): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
95 |
"""The core handler that controls the Melange API. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
96 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
97 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
98 |
def __init__(self): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
99 |
"""Creates a new instance of the Core. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
100 |
""" |
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
101 |
|
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
102 |
# pylint: disable-msg=C0103 |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
103 |
self.API_VERSION = 1 |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
104 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
105 |
self.registered_callbacks = [] |
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
106 |
self.capabilities = [] |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
107 |
self.services = [] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
108 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
109 |
self.sitemap = [] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
110 |
self.sidebar = [] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
111 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
112 |
## |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
113 |
## internal |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
114 |
## |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
115 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
116 |
def getService(self, callback, service): |
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
117 |
"""Retrieves the specified service from the callback if supported. |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
118 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
119 |
Args: |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
120 |
callback: the callback to retrieve the capability from |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
121 |
service: the service to retrieve |
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
122 |
""" |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
123 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
124 |
if not hasattr(callback, service): |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
125 |
return False |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
126 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
127 |
func = getattr(callback, service) |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
128 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
129 |
if not callable(func): |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
130 |
return False |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
131 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
132 |
return func |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
133 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
134 |
## |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
135 |
## Core code |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
136 |
## |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
137 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
138 |
def getPatterns(self): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
139 |
"""Returns the Django patterns for this site. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
140 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
141 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
142 |
self.callService('registerWithSitemap', True) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
143 |
return defaults.patterns(None, *self.sitemap) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
144 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
145 |
@soc.cache.sidebar.cache |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
146 |
def getSidebar(self, id, user): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
147 |
"""Constructs a sidebar for the current user. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
148 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
149 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
150 |
self.callService('registerWithSidebar', True) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
151 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
152 |
sidebar = [] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
153 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
154 |
for i in self.sidebar: |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
155 |
menus = i(id, user) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
156 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
157 |
for menu in (menus if menus else []): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
158 |
sidebar.append(menu) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
159 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
160 |
return sorted(sidebar, key=lambda x: x.get('group')) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
161 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
162 |
def callService(self, service, unique, *args, **kwargs): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
163 |
"""Calls the specified service on all callbacks. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
164 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
165 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
166 |
if unique and (service in self.services): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
167 |
return |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
168 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
169 |
results = [] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
170 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
171 |
for callback in self.registered_callbacks: |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
172 |
func = self.getService(callback, service) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
173 |
if not func: |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
174 |
continue |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
175 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
176 |
result = func(*args, **kwargs) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
177 |
results.append(result) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
178 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
179 |
self.services.append(service) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
180 |
return results |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
181 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
182 |
def registerModuleCallbacks(self): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
183 |
"""Retrieves all callbacks for the modules of this site. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
184 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
185 |
Callbacks for modules without a version number or the wrong API_VERSION |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
186 |
number are dropped. They won't be called. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
187 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
188 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
189 |
fmt = settings.MODULE_FMT |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
190 |
modules = ['soc_core'] + settings.MODULES |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
191 |
modules = [__import__(fmt % i, fromlist=['']) for i in modules] |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
192 |
|
2404
4f87bab3c6b3
Remove getCallback indirection in modules
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2359
diff
changeset
|
193 |
for callback_class in [i.Callback for i in modules]: |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
194 |
if callback_class.API_VERSION != self.API_VERSION: |
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
195 |
raise APIVersionMismatch(self.API_VERSION, |
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
196 |
callback_class.API_VERSION) |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
197 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
198 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
199 |
callback = callback_class(self) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
200 |
self.registered_callbacks.append(callback) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
201 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
202 |
return True |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
203 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
204 |
## |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
205 |
## Module code |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
206 |
## |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
207 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
208 |
def registerCapability(self, capability): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
209 |
"""Registers the specified capability. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
210 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
211 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
212 |
self.capabilities.append(capability) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
213 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
214 |
def requireCapability(self, capability): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
215 |
"""Requires that the specified capability is present. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
216 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
217 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
218 |
if capability in self.capabilities: |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
219 |
return True |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
220 |
|
2359
d4de17ab9a1f
Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2333
diff
changeset
|
221 |
raise MissingCapability(capability, self.capabilities) |
2333
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
222 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
223 |
def requireService(self, service): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
224 |
"""Requires that the specified service has been called. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
225 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
226 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
227 |
if service in self.services: |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
228 |
return True |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
229 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
230 |
raise MissingService(service, self.services) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
231 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
232 |
def requireUniqueService(self, service): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
233 |
"""Requires that the specified service is called exactly once. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
234 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
235 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
236 |
if service not in self.services: |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
237 |
return True |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
238 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
239 |
raise NonUniqueService(service, self.services) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
240 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
241 |
def registerSitemapEntry(self, entries): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
242 |
"""Registers the specified entries with the sitemap. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
243 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
244 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
245 |
self.sitemap.extend(entries) |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
246 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
247 |
def registerSidebarEntry(self, entry): |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
248 |
"""Registers the specified entry with the sidebar. |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
249 |
""" |
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
250 |
|
221482a54238
First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff
changeset
|
251 |
self.sidebar.append(entry) |