app/soc/logic/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Mon, 09 Mar 2009 23:56:14 +0000
changeset 1780 210ab3d5d6ea
parent 1696 7b120f7f9a7a
child 2030 a21ee5f0591d
permissions -rw-r--r--
Added a getFromKeyNameOr404 to base.Logic This also fixes a bug introduced in r2437 where this method was used although it didn't exist yet. Patch by: Sverre Rabbelier
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__ = [
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    22
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
634
ad58da0b78e8 Changed email address in logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 608
diff changeset
    23
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
  ]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from google.appengine.ext import db
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 961
diff changeset
    30
from django.utils.translation import ugettext
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    31
1106
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 986
diff changeset
    32
from soc.cache import sidebar
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
    33
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
    34
from soc.views import out_of_band
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
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
    37
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
    38
  """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
    39
  """
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    40
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    41
  pass
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    42
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
    43
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    44
class InvalidArgumentError(Error):
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    45
  """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
    46
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    47
  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
    48
  """
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
  pass
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    51
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
    52
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    53
class NoEntityError(InvalidArgumentError):
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    54
  """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
    55
  """
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
    56
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
    57
  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
    58
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
    59
642
671dc971c3d1 Allow for extra dynamic fields for the create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 634
diff changeset
    60
class Logic(object):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
  """Base logic for entity classes.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  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
    64
  on arguments passed to __init__.
308
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
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    67
  def __init__(self, model, base_model=None, scope_logic=None,
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
    68
               name=None, skip_properties=None):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    69
    """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
    70
    """
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    71
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
    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
    73
    self._base_model = base_model
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    74
    self._scope_logic = scope_logic
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    75
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
    76
    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
    77
      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
    78
    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
    79
      self._name =  self._model.__name__
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 642
diff changeset
    80
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
    81
    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
    82
      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
    83
    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
    84
      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
    85
606
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    86
  def getModel(self):
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
    87
    """Returns the model this logic class uses.
606
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    88
    """
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    89
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    90
    return self._model
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 596
diff changeset
    91
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    92
  def getScopeLogic(self):
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
    93
    """Returns the logic of the enclosing scope.
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    94
    """
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    95
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    96
    return self._scope_logic
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 669
diff changeset
    97
679
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
    98
  def getScopeDepth(self):
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
    99
    """Returns the scope depth for this entity.
679
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   100
689
46db8c4bbffc Add missing dots in docstrings in modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 679
diff changeset
   101
    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
   102
    """
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   103
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   104
    if not self._scope_logic:
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   105
      return 0
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
    depth = self._scope_logic.logic.getScopeDepth()
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   108
    return None if (depth is None) else (depth + 1)
77a286ff6667 Introduce dynamic scope_path regexps
Sverre Rabbelier <srabbelier@gmail.com>
parents: 673
diff changeset
   109
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   110
  def getKeyNameFromFields(self, fields):
1218
569a3fe9cb88 Cleaned up getKeyNameFromFields in Logic base.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1183
diff changeset
   111
    """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
   112
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
   113
    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
   114
    <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
   115
    """
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   116
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   117
    if not fields:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   118
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   119
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   120
    key_field_names = self.getKeyFieldNames()
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   121
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   122
    # 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
   123
    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
   124
      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
   125
1218
569a3fe9cb88 Cleaned up getKeyNameFromFields in Logic base.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1183
diff changeset
   126
    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
   127
      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
   128
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
   129
    # 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
   130
    keyvalues = []
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   131
    for key_field_name in key_field_names:
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   132
      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
   133
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
   134
    # 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
   135
    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
   136
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   137
  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
   138
    """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
   139
    """
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   140
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
   141
    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
   142
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   143
  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
   144
    """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
   145
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   146
    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
   147
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   148
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   149
      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
   150
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   151
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   152
    if not entity:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   153
      raise NoEntityError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   154
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   155
    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
   156
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   157
  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
   158
    """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
   159
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   160
    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
   161
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   162
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   163
      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
   164
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   165
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   166
    if not all( (i in fields for i in ['scope_path', 'link_id']) ):
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   167
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   168
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   169
    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
   170
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   171
  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
   172
    """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
   173
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 534
diff changeset
   174
    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
   175
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   176
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
    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
   178
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   179
  def getKeySuffix(self, entity):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 487
diff changeset
   180
    """Returns a suffix for the specified entity or None if no entity specified.
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   181
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   182
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   183
      entity: the entity for which to get the suffix
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   184
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   185
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   186
    if not entity:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   187
      return None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   188
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   189
    key_values = self.getKeyValuesFromEntity(entity)
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   190
    suffix = '/'.join(key_values)
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   191
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   192
    return suffix
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   193
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   194
  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
   195
    """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
   196
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   197
    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
   198
    required translations/modifications performed.
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
    Args:
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   201
      dictionary: The arguments to massage
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   202
    """
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   203
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   204
    if not dictionary:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   205
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   206
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   207
    keys = self.getKeyFieldNames()
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   208
    values = self.getKeyValuesFromFields(dictionary)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   209
    key_fields = dicts.zip(keys, values)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   210
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   211
    return key_fields
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   212
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
  def getFromKeyName(self, key_name):
487
1a7591ff0051 Fixed a typo in soc.logic.models.base
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   214
    """"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
   215
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   216
    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
   217
      key_name: key name of entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   218
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   219
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   220
    if not key_name:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   221
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   222
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   223
    return self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   224
