app/soc/logic/models/base.py
author Todd Larsen <tlarsen@google.com>
Sat, 18 Oct 2008 01:32:40 +0000
changeset 372 8595c1129c74
parent 368 f90f9b22751a
child 387 c55195361cb6
permissions -rw-r--r--
Formalize the concept of a NonPage that can appear in the site-map, useful for non-clickable sidebar menu divisions, for example. Convert "hacky" fake Pages into NonPages in the site-map. Also, pass page=self to every view as a keyword argument when generating the Django urlpatterns. Patch by: Todd Larsen Review by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Helpers functions for updating different kinds of models in datastore.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  '"Sverre Rabbelier" <sverer@rabbelier.nl>',
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
  ]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from google.appengine.ext import db
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
from soc.logic import key_name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
from soc.logic import out_of_band
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
class Logic():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
  """Base logic for entity classes.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  The BaseLogic class functions specific to Entity classes by relying
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  on the the child-classes to implement _model, _name and _key_name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
  def _updateField(self, model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
    """Hook called when a field is updated.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
    Base classes should override if any special actions need to be
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
    taken when a field is updated. The field is not updated if the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
    method does not return a True value.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
    return True
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  def getFromKeyName(self, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
    """"Returns User entity for key_name or None if not found.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
-
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
-    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
-      key_name: key name of entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
    return self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
  def getFromFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    """Returns the entity for a given link name, or None if not found.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    key_name = self.getKeyNameForFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
    if key_name:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
      entity = self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
    else:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
      entity = None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
    return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
  def getIfFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
    """Returns entity for supplied link name if one exists.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
      * None if a field is false.
317
87215136c268 Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 308
diff changeset
    83
      * Entity for supplied fields
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    Raises:
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
    86
      out_of_band.ErrorResponse if link name is not false, but no entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
      with the supplied link name exists in the Datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
    if not all(kwargs.values()):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
      # exit without error, to let view know that link_name was not supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
      return None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    entity = self.getFromFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
    if entity:
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
    97
      # an entity exist for this link_name, so return that entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
      return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
    fields = []
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
    for key, value in kwargs.iteritems():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
      fields.extend('"%s" is "%s" ' % (key, value))
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
    # else: fields were supplied, but there is no Entity that has it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
    raise out_of_band.ErrorResponse(
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
        'There is no %s with %s.' % (self._name, ''.join(fields)), status=404)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
  def getKeyNameForFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    """Return a Datastore key_name for a Entity from the specified fields.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
    if not all(kwargs.values()):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
      return None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
    return self._keyName(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   121
  def extractKeyFields(self, fields):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   122
    """Extracts all the fields from that are in the mode's key_fields property
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   123
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   124
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   125
      fields: A dict from which the fields should be extracted
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   126
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   127
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   128
    key_fields = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   129
    keys = fields.keys()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   130
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   131
    for key in keys[:]:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   132
      if key in self._model.key_fields:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   133
        key_fields[key] = fields[key]
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   134
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   135
    return key_fields
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   136
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
  def getForLimitAndOffset(self, limit, offset=0):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
    """Returns entities for given offset and limit or None if not found.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
      limit: max amount of entities to return
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
      offset: optional offset in entities list which defines first entity to
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
        return; default is zero (first entity)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
    query = self._model.all()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
    return query.fetch(limit, offset)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
  def updateModelProperties(self, model, **model_properties):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
    """Update existing model entity using supplied model properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
      model: a model entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
      **model_properties: keyword arguments that correspond to model entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
        properties and their values
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
      the original model entity with any supplied properties changed
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
    def update():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
      return self._unsafeUpdateModelProperties(model, **model_properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
    return db.run_in_transaction(update)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
  def _unsafeUpdateModelProperties(self, model, **model_properties):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
    """(see updateModelProperties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
    Like updateModelProperties(), but not run within a transaction.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
    properties = model.properties()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
    for prop in properties.values():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
      name = prop.name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
      if not name in self._skip_properties and name in model_properties:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
        value = model_properties[prop.name]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
        if self._updateField(model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
          prop.__set__(model, value)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
    model.put()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
    return model
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
  def updateOrCreateFromKeyName(self, properties, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
    """Update existing entity, or create new one with supplied properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
    Args:
317
87215136c268 Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 308
diff changeset
   190
      properties: dict with entity properties and their values
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
      key_name: the key_name of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
      the entity corresponding to the key_name, with any supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
      properties changed, or a new entity now associated with the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
      supplied key_name and properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
    entity = self.getFromKeyName(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
    if not entity:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
      # entity did not exist, so create one in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
      entity = self._model.get_or_insert(key_name, **properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
    # there is no way to be sure if get_or_insert() returned a new entity or
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
   206
    # got an existing one due to a race, so update with properties anyway,
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
    # in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
    return self.updateModelProperties(entity, **properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
  def updateOrCreateFromFields(self, properties, **kwargs):
368
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   211
    """Like updateOrCreateFromKeyName, but resolves **kwargs to a key_name first.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
    # attempt to retrieve the existing entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    key_name  = self.getKeyNameForFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
    return self.updateOrCreateFromKeyName(properties, key_name)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   218
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   219
  def isDeletable(self, entity):
368
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   220
    """Returns whether the specified entity can be deleted.
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   221
    
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   222
    Args:
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   223
      entity: an existing entity in datastore
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   224
    """
368
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   225
    
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   226
    return True
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   227
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   228
  def delete(self, entity):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   229
    """Delete existing entity from datastore.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   230
    
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   231
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   232
      entity: an existing entity in datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   234
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   235
    entity.delete()