app/soc/logic/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Wed, 02 Sep 2009 10:24:10 +0200
changeset 2854 003a84e774e2
parent 2853 80a71738cb4f
child 2897 c0e78185444c
permissions -rw-r--r--
Add support for prefetching fields This makes it possible to specify the fields that should be prefetched as part of a getForFields query. Doing a prefetch on a ReferenceProperty when it is known that the property will be referenced in (almost all) entities is far more efficient than doing the invididual fetches sequentially.
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
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
308
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__ = [
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
    21
  '"Madhusudan C.S." <madhusudancs@gmail.com>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  '"Todd Larsen" <tlarsen@google.com>',
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    23
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
634
ad58da0b78e8 Changed email address in logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 608
diff changeset
    24
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
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
2346
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
    29
import logging
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
    30
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
from google.appengine.ext import db
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 961
diff changeset
    33
from django.utils.translation import ugettext
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    34
1106
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 986
diff changeset
    35
from soc.cache import sidebar
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
    36
from soc.logic import dicts
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 535
diff changeset
    37
from soc.views import out_of_band
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
441
8a7110ad3d82 Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 436
diff changeset
    40
class Error(Exception):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 487
diff changeset
    41
  """Base class for all exceptions raised by this module.
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 487
diff changeset
    42
  """
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    43
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    44
  pass
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    45
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
    46
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    47
class InvalidArgumentError(Error):
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    48
  """Raised when an invalid argument is passed to a method.
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    49
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    50
  For example, if an argument is None, but must always be non-False.
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    51
  """
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    52
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    53
  pass
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    54
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
    55
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    56
class NoEntityError(InvalidArgumentError):
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    57
  """Raised when no entity is passed to a method that requires one.
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    58
  """
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    59
441
8a7110ad3d82 Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 436
diff changeset
    60
  pass
8a7110ad3d82 Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 436
diff changeset
    61
8a7110ad3d82 Fix not working former ids feature of User Profile Developer Edit view. Add missing Error exception class to soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 436
diff changeset
    62
642
671dc971c3d1 Allow for extra dynamic fields for the create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 634
diff changeset
    63
class Logic(object):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  """Base logic for entity classes.
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
  The BaseLogic class functions specific to Entity classes by relying
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    67
  on arguments passed to __init__.
308
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
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    70
  def __init__(self, model, base_model=None, scope_logic=None,
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
    71
               name=None, skip_properties=None, id_based=False):
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    72
    """Defines the name, key_name and model for this entity.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    73
    """
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    74
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    75
    self._model = model
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    76
    self._base_model = base_model
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    77
    self._scope_logic = scope_logic
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
    78
    self._id_based = id_based
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    79
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    80
    if name:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    81
      self._name = name
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    82
    else:
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
    83
      self._name =  self._model.__name__
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    84
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    85
    if skip_properties:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    86
      self._skip_properties = skip_properties
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    87
    else:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    88
      self._skip_properties = []
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    89
606
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    90
  def getModel(self):
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
    91
    """Returns the model this logic class uses.
606
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    92
    """
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    93
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    94
    return self._model
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    95
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    96
  def getScopeLogic(self):
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
    97
    """Returns the logic of the enclosing scope.
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    98
    """
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    99
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
   100
    return self._scope_logic
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
   101
679
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   102
  def getScopeDepth(self):
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
   103
    """Returns the scope depth for this entity.
679
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   104
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
   105
    Returns None if any of the parent scopes return None.
679
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   106
    """
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   107
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   108
    if not self._scope_logic:
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   109
      return 0
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   110
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   111
    depth = self._scope_logic.logic.getScopeDepth()
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   112
    return None if (depth is None) else (depth + 1)
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   113
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   114
  def getKeyNameFromFields(self, fields):
1218
569a3fe9cb88 Cleaned up getKeyNameFromFields in Logic base.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1183
diff changeset
   115
    """Returns the KeyName constructed from fields dict for this type of entity.
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   116
436
f3c313d54aa4 Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 435
diff changeset
   117
    The KeyName is in the following format:
1218
569a3fe9cb88 Cleaned up getKeyNameFromFields in Logic base.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1183
diff changeset
   118
    <key_value1>/<key_value2>/.../<key_valueN>
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   119
    """
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   120
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   121
    if not fields:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   122
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   123
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   124
    key_field_names = self.getKeyFieldNames()
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   125
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   126
    # check if all key_field_names for this entity are present in fields
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   127
    if not all(field in fields.keys() for field in key_field_names):
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   128
      raise InvalidArgumentError("Not all the required key fields are present")
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   129
1218
569a3fe9cb88 Cleaned up getKeyNameFromFields in Logic base.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1183
diff changeset
   130
    if not all(fields.get(field) for field in key_field_names):
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   131
      raise InvalidArgumentError("Not all KeyValues are non-false")
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   132
436
f3c313d54aa4 Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 435
diff changeset
   133
    # construct the KeyValues in the order given by getKeyFieldNames()
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   134
    keyvalues = []
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   135
    for key_field_name in key_field_names:
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   136
      keyvalues.append(fields[key_field_name])
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   137
436
f3c313d54aa4 Add missing TODOs to soc.logic.models classes. Fix comments format in base.Logic class. Changes based on comments to r972.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 435
diff changeset
   138
    # construct the KeyName in the appropriate format
661
d7b643255255 Change the key name to not include entity type
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
   139
    return '/'.join(keyvalues)
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   140
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   141
  def getFullModelClassName(self):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   142
    """Returns fully-qualified model module.class name string.
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   143
    """
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   144
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
   145
    return '%s.%s' % (self._model.__module__, self._model.__name__)
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   146
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   147
  def getKeyValuesFromEntity(self, entity):
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   148
    """Extracts the key values from entity and returns them.
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   149
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   150
    The default implementation uses the scope and link_id as key values.
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   151
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   152
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   153
      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
   154
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   155
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   156
    if not entity:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   157
      raise NoEntityError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   158
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   159
    return [entity.scope_path, entity.link_id]
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   160
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   161
  def getKeyValuesFromFields(self, fields):
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   162
    """Extracts the key values from a dict and returns them.
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   163
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   164
    The default implementation uses the scope and link_id as key values.
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   165
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   166
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   167
      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
   168
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   169
2190
399fc9411abc Readability fix in base logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2161
diff changeset
   170
    if ('scope_path' not in fields) or ('link_id' not in fields):
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   171
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   172
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   173
    return [fields['scope_path'], fields['link_id']]
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   174
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   175
  def getKeyFieldNames(self):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 487
diff changeset
   176
    """Returns an array with the names of the Key Fields.
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   177
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   178
    The default implementation uses the scope and link_id as key values.
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   179
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   180
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   181
    return ['scope_path', 'link_id']
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   182
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   183
  def getKeyFieldsFromFields(self, dictionary):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 487
diff changeset
   184
    """Does any required massaging and filtering of dictionary.
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   185
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   186
    The resulting dictionary contains just the key names, and has any
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   187
    required translations/modifications performed.
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   188
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   189
    Args:
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   190
      dictionary: The arguments to massage
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   191
    """
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   192
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   193
    if not dictionary:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   194
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   195
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   196
    keys = self.getKeyFieldNames()
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   197
    values = self.getKeyValuesFromFields(dictionary)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   198
    key_fields = dicts.zip(keys, values)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   199
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   200
    return key_fields
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   201
2709
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   202
  def getFromKeyName(self, key_name, parent=None):
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   203
    """Returns entity for key_name or None if not found.
1222
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   204
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   205
    Args:
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   206
      key_name: key name of entity
2709
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   207
      parent: parent of the entity
308
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
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   210
    if self._id_based:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   211
      raise Error("getFromKeyName called on an id based logic")
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   212
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   213
    if not key_name:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   214
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   215
2709
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   216
    return self._model.get_by_key_name(key_name, parent=parent)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
2709
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   218
  def getFromID(self, id, parent=None):
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   219
    """Returns entity for id or None if not found.
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   220
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   221
    Args:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   222
      id: id of entity
2709
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   223
      parent: parent of the entity
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   224
    """
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   225
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   226
    if not self._id_based:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   227
      raise Error("getFromID called on a not id based logic")
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   228
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   229
    if not id:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   230
      raise InvalidArgumentError
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   231
2709
7a595f688ab5 Add parent parameter to accommodate entity groups.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2364
diff changeset
   232
    return self._model.get_by_id(id, parent=parent)
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   233
1780
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   234
  def getFromKeyNameOr404(self, key_name):
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   235
    """Like getFromKeyName but expects to find an entity.
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   236
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   237
    Raises:
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   238
      out_of_band.Error if no entity is found
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   239
    """
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   240
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   241
    entity = self.getFromKeyName(key_name)
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   242
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   243
    if entity:
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   244
      return entity
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   245
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   246
    msg = ugettext('There is no "%(name)s" named %(key_name)s.') % {
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   247
        'name': self._name, 'key_name': key_name}
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   248
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   249
    raise out_of_band.Error(msg, status=404)
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   250
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   251
  def getFromIDOr404(self, id):
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   252
    """Like getFromID but expects to find an entity.
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   253
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   254
    Raises:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   255
      out_of_band.Error if no entity is found
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   256
    """
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   257
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   258
    entity = self.getFromID(id)
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   259
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   260
    if entity:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   261
      return entity
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   262
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   263
    msg = ugettext('There is no "%(name)s" with id %(id)s.') % {
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   264
        'name': self._name, 'id': id}
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   265
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   266
    raise out_of_band.Error(msg, status=404)
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   267
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   268
  def getFromKeyFields(self, fields):
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   269
    """Returns the entity for the specified key names, or None if not found.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   270
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
    Args:
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   272
      fields: a dict containing the fields of the entity that
1222
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   273
        uniquely identifies it
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   274
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   275
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   276
    if not fields:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   277
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   278
1222
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   279
    key_fields = self.getKeyFieldsFromFields(fields)
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   280
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   281
    if all(key_fields.values()):
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   282
      key_name = self.getKeyNameFromFields(key_fields)
672
c8f9281f535b Some more cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 671
diff changeset
   283
      entity = self.getFromKeyName(key_name)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   284
    else:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   285
      entity = None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   286
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   287
    return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   288
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   289
  def getFromKeyFieldsOr404(self, fields):
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   290
    """Like getFromKeyFields but expects to find an entity.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   291
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   292
    Raises:
1222
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   293
      out_of_band.Error if no entity is found
308
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
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   296
    entity = self.getFromKeyFields(fields)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   297
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   298
    if entity:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   299
      return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   300
1672
9b6cc71dd70c Only show key_fields in error message
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1626
diff changeset
   301
    key_fields = self.getKeyFieldsFromFields(fields)
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 961
diff changeset
   302
    format_text = ugettext('"%(key)s" is "%(value)s"')
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   303
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   304
    msg_pairs = [format_text % {'key': key, 'value': value}
1672
9b6cc71dd70c Only show key_fields in error message
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1626
diff changeset
   305
      for key, value in key_fields.iteritems()]
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   306
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   307
    joined_pairs = ' and '.join(msg_pairs)
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   308
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 961
diff changeset
   309
    msg = ugettext(
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   310
      'There is no "%(name)s" where %(pairs)s.') % {
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   311
        'name': self._name, 'pairs': joined_pairs}
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   312
592
be98a2f5d8a2 Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents: 565
diff changeset
   313
    raise out_of_band.Error(msg, status=404)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   314
2854
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   315
  def prefetchField(self, field, data):
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   316
    """Prefetches all fields in data from the datastore in one fetch.
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   317
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   318
    Args:
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   319
      field: the field that should be fetched
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   320
      data: the data for which the prefetch should be done
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   321
    """
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   322
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   323
    prop = getattr(self._model, field, None)
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   324
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   325
    if not prop:
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   326
      logging.exception("Model %s does not have attribute %s" %
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   327
                        (self._model, field))
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   328
      return
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   329
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   330
    if not isinstance(prop, db.ReferenceProperty):
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   331
      logging.exception("Property %s of %s is not a ReferenceProperty but a %s" %
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   332
                        (field, self._model.kind(), prop.__class__.__name__))
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   333
      return
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   334
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   335
    keys = [prop.get_value_for_datastore(i) for i in data]
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   336
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   337
    prefetched_entities = db.get(keys)
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   338
    prefetched_dict = dict((i.key(), i) for i in prefetched_entities)
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   339
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   340
    for i in data:
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   341
      value = prefetched_dict[prop.get_value_for_datastore(i)]
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   342
      setattr(i, field, value)
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   343
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   344
  def getForFields(self, filter=None, unique=False, limit=1000, offset=0,
2854
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   345
                   ancestors=None, order=None, prefetch=None):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 487
diff changeset
   346
    """Returns all entities that have the specified properties.
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   347
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   348
    Args:
961
36a90d0e9211 Add Madhusudan.C.S to AUTHORS. Fix docstring parameters list in soc.logic.models.base module Base class getForFields method.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 723
diff changeset
   349
      filter: a dict for the properties that the entities should have
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   350
      unique: if set, only the first item from the resultset will be returned
1165
4db62684ce13 Unify 'getForFields' and 'getForLimitAndOffset'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1115
diff changeset
   351
      limit: the amount of entities to fetch at most
4db62684ce13 Unify 'getForFields' and 'getForLimitAndOffset'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1115
diff changeset
   352
      offset: the position to start at
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   353
      ancestors: list of ancestors properties to set for this query
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   354
      order: a list with the sort order
2854
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   355
      prefetch: the fields of the data that should be pre-fetched,
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   356
          has no effect if unique is True
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   357
    """
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   358
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   359
    if unique:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   360
      limit = 1
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   361
2854
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   362
    if not prefetch:
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   363
      prefetch = []
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   364
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   365
    query = self.getQueryForFields(filter=filter, 
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   366
                                   ancestors=ancestors, order=order)
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   367
2346
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
   368
    try:
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
   369
      result = query.fetch(limit, offset)
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
   370
    except db.NeedIndexError, exception:
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
   371
      result = []
2758
688753f1d406 Changed the parameter from ancestor to ancestors to accommodate a list of ancestors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2757
diff changeset
   372
      logging.exception("%s, model: %s filter: %s, ancestors: %s, order: %s" % 
688753f1d406 Changed the parameter from ancestor to ancestors to accommodate a list of ancestors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2757
diff changeset
   373
                        (exception, self._model, filter, ancestors, order))
2346
d830123c1be1 Do not die on NeedIndexErrors
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2230
diff changeset
   374
      # TODO: send email
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   375
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   376
    if unique:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   377
      return result[0] if result else None
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   378
2854
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   379
    for field in prefetch:
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   380
      self.prefetchField(field, result)
003a84e774e2 Add support for prefetching fields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2853
diff changeset
   381
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   382
    return result
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   383
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   384
  def getQueryForFields(self, filter=None, ancestors=None, order=None):
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   385
    """Returns a query with the specified properties.
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   386
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   387
    Args:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   388
      filter: a dict for the properties that the entities should have
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   389
      ancestors: list with ancestor properties to set for this query
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   390
      order: a list with the sort order
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   391
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   392
    Returns:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   393
      - Query object instantiated with the given properties
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   394
    """
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   395
2851
792e2c58dc62 Store queries in memcache
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2758
diff changeset
   396
    from google.appengine.api import memcache
2853
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   397
    from soc.logic import system
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   398
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   399
    if system.isDebug():
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   400
      queries = memcache.get('__melange_queries')
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   401
      if not queries:
2851
792e2c58dc62 Store queries in memcache
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2758
diff changeset
   402
        queries = []
2853
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   403
      queries.append((self._model.__name__, filter, order))
80a71738cb4f only store queries in debug mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2852
diff changeset
   404
      memcache.set('__melange_queries', queries)
2851
792e2c58dc62 Store queries in memcache
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2758
diff changeset
   405
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   406
    if not filter:
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   407
      filter = {}
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   408
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   409
    if not ancestors:
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   410
      ancestors = []
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   411
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   412
    if not order:
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   413
      order = []
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   414
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   415
    orderset = set([i.strip('-') for i in order])
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   416
    if len(orderset) != len(order):
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   417
      raise InvalidArgumentError
1179
427d2ec42823 Rewrite getForFields to use GQL instead of the Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   418
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   419
    query = db.Query(self._model)
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   420
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   421
    for key, value in filter.iteritems():
1696
7b120f7f9a7a Small optimization in getForFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1694
diff changeset
   422
      if isinstance(value, list) and len(value) == 1:
7b120f7f9a7a Small optimization in getForFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1694
diff changeset
   423
        value = value[0]
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   424
      if isinstance(value, list):
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   425
        op = '%s IN' % key
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   426
        query.filter(op, value)
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   427
      else:
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   428
        query.filter(key, value)
723
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 689
diff changeset
   429
2756
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   430
    for ancestor in ancestors:
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   431
      query.ancestor(ancestor)
054810192277 Added ancestors property to getQueryForFields for queries with ancestor filter.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2736
diff changeset
   432
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   433
    for key in order:
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   434
      query.order(key)
1183
03db1f6d222a Make getForFields work with no filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1179
diff changeset
   435
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   436
    return query
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   437
2736
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2709
diff changeset
   438
  def updateEntityProperties(self, entity, entity_properties, silent=False,
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2709
diff changeset
   439
                             store=True):
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   440
    """Update existing entity using supplied properties.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   441
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   442
    Args:
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   443
      entity: a model entity
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   444
      entity_properties: keyword arguments that correspond to entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   445
        properties and their values
1573
063306b7210e Added missing comment to soc/logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1549
diff changeset
   446
      silent: iff True does not call _onUpdate method
2736
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2709
diff changeset
   447
      store: iff True updated entity is actually stored in the data model
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   448
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   449
    Returns:
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   450
      The original entity with any supplied properties changed.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   451
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   452
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   453
    if not entity:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   454
      raise NoEntityError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   455
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   456
    if not entity_properties:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   457
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   458
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   459
    properties = self._model.properties()
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   460
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   461
    for name, prop in properties.iteritems():
2030
a21ee5f0591d Fixed typo in docstring for logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1780
diff changeset
   462
      # if the property is not updateable or is not updated, skip it
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   463
      if name in self._skip_properties or (name not in entity_properties):
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   464
        continue
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   465
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1516
diff changeset
   466
      if self._updateField(entity, entity_properties, name):
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1516
diff changeset
   467
        value = entity_properties[name]
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   468
        prop.__set__(entity, value)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   469
2736
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2709
diff changeset
   470
    if store:
8f3935f0f4ba Argument store added to updateEntityProperties.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2709
diff changeset
   471
      entity.put()
1549
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   472
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   473
    # call the _onUpdate method
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   474
    if not silent:
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   475
      self._onUpdate(entity)
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   476
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   477
    return entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   478
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   479
  def updateOrCreateFromKeyName(self, properties, key_name, silent=False):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   480
    """Update existing entity, or create new one with supplied properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   481
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   482
    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
   483
      properties: dict with entity properties and their values
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   484
      key_name: the key_name of the entity that uniquely identifies it
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   485
      silent: if True, do not run the _onCreate hook
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   486
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   487
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   488
      the entity corresponding to the key_name, with any supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   489
      properties changed, or a new entity now associated with the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   490
      supplied key_name and properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   491
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   492
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   493
    entity = self.getFromKeyName(key_name)
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   494
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   495
    create_entity = not entity
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   496
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   497
    if create_entity:
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   498
      for property_name in properties:
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   499
        self._createField(properties, property_name)
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   500
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   501
      # entity did not exist, so create one in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   502
      entity = self._model.get_or_insert(key_name, **properties)
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   503
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   504
      if not silent:
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   505
        # a new entity has been created call _onCreate
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   506
        self._onCreate(entity)
1516
8df06dc877aa Do not update newly created model properties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1515
diff changeset
   507
    else:
8df06dc877aa Do not update newly created model properties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1515
diff changeset
   508
      # If someone else already created the entity (due to a race), we
8df06dc877aa Do not update newly created model properties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1515
diff changeset
   509
      # should not update the propties (as they 'won' the race).
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   510
      entity = self.updateEntityProperties(entity, properties, silent=silent)
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   511
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   512
    return entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   513
2221
1177ab97c2f0 Add a silent option to updateOrCreateFromFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2203
diff changeset
   514
  def updateOrCreateFromFields(self, properties, silent=False):
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   515
    """Update existing entity or creates a new entity with the supplied 
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   516
    properties containing the key fields.
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   517
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   518
    Args:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   519
      properties: dict with entity properties and their values
2221
1177ab97c2f0 Add a silent option to updateOrCreateFromFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2203
diff changeset
   520
      silent: if True, do not run the _onCreate hook
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   521
    """
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   522
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   523
    for property_name in properties:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   524
      self._createField(properties, property_name)
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   525
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   526
    if self._id_based:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   527
      entity = self._model(**properties)
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   528
      entity.put()
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   529
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   530
      if not silent:
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   531
        # call the _onCreate hook
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   532
        self._onCreate(entity)
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   533
    else:
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   534
      key_name = self.getKeyNameFromFields(properties)
2757
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   535
      entity = self.updateOrCreateFromKeyName(properties, key_name,
7d0d0ce76bd9 updateOrCreateFromFields is made to call updateOrCreateFromKeyName.
Madhusudan C.S. <madhusudancs@gmail.com>
parents: 2756
diff changeset
   536
                                              silent=silent)
2221
1177ab97c2f0 Add a silent option to updateOrCreateFromFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2203
diff changeset
   537
2203
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   538
    return entity
e2c6e6722d69 Add support for ID based entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2190
diff changeset
   539
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   540
  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
   541
    """Returns whether the specified entity can be deleted.
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   542
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
   543
    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
   544
      entity: an existing entity in datastore
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   545
    """
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   546
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   547
    return True
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   548
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   549
  def delete(self, entity):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   550
    """Delete existing entity from datastore.
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   551
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   552
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   553
      entity: an existing entity in datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   554
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   555
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   556
    entity.delete()
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   557
    # entity has been deleted call _onDelete
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   558
    self._onDelete(entity)
1515
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   559
1601
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   560
  def getAll(self, query):
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   561
    """Retrieves all entities for the specified query.
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   562
    """
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   563
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   564
    chunk = 999
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   565
    offset = 0
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   566
    result = []
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   567
    more = True
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   568
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   569
    while(more):
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   570
      data = query.fetch(chunk+1, offset)
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   571
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   572
      more = len(data) > chunk
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   573
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   574
      if more:
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   575
        del data[chunk]
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   576
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   577
      result.extend(data)
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   578
      offset = offset + chunk
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   579
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   580
    return result
2364
a1cbd3143277 Rename bathSize variable to batch_size in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2346
diff changeset
   581
  # pylint: disable-msg=C0103
a1cbd3143277 Rename bathSize variable to batch_size in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2346
diff changeset
   582
  def entityIterator(self, queryGen, batch_size = 100):
2230
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   583
    """Iterator that yields an entity in batches.
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   584
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   585
    Args:
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   586
      queryGen: should return a Query object
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   587
      batchSize: how many entities to retrieve in one datastore call
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   588
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   589
    Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook).
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   590
    """
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   591
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   592
     # AppEngine will not fetch more than 1000 results
2364
a1cbd3143277 Rename bathSize variable to batch_size in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2346
diff changeset
   593
    batch_size = min(batch_size, 1000)
2230
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   594
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   595
    done = False
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   596
    count = 0
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   597
    key = None
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   598
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   599
    while not done:
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   600
      query = queryGen()
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   601
      if key:
2364
a1cbd3143277 Rename bathSize variable to batch_size in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2346
diff changeset
   602
        query.filter("__key__ > ", key)
a1cbd3143277 Rename bathSize variable to batch_size in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2346
diff changeset
   603
      results = query.fetch(batch_size)
2230
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   604
      for result in results:
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   605
        count += 1
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   606
        yield result
2364
a1cbd3143277 Rename bathSize variable to batch_size in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2346
diff changeset
   607
      if batch_size > len(results):
2230
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   608
        done = True
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   609
      else:
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   610
        key = results[-1].key()
f3d59025d254 Add entityIterator to base Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2221
diff changeset
   611
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   612
  def _createField(self, entity_properties, name):
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   613
    """Hook called when a field is created.
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   614
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   615
    To be exact, this method is called for each field (that has a value
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   616
    specified) on an entity that is being created.
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   617
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   618
    Base classes should override if any special actions need to be
1604
297444daac68 Two docstring fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1601
diff changeset
   619
    taken when a field is created.
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   620
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   621
    Args:
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   622
      entity_properties: keyword arguments that correspond to entity
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   623
        properties and their values
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   624
      name: the name of the field to be created
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   625
    """
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   626
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   627
    if not entity_properties or (name not in entity_properties):
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   628
      raise InvalidArgumentError
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   629
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1516
diff changeset
   630
  def _updateField(self, entity, entity_properties, name):
1515
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   631
    """Hook called when a field is updated.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   632
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   633
    Base classes should override if any special actions need to be
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   634
    taken when a field is updated. The field is not updated if the
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   635
    method does not return a True value.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   636
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   637
    Args:
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   638
      entity: the unaltered entity
1626
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   639
      entity_properties: keyword arguments that correspond to entity
fe455c93cbf6 Remove unused imports, add missing blank lines, fix too long lines, fix docstring parameters to match the ones in method declaration, rename to short variable names in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1614
diff changeset
   640
        properties and their values
1515
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   641
      name: the name of the field to be changed
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   642
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   643
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   644
    if not entity:
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   645
      raise NoEntityError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   646
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   647
    if not entity_properties or (name not in entity_properties):
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   648
      raise InvalidArgumentError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   649
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   650
    return True
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   651
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   652
  def _onCreate(self, entity):
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   653
    """Called when an entity has been created.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   654
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   655
    Classes that override this can use it to do any post-creation operations.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   656
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   657
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   658
    if not entity:
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   659
      raise NoEntityError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   660
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   661
    sidebar.flush()
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   662
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   663
  def _onUpdate(self, entity):
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   664
    """Called when an entity has been updated.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   665
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   666
    Classes that override this can use it to do any post-update operations.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   667
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   668
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   669
    if not entity:
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   670
      raise NoEntityError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   671
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   672
  def _onDelete(self, entity):
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   673
    """Called when an entity has been deleted.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   674
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   675
    Classes that override this can use it to do any post-deletion operations.
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   676
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   677
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   678
    if not entity:
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   679
      raise NoEntityError