app/soc/logic/models/expando_base.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sat, 24 Oct 2009 15:14:11 -0700
changeset 3036 c703f3802841
parent 2736 8f3935f0f4ba
permissions -rw-r--r--
Use skipFields method in the expando base logic.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2481
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Helpers functions for updating different kinds of Expando models.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
from soc.logic.models import base
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
class Logic(base.Logic):
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
  """Base logic for Expando entity classes.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
  """
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
  def __init__(self, model, base_model=None, scope_logic=None,
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
               name=None, skip_properties=None, id_based=False):
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
    """Defines the name, key_name and model for this entity.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
    """
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
    super(Logic, self).__init__(model=model, base_model=base_model,
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
                                scope_logic=scope_logic, name=name,
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
                                skip_properties=skip_properties,
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
                                id_based=id_based)
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
2736
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2481
diff changeset
    42
  def updateEntityProperties(self, entity, entity_properties, silent=False,
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2481
diff changeset
    43
                             store=True):
2481
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    """Update existing entity using supplied properties.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
    Overwrites base because of Expando properties.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
    Args:
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
      entity: a model entity
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
      entity_properties: keyword arguments that correspond to entity
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
        properties and their values
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
      silent: iff True does not call _onUpdate method
2736
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2481
diff changeset
    53
      store: iff True updated entity is actually stored in the data model 
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2481
diff changeset
    54
      
2481
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
    Returns:
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
      The original entity with any supplied properties changed.
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
    """
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
    if not entity:
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
      raise NoEntityError
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
    if not entity_properties:
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
      raise InvalidArgumentError
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
    for name, value in entity_properties.iteritems():
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
      # if the property is not to be updated, skip it
3036
c703f3802841 Use skipFields method in the expando base logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2736
diff changeset
    67
      if self.skipField(name):
2481
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
        continue
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
      if self._updateField(entity, entity_properties, name):
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
        setattr(entity, name, value)
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
2736
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2481
diff changeset
    73
    if store:
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2481
diff changeset
    74
      entity.put()
2481
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
    # call the _onUpdate method
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
    if not silent:
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
      self._onUpdate(entity)
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
031e5828f3a4 Added base class logic for Expando based models.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
    return entity