app/soc/modules/core.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 12 Sep 2009 00:59:58 +0200
changeset 2899 691735924efb
parent 2849 c0462a1d3958
child 2908 391d57b91615
permissions -rw-r--r--
Fix broken maintenance mode Maintenance mode has been broken since r7fe19ebfe0d3 (Thu Jun 4) "Display caught errors on the page itself", fix it by passing the site entity, rather than the site module to isActivePeriod.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
    24
import logging
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
    25
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    26
from django.conf.urls import defaults
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    27
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    28
import settings
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    29
import soc.cache.sidebar
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    30
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    31
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    32
class Error(Exception):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    33
  """Error class for the callback module.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    34
  """
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
  pass
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    37
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    38
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    39
class APIVersionMismatch(Error):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    40
  """Error raised when API version mismatches.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    41
  """
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
  MISMATCH_MSG_FMT = "API mismatch, expected '%d', got '%d'."
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    44
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    45
  def __init__(self, expected, actual):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    46
    """Instantiates a new exception with a customized message.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    47
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    48
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    49
    msg = self.MISMATCH_MSG_FMT % (expected, actual)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    50
    super(APIVersionMismatch, self).__init__(msg)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    51
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    52
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    53
class MissingService(Error):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    54
  """Error raised when a required service is missing.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    55
  """
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
  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
    58
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    59
  def __init__(self, service, services):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    60
    """Instantiates a new exception with a customized message.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    61
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    62
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    63
    msg = self.MISSING_SERVICE_FMT % (service, services)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    64
    super(MissingService, self).__init__(msg)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    65
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    66
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    67
class MissingCapability(Error):
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    68
  """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
    69
  """
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    70
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    71
  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
    72
      "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
    73
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    74
  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
    75
    """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
    76
    """
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    77
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    78
    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
    79
    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
    80
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    81
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    82
class NonUniqueService(Error):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    83
  """Error raised when a required service is missing.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    84
  """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    85
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
    86
  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
    87
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    88
  def __init__(self, service, services):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    89
    """Instantiates a new exception with a customized message.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    90
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    91
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    92
    msg = self.NON_UNIQUE_SERVICE_FMT % (service, services)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    93
    super(NonUniqueService, self).__init__(msg)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    94
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    95
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    96
class Core(object):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    97
  """The core handler that controls the Melange API.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    98
  """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
    99
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   100
  def __init__(self):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   101
    """Creates a new instance of the Core.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   102
    """
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   103
    
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   104
    # pylint: disable-msg=C0103
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   105
    self.API_VERSION = 1
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   106
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   107
    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
   108
    self.capabilities = []
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   109
    self.services = []
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   110
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   111
    self.sitemap = []
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   112
    self.sidebar = []
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   113
    self.per_request_cache = {}
2849
c0462a1d3958 Add self.in_request to Core and verify correct state
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2833
diff changeset
   114
    self.in_request = False
2333
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
  ##
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   117
  ## internal
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   118
  ##
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   119
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   120
  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
   121
    """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
   122
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   123
    Args:
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   124
     callback: the callback to retrieve the capability from
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   125
     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
   126
    """
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   127
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   128
    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
   129
      return False
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   130
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   131
    func = getattr(callback, service)
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   132
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   133
    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
   134
      return False
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   135
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   136
    return func
2333
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
  ##
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   139
  ## Core code
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
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   142
  def startNewRequest(self, request):
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   143
    """Prepares core to handle a new request.
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   144
    """
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   145
2849
c0462a1d3958 Add self.in_request to Core and verify correct state
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2833
diff changeset
   146
    self.in_request = True
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   147
    self.per_request_value = {}
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   148
    self.setRequestValue('request', request)
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   149
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   150
  def endRequest(self, request):
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   151
    """Performs cleanup after current request.
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   152
    """
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   153
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   154
    old_request = self.getRequestValue('request')
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   155
    self.per_request_value = {}
2849
c0462a1d3958 Add self.in_request to Core and verify correct state
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2833
diff changeset
   156
    self.in_request = False
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   157
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   158
    if id(old_request) != id(request):
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   159
      logging.error("ending request: \n'%s'\n != \n'%s'\n" % (
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   160
          old_request, request))
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   161
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   162
  def getRequestValue(self, key, default=None):
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   163
    """Gets a per-request value.
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   164
    """
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   165
2849
c0462a1d3958 Add self.in_request to Core and verify correct state
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2833
diff changeset
   166
    assert self.in_request
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   167
    return self.per_request_value.get(key, default)
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   168
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   169
  def setRequestValue(self, key, value):
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   170
    """Sets a per-request value.
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   171
    """
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   172
2849
c0462a1d3958 Add self.in_request to Core and verify correct state
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2833
diff changeset
   173
    assert self.in_request
2833
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   174
    self.per_request_value[key] = value
5aa891de5aa5 Add a new per-request value store to Core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2404
diff changeset
   175
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   176
  def getPatterns(self):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   177
    """Returns the Django patterns for this site.
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
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   180
    self.callService('registerWithSitemap', True)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   181
    return defaults.patterns(None, *self.sitemap)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   182
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   183
  @soc.cache.sidebar.cache
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   184
  def getSidebar(self, id, user):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   185
    """Constructs a sidebar for the current user.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   186
    """
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
    self.callService('registerWithSidebar', True)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   189
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   190
    sidebar = []
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   191
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   192
    for i in self.sidebar:
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   193
      menus = i(id, user)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   194
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   195
      for menu in (menus if menus else []):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   196
        sidebar.append(menu)
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
    return sorted(sidebar, key=lambda x: x.get('group'))
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   199
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   200
  def callService(self, service, unique, *args, **kwargs):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   201
    """Calls the specified service on all callbacks.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   202
    """
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
    if unique and (service in self.services):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   205
      return
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
    results = []
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   208
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   209
    for callback in self.registered_callbacks:
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   210
      func = self.getService(callback, service)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   211
      if not func:
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   212
        continue
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
      result = func(*args, **kwargs)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   215
      results.append(result)
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
    self.services.append(service)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   218
    return results
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   219
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   220
  def registerModuleCallbacks(self):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   221
    """Retrieves all callbacks for the modules of this site.
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
    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
   224
    number are dropped.  They won't be 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
    fmt = settings.MODULE_FMT
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   228
    modules = ['soc_core'] + settings.MODULES
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   229
    modules = [__import__(fmt % i, fromlist=['']) for i in modules]
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   230
2404
4f87bab3c6b3 Remove getCallback indirection in modules
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2359
diff changeset
   231
    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
   232
      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
   233
        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
   234
                                 callback_class.API_VERSION)
2333
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
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   237
      callback = callback_class(self)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   238
      self.registered_callbacks.append(callback)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   239
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   240
    return True
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   241
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   242
  ##
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   243
  ## Module code
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
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   246
  def registerCapability(self, capability):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   247
    """Registers the specified capability.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   248
    """
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
    self.capabilities.append(capability)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   251
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   252
  def requireCapability(self, capability):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   253
    """Requires that the specified capability is present.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   254
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   255
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   256
    if capability in self.capabilities:
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   257
      return True
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   258
2359
d4de17ab9a1f Style and variable names fixes in soc.modules.core module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2333
diff changeset
   259
    raise MissingCapability(capability, self.capabilities)
2333
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   260
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   261
  def requireService(self, service):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   262
    """Requires that the specified service has been called.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   263
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   264
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   265
    if service in self.services:
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   266
      return True
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   267
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   268
    raise MissingService(service, self.services)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   269
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   270
  def requireUniqueService(self, service):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   271
    """Requires that the specified service is called exactly once.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   272
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   273
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   274
    if service not in self.services:
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   275
      return True
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   276
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   277
    raise NonUniqueService(service, self.services)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   278
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   279
  def registerSitemapEntry(self, entries):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   280
    """Registers the specified entries with the sitemap.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   281
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   282
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   283
    self.sitemap.extend(entries)
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   284
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   285
  def registerSidebarEntry(self, entry):
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   286
    """Registers the specified entry with the sidebar.
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   287
    """
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   288
221482a54238 First step in the module design
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
diff changeset
   289
    self.sidebar.append(entry)