app/soc/logic/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 23 Oct 2008 05:21:26 +0000
changeset 409 9d24850db88f
parent 407 3cf5630d86d1
child 410 2af7f84f4fc7
permissions -rw-r--r--
Addressed comments by Pawel and Todd Patch by: Sverre Rabbelier Reviewed 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
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    29
from django.utils.translation import ugettext_lazy
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    30
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
from soc.logic import out_of_band
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
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 387
diff changeset
    34
class Logic:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
  """Base logic for entity classes.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  The BaseLogic class functions specific to Entity classes by relying
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  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
    39
  """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
  def _updateField(self, model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
    """Hook called when a field is updated.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
    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
    45
    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
    46
    method does not return a True value.
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
    return True
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  def getFromKeyName(self, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    """"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
    53
-
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
-    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
-      key_name: key name of entity
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
    return self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  def getFromFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
    """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
    62
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
      **kwargs: the fields of the entity that uniquely identifies it
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    key_name = self.getKeyNameForFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
    if key_name:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
      entity = self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
    else:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
      entity = None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
    return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
  def getIfFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
    """Returns entity for supplied link name if one exists.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
      * 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
    84
      * Entity for supplied fields
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
    Raises:
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
    87
      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
    88
      with the supplied link name exists in the Datastore
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
    if not all(kwargs.values()):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
      # 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
    93
      return None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    entity = self.getFromFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
    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
    98
      # 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
    99
      return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
    fields = []
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   103
    format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   104
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   105
    msg_pairs = [format_text % {'key': key, 'value': value}
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   106
      for key, value in kwargs.iteritems()]
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   107
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   108
    joined_pairs = ' and '.join(msg_pairs)
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   109
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   110
    msg = ugettext_lazy(
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   111
      'There is no "%(name)s" where %(pairs)s.') % {
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   112
        'name': self._name, 'pairs': joined_pairs}
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   113
308
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
    # else: fields were supplied, but there is no Entity that has it
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   116
    raise out_of_band.ErrorResponse(msg, status=404)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
  def getKeyNameForFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
    """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
   120
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
    if not all(kwargs.values()):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
      return None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
    return self._keyName(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   130
  def getEmptyKeyFields(self):
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   131
    """Returns an dict with all the entities key_fields set to None
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   132
    """
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   133
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   134
    kwargs = {}
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   135
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   136
    for field in self._model.KEY_FIELDS:
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   137
      kwargs[field] = None
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   138
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   139
    return kwargs
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   140
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   141
  def constructKeyNameSuffix(self, entity):
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   142
    """Constructs a suffix from the specified fields
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   143
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   144
    The resulting suffix is constructed by adding a '/' after all
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   145
    key fields. The suffix is constructed in the order as specified
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   146
    in the model's key_fields property. The suffix will not have a
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   147
    trailing '/'.
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   148
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   149
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   150
      fields: a dictionary with the values for all the key_fields
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   151
          of this entity.
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   152
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   153
407
3cf5630d86d1 Fix not working Create views (committed in r864). Check if entity exist in constructKeyNameSuffix funtion and apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 403
diff changeset
   154
    if not entity:
3cf5630d86d1 Fix not working Create views (committed in r864). Check if entity exist in constructKeyNameSuffix funtion and apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 403
diff changeset
   155
      return None
3cf5630d86d1 Fix not working Create views (committed in r864). Check if entity exist in constructKeyNameSuffix funtion and apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 403
diff changeset
   156
    
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   157
    suffix = []
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   158
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   159
    for field in entity.KEY_FIELDS:
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   160
      # Four hours wasted on this line, because apparently passing in a dict
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   161
      # one time, and a db.Model the next time, things get rather hard to debug
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   162
      value = entity.__getattribute__(field)
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   163
      suffix.append(value)
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   164
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   165
    res = '/'.join(suffix)
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   166
    return res
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 396
diff changeset
   167
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   168
  def extractKeyFields(self, fields):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   169
    """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
   170
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   171
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   172
      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
   173
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   174
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   175
    key_fields = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   176
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 368
diff changeset
   177
    for key, value in fields.iteritems():
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   178
      if key in self._model.KEY_FIELDS:
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 368
diff changeset
   179
        key_fields[key] = value
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   180
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   181
    return key_fields
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   182
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
  def getForLimitAndOffset(self, limit, offset=0):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
    """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
   185
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
      limit: max amount of entities to return
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
      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
   189
        return; default is zero (first entity)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
    query = self._model.all()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
    return query.fetch(limit, offset)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
  def updateModelProperties(self, model, **model_properties):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
    """Update existing model entity using supplied model 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
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
      model: a model entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
      **model_properties: keyword arguments that correspond to model entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
        properties and their values
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
      the original model entity with any supplied properties changed
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
    def update():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
      return self._unsafeUpdateModelProperties(model, **model_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
    return db.run_in_transaction(update)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
  def _unsafeUpdateModelProperties(self, model, **model_properties):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
    """(see updateModelProperties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    Like updateModelProperties(), but not run within a transaction.
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   218
    properties = model.properties()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   219
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   220
    for prop in properties.values():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   221
      name = prop.name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   222
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   223
      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
   224
        value = model_properties[prop.name]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   225
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   226
        if self._updateField(model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   227
          prop.__set__(model, value)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   228
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   229
    model.put()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   230
    return model
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   231
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   232
  def updateOrCreateFromKeyName(self, properties, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
    """Update existing entity, or create new one with supplied properties.
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
    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
   236
      properties: dict with entity properties and their values
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   237
      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
   238
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   239
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   240
      the entity corresponding to the key_name, with any supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   241
      properties changed, or a new entity now associated with the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   242
      supplied key_name and properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   243
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   244
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
    entity = self.getFromKeyName(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   246
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   247
    if not entity:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   248
      # entity did not exist, so create one in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
      entity = self._model.get_or_insert(key_name, **properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   250
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   251
    # 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
   252
    # 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
   253
    # in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
    return self.updateModelProperties(entity, **properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   255
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   256
  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
   257
    """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
   258
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   259
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
    # attempt to retrieve the existing entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
    key_name  = self.getKeyNameForFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
    return self.updateOrCreateFromKeyName(properties, key_name)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   264
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   265
  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
   266
    """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
   267
    
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
   268
    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
   269
      entity: an existing entity in datastore
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   270
    """
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
   271
    
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   272
    return True
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   273
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   274
  def delete(self, entity):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   275
    """Delete existing entity from datastore.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   276
    
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   277
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   278
      entity: an existing entity in datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   279
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   280
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   281
    entity.delete()