1780
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   225
  def getFromKeyNameOr404(self, key_name):
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   226
    """Like getFromKeyName but expects to find an entity.
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   227
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   228
    Raises:
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   229
      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
   230
    """
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   231
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   232
    entity = self.getFromKeyName(key_name)
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   233
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   234
    if entity:
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   235
      return 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
    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
   238
        'name': self._name, 'key_name': key_name}
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
    raise out_of_band.Error(msg, status=404)
210ab3d5d6ea Added a getFromKeyNameOr404 to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1696
diff changeset
   241
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   242
  def getFromKeyFields(self, fields):
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   243
    """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
   244
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
    Args:
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   246
      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
   247
        uniquely identifies it
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   248
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   250
    if not fields:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   251
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   252
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
   253
    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
   254
bea3987b2040 Updated docstrings and used keyfields where necessary in soc/logic/models/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   255
    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
   256
      key_name = self.getKeyNameFromFields(key_fields)
672
c8f9281f535b Some more cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 671
diff changeset
   257
      entity = self.getFromKeyName(key_name)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   258
    else:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   259
      entity = None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
    return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   263
  def getFromKeyFieldsOr404(self, fields):
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   264
    """Like getFromKeyFields but expects to find an entity.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   265
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
    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
   267
      out_of_band.Error if no entity is found
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   268
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   269
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   270
    entity = self.getFromKeyFields(fields)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   272
    if entity:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   273
      return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   274
