app/soc/logic/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 02 Nov 2008 22:56:28 +0000
changeset 433 001b981be45e
parent 432 1851d67a1004
child 435 829fe8302a8b
permissions -rw-r--r--
Add a getForFields method It allows for generic queries with restrictions specified in a dictionary.
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>',
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    22
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    23
  '"Lennard de Rijk" <rijk0214@gmail.com>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from google.appengine.ext import db
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    30
from django.utils.translation import ugettext_lazy
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    31
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
from soc.logic import out_of_band
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 387
diff changeset
    35
class Logic:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  """Base logic for entity classes.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  The BaseLogic class functions specific to Entity classes by relying
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
  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
    40
  """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  def _updateField(self, model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
    """Hook called when a field is updated.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
    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
    46
    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
    47
    method does not return a True value.
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
    return True
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    52
  def _keyName(self, **kwargs):
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    53
    """
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    54
    Returns the KeyName constructed from kwargs for this type of entity
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    55
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    56
    The Keyname is in the following format:
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    57
    entity.name:<key_value1>:<key_value2>:...:<key_valueN>
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    58
    """
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    59
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    60
    #Get the KeyFieldNames for this entity
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    61
    key_field_names = self.getKeyFieldNames()
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    62
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    63
    #Check if all given KeyFieldNames are valid for this entity
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    64
    if not all(key in key_field_names for key in kwargs.keys()):
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    65
      raise Error("Some of the provided arguments are not key fields")
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    66
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    67
    #Check if all key_field_names for this entity are present in kwargs
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    68
    if not all(field in kwargs.keys() for field in key_field_names):
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    69
        raise Error("Not all the required key fields are present")
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    70
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    71
    #Check if all kwargs.values() are non-false
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    72
    if not all(kwargs.values()):
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    73
        raise Error("Not all KeyValues are non-false")
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    74
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    75
    #Construct the KeyValues in the order given by getKeyFieldNames()
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    76
    keyvalues = []
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    77
    for key_field_name in key_field_names:
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    78
        keyvalues.append(kwargs[key_field_name])
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    79
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    80
    #Construct the KeyName in the appropriate format
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    81
    return ":".join([self._name] + keyvalues)
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    82
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    83
  def getKeyValues(self, entity):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    84
    """Exctracts the key values from entity and returns them
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    85
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    86
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    87
      entity: the entity from which to extract the key values
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    88
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    89
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    90
    raise NotImplementedError
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    91
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    92
  def getKeyValuesFromFields(self, fields):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    93
    """Exctracts the key values from a dict and returns them
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    94
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    95
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    96
      fields: the dict from which to extract the key values
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    97
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    98
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
    99
    raise NotImplementedError
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   100
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   101
  def getKeyFieldNames(self):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   102
    """Returns an array with the names of the Key Fields 
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   103
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   104
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   105
    raise NotImplementedError
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   106
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   107
  def getKeySuffix(self, entity):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   108
    """Returns a suffix for the specified entity or None if no entity specified
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   109
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   110
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   111
      entity: the entity for which to get the suffix
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   112
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   113
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   114
    if not entity:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   115
      return None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   116
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   117
    key_values = self.getKeyValues(entity)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   118
    suffix = '/'.join(key_values)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   119
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   120
    return suffix
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   121
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
  def getFromKeyName(self, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
    """"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
   124
-
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
-    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
-      key_name: key name of entity
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
    return self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
  def getFromFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
    """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
   133
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
    key_name = self.getKeyNameForFields(**kwargs)
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
    if key_name:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
      entity = self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
    else:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
      entity = None
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
    return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
  def getIfFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
    """Returns entity for supplied link name if one exists.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
      * 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
   155
      * Entity for supplied fields
308
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
    Raises:
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
   158
      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
   159
      with the supplied link name exists in the Datastore
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
    if not all(kwargs.values()):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
      # 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
   164
      return None
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
    entity = self.getFromFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
    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
   169
      # 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
   170
      return entity
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
    fields = []
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   174
    format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   175
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   176
    msg_pairs = [format_text % {'key': key, 'value': value}
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   177
      for key, value in kwargs.iteritems()]
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   178
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   179
    joined_pairs = ' and '.join(msg_pairs)
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   180
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   181
    msg = ugettext_lazy(
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   182
      'There is no "%(name)s" where %(pairs)s.') % {
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   183
        'name': self._name, 'pairs': joined_pairs}
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   184
308
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
    # 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
   187
    raise out_of_band.ErrorResponse(msg, status=404)
308
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
  def getKeyNameForFields(self, **kwargs):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
    """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
   191
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
      **kwargs: the fields of the entity that uniquely identifies it
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
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
    if not all(kwargs.values()):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
      return None
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
    return self._keyName(**kwargs)
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
  def getForLimitAndOffset(self, limit, offset=0):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
    """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
   203
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
      limit: max amount of entities to return
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
      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
   207
        return; default is zero (first entity)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
    """
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
    query = self._model.all()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
    return query.fetch(limit, offset)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   213
  def getForFields(self, properties, unique=False, limit=1000, offset=0):
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   214
    """Returns all entities that have the specified properties
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   215
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   216
    Args:
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   217
      properties: the properties that the entity should have
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   218
      unique: if set, only the first item from the resultset will be returned
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   219
      limit: max amount of entities to return
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   220
      offset: optional number of results to skip first; default zero.
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   221
    """
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   222
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   223
    if not properties:
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   224
      raise Error("Properties did not contain any values")
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   225
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   226
    format_text = '%(key)s = :%(key)s'
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   227
    msg_pairs = [format_text % {'key': key} for key in properties.iterkeys()]
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   228
    joined_pairs = ' AND '.join(msg_pairs)
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   229
    condition = 'WHERE %s' % joined_pairs
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   230
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   231
    query = self._model.gql(condition, **properties)
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   232
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   233
    if unique:
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   234
      return query.get()
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   235
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   236
    result = query.fetch(limit, offset)
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   237
    return result
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   238
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   239
  def updateModelProperties(self, model, **model_properties):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   240
    """Update existing model entity using supplied model properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   241
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   242
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   243
      model: a model entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   244
      **model_properties: keyword arguments that correspond to model entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
        properties and their values
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
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   248
      the original model entity with any supplied properties changed
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
    """
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
    def update():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   252
      return self._unsafeUpdateModelProperties(model, **model_properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   253
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
    return db.run_in_transaction(update)
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 _unsafeUpdateModelProperties(self, model, **model_properties):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   257
    """(see updateModelProperties)
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
    Like updateModelProperties(), but not run within a transaction.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
    properties = model.properties()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   264
    for prop in properties.values():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   265
      name = prop.name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   267
      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
   268
        value = model_properties[prop.name]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   269
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   270
        if self._updateField(model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
          prop.__set__(model, value)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   272
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   273
    model.put()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   274
    return model
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   275
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   276
  def updateOrCreateFromKeyName(self, properties, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   277
    """Update existing entity, or create new one with supplied properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   278
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   279
    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
   280
      properties: dict with entity properties and their values
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   281
      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
   282
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   283
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   284
      the entity corresponding to the key_name, with any supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   285
      properties changed, or a new entity now associated with the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   286
      supplied key_name and properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   287
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   288
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   289
    entity = self.getFromKeyName(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   290
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   291
    if not entity:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   292
      # entity did not exist, so create one in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   293
      entity = self._model.get_or_insert(key_name, **properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   294
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   295
    # 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
   296
    # 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
   297
    # in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   298
    return self.updateModelProperties(entity, **properties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   299
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   300
  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
   301
    """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
   302
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   303
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   304
    # attempt to retrieve the existing entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   305
    key_name  = self.getKeyNameForFields(**kwargs)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   306
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   307
    return self.updateOrCreateFromKeyName(properties, key_name)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   308
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   309
  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
   310
    """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
   311
    
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
   312
    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
   313
      entity: an existing entity in datastore
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   314
    """
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
   315
    
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   316
    return True
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   317
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   318
  def delete(self, entity):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   319
    """Delete existing entity from datastore.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   320
    
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   321
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   322
      entity: an existing entity in datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   323
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   324
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   325
    entity.delete()