1672
9b6cc71dd70c Only show key_fields in error message
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1626
diff changeset
   275
    key_fields = self.getKeyFieldsFromFields(fields)
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 961
diff changeset
   276
    format_text = ugettext('"%(key)s" is "%(value)s"')
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   277
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   278
    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
   279
      for key, value in key_fields.iteritems()]
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   280
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   281
    joined_pairs = ' and '.join(msg_pairs)
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   282
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 961
diff changeset
   283
    msg = ugettext(
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   284
      'There is no "%(name)s" where %(pairs)s.') % {
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   285
        'name': self._name, 'pairs': joined_pairs}
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   286
592
be98a2f5d8a2 Fixed an outstanding TODO for tlarsen
Sverre Rabbelier <srabbelier@gmail.com>
parents: 565
diff changeset
   287
    raise out_of_band.Error(msg, status=404)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   288
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   289
  def getForFields(self, filter=None, unique=False,
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   290
                   limit=1000, offset=0, order=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
   291
    """Returns all entities that have the specified properties.
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   292
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   293
    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
   294
      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
   295
      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
   296
      limit: the amount of entities to fetch at most
4db62684ce13 Unify 'getForFields' and 'getForLimitAndOffset'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1115
diff changeset
   297
      offset: the position to start at
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   298
      order: a list with the sort order
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   299
    """
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   300
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   301
    if unique:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   302
      limit = 1
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   303
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   304
    query = self.getQueryForFields(filter=filter, order=order)
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   305
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   306
    result = query.fetch(limit, offset)
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   307
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   308
    if unique:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   309
      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
   310
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   311
    return result
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   312
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   313
  def getQueryForFields(self, filter=None, order=None):
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   314
    """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
   315
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   316
    Args:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   317
      filter: a dict for the properties that the entities should have
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   318
      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
   319
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   320
    Returns:
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   321
      - 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
   322
    """
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   323
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   324
    if not filter:
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   325
      filter = {}
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   326
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   327
    if not order:
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   328
      order = []
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   329
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   330
    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
   331
    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
   332
      raise InvalidArgumentError
1179
427d2ec42823 Rewrite getForFields to use GQL instead of the Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1165
diff changeset
   333
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
   334
    query = db.Query(self._model)
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   335
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   336
    for key, value in filter.iteritems():
1696
7b120f7f9a7a Small optimization in getForFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1694
diff changeset
   337
      if isinstance(value, list) and len(value) == 1:
7b120f7f9a7a Small optimization in getForFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1694
diff changeset
   338
        value = value[0]
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   339
      if isinstance(value, list):
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   340
        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
   341
        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
   342
      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
   343
        query.filter(key, value)
723
69e5130e4a0a Make getForFields use the db.Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 689
diff changeset
   344
1614
797f5ae462e7 Convert getForFields back to the Query API and add tests
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1604
diff changeset
   345
    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
   346
      query.order(key)
1183
03db1f6d222a Make getForFields work with no filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1179
diff changeset
   347
1694
d388ff2fbe90 Refactored part of getForFields into a getQueryForFields method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1672
diff changeset
   348
    return query
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   349
1510
bc6dc3bb8d2a Remove _silentUpdateEntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1507
diff changeset
   350
  def updateEntityProperties(self, entity, entity_properties, silent=False):
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   351
    """Update existing entity using supplied properties.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   352
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   353
    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
   354
      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
   355
      entity_properties: keyword arguments that correspond to entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   356
        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
   357
      silent: iff True does not call _onUpdate method
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   358
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   359
    Returns:
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   360
      The original entity with any supplied properties changed.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   361
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   362
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   363
    if not entity:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   364
      raise NoEntityError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   365
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   366
    if not entity_properties:
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   367
      raise InvalidArgumentError
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   368
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   369
    properties = self._model.properties()
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   370
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   371
    for name, prop in properties.iteritems():
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   372
      # if the property is not updatable or is not updated, skip it
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   373
      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
   374
        continue
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   375
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1516
diff changeset
   376
      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
   377
        value = entity_properties[name]
1514
4a233f5a4264 Minor cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1513
diff changeset
   378
        prop.__set__(entity, value)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   379
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   380
    entity.put()
1549
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   381
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   382
    # call the _onUpdate method
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   383
    if not silent:
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   384
      self._onUpdate(entity)
bd07b231fe39 Removed updateEntityProperties's transactionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1518
diff changeset
   385
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   386
    return entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   387
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   388
  def updateOrCreateFromKeyName(self, properties, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   389
    """Update existing entity, or create new one with supplied properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   390
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   391
    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
   392
      properties: dict with entity properties and their values
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   393
      key_name: the key_name of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   394
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   395
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   396
      the entity corresponding to the key_name, with any supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   397
      properties changed, or a new entity now associated with the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   398
      supplied key_name and properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   399
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   400
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   401
    entity = self.getFromKeyName(key_name)
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   402
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   403
    create_entity = not entity
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   404
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   405
    if create_entity:
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   406
      for property_name in properties:
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   407
        self._createField(properties, property_name)
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   408
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   409
      # entity did not exist, so create one in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   410
      entity = self._model.get_or_insert(key_name, **properties)
1516
8df06dc877aa Do not update newly created model properties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1515
diff changeset
   411
    else:
8df06dc877aa Do not update newly created model properties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1515
diff changeset
   412
      # 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
   413
      # should not update the propties (as they 'won' the race).
8df06dc877aa Do not update newly created model properties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1515
diff changeset
   414
      entity = self.updateEntityProperties(entity, properties, silent=True)
1510
bc6dc3bb8d2a Remove _silentUpdateEntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1507
diff changeset
   415
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   416
    if create_entity:
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   417
      # a new entity has been created call _onCreate
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   418
      self._onCreate(entity)
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   419
    else:
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   420
      # the entity has been updated call _onUpdate
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   421
      self._onUpdate(entity)
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   422
608
77bffba4e946 Added _onCreate _onUpdate and _onDelete methods to logic/models/base.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 606
diff changeset
   423
    return entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   424
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   425
  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
   426
    """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
   427
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
   428
    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
   429
      entity: an existing entity in datastore
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   430
    """
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   431
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   432
    return True
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   433
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   434
  def delete(self, entity):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   435
    """Delete existing entity from datastore.
1513
13efb916a73c Add argument validation to base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1512
diff changeset
   436
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   437
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   438
      entity: an existing entity in datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   439
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   440
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   441
    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
   442
    # 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
   443
    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
   444
1601
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   445
  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
   446
    """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
   447
    """
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   448
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   449
    chunk = 999
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   450
    offset = 0
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   451
    result = []
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   452
    more = True
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   453
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   454
    while(more):
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   455
      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
   456
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   457
      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
   458
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   459
      if more:
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   460
        del data[chunk]
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   461
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   462
      result.extend(data)
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   463
      offset = offset + chunk
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   464
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   465
    return result
172e7de6c004 Make it possible to work on all entities in a query
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1574
diff changeset
   466
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   467
  def _createField(self, entity_properties, name):
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   468
    """Hook called when a field is created.
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   469
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   470
    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
   471
    specified) on an entity that is being created.
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   472
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   473
    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
   474
    taken when a field is created.
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   475
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   476
    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
   477
      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
   478
        properties and their values
1518
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   479
      name: the name of the field to be created
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   480
    """
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   481
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   482
    if not entity_properties or (name not in entity_properties):
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   483
      raise InvalidArgumentError
f6f43a1675eb Add a _createField hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   484
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1516
diff changeset
   485
  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
   486
    """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
   487
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   488
    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
   489
    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
   490
    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
   491
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   492
    Args:
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   493
      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
   494
      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
   495
        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
   496
      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
   497
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   498
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   499
    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
   500
      raise NoEntityError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   501
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   502
    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
   503
      raise InvalidArgumentError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   504
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   505
    return True
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   506
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   507
  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
   508
    """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
   509
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   510
    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
   511
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   512
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   513
    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
   514
      raise NoEntityError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   515
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   516
    sidebar.flush()
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   517
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   518
  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
   519
    """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
   520
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   521
    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
   522
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   523
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   524
    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
   525
      raise NoEntityError
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   526
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   527
  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
   528
    """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
   529
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   530
    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
   531
    """
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   532
75de65fcb017 Move the hooks in base.Logic to the bottom of the class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1514
diff changeset
   533
    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
   534
      raise NoEntityError