app/soc/logic/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Mon, 17 Nov 2008 00:01:33 +0000
changeset 496 875bdc5741eb
parent 487 1a7591ff0051
child 499 d22e4fe8e64b
permissions -rw-r--r--
Temporary fix for creating a new user account This should probably be solved more elegantly, but edit_self is going to be used soon.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Helpers functions for updating different kinds of models in datastore.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    22
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    23
  '"Lennard de Rijk" <rijk0214@gmail.com>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
  ]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from google.appengine.ext import db
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    30
from django.utils.translation import ugettext_lazy
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
    31
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
    32
from soc.logic import dicts
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
from soc.logic import out_of_band
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
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
    36
class Error(Exception):
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
  """Base class for all exceptions raised by this module."""
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
    38
  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
    39
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
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 387
diff changeset
    41
class Logic:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  """Base logic for entity classes.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  The BaseLogic class functions specific to Entity classes by relying
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
  on the the child-classes to implement _model, _name and _key_name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
  """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
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
    48
  def __init__(self, model, base_model=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
    49
               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
    50
    """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
    51
    """
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    52
    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
    53
    self._base_model = base_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
    54
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    55
    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
    56
      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
    57
    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
    58
      self._name = self.getModelClassName()
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    59
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    60
    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
    61
      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
    62
    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
    63
      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
    64
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
  def _updateField(self, model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    """Hook called when a field is updated.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
    Base classes should override if any special actions need to be
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
    taken when a field is updated. The field is not updated if the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
    method does not return a True value.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
    return True
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    75
  def _keyName(self, **kwargs):
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
    76
    """Returns the KeyName constructed from kwargs for this type of entity
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    77
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
    78
    The KeyName is in the following format:
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    79
    entity.name:<key_value1>:<key_value2>:...:<key_valueN>
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    80
    """
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    81
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
    82
    # get the KeyFieldNames for this entity
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    83
    key_field_names = self.getKeyFieldNames()
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    84
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
    85
    # check if all given KeyFieldNames are valid for this entity
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    86
    if not all(key in key_field_names for key in kwargs.keys()):
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    87
      raise Error("Some of the provided arguments are not key fields")
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    88
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
    89
    # check if all key_field_names for this entity are present in kwargs
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    90
    if not all(field in kwargs.keys() for field in key_field_names):
447
83b49d60effd Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 442
diff changeset
    91
      raise Error("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
    92
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
    93
    # check if all kwargs.values() are non-false
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    94
    if not all(kwargs.values()):
447
83b49d60effd Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 442
diff changeset
    95
      raise Error("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
    96
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
    97
    # 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
    98
    keyvalues = []
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    99
    for key_field_name in key_field_names:
447
83b49d60effd Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 442
diff changeset
   100
      keyvalues.append(kwargs[key_field_name])
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   101
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
   102
    # construct the KeyName in the appropriate format
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   103
    return ":".join([self._name] + keyvalues)
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   104
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
   105
  def getModelClassName(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
   106
    """Returns model class name string.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   107
    """ 
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   108
    return self._model.__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
   109
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   110
  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
   111
    """Returns fully-qualified model module.class name string.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   112
    """ 
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   113
    return '%s.%s' % (self._model.__module__,
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   114
                      self.getModelClassName()) 
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   115
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   116
  def getKeyValues(self, entity):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   117
    """Exctracts the key values from entity and returns them
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   118
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   119
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   120
      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
   121
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   122
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   123
    raise NotImplementedError
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   124
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   125
  def getSuffixValues(self, entity):
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   126
    """Returns an array with the public values of the Key Fields
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   127
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   128
    Defaults to getKeyValues
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   129
    If any of the Key Fields should not be made public, this method
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   130
    should return their public counterparts instead.
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   131
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   132
    Args:
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   133
      entity: the entity for which to construct the suffix 
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   134
    """
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   135
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   136
    return self.getKeyValues(entity)
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   137
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   138
  def getKeyValuesFromFields(self, fields):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   139
    """Exctracts the key values from a dict and returns them
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   140
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   141
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   142
      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
   143
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   144
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   145
    raise NotImplementedError
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   146
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   147
  def getKeyFieldNames(self):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   148
    """Returns an array with the names of the Key Fields 
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   149
    """
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
    raise NotImplementedError
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   152
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   153
  def getKeySuffix(self, entity):
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   154
    """Returns a suffix for the specified entity or None if no entity specified
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   155
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   156
    Args:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   157
      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
   158
    """
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   159
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   160
    if not entity:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   161
      return None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   162
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 447
diff changeset
   163
    key_values = self.getSuffixValues(entity)
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   164
    suffix = '/'.join(key_values)
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
    return suffix
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   167
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   168
  def getKeyFieldsFromDict(self, dictionary):
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   169
    """Does any required massaging and filtering of dictionary
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   170
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   171
    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
   172
    required translations/modifications performed.
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   173
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   174
    Args:
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   175
      dictionary: The arguments to massage
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   176
    """
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   177
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   178
    keys = self.getKeyFieldNames()
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   179
    values = self.getKeyValuesFromFields(dictionary)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   180
    key_fields = dicts.zip(keys, values)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   181
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   182
    return key_fields
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   183
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
  def getFromKeyName(self, key_name):
487
1a7591ff0051 Fixed a typo in soc.logic.models.base
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   185
    """"Returns entity for key_name or None if not found.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
-
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
-    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
-      key_name: key name of entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
    return self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
  def getFromFields(self, **kwargs):
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   194
    """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
   195
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
      **kwargs: the fields of the entity that uniquely identifies it
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   200
    key_name = self.getKeyNameForFields(kwargs)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
    if key_name:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
      entity = self._model.get_by_key_name(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
    else:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
      entity = None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
    return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   209
  def getIfFields(self, fields):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
    """Returns entity for supplied link name if one exists.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
    Args:
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   213
      fields: the fields of the entity that uniquely identifies it
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
      * None if a field is false.
317
87215136c268 Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 308
diff changeset
   217
      * Entity for supplied fields
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
    Raises:
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
   220
      out_of_band.ErrorResponse if link name is not false, but no entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   221
      with the supplied link name exists in the Datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   222
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   223
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   224
    if not all(fields.values()):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   225
      # exit without error, to let view know that link_name was not supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   226
      return None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   227
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   228
    entity = self.getFromFields(**fields)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   229
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   230
    if entity:
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
   231
      # an entity exist for this link_name, so return that entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   232
      return entity
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   234
    format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   235
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   236
    msg_pairs = [format_text % {'key': key, 'value': value}
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   237
      for key, value in fields.iteritems()]
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   238
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   239
    joined_pairs = ' and '.join(msg_pairs)
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   240
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   241
    msg = ugettext_lazy(
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   242
      'There is no "%(name)s" where %(pairs)s.') % {
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   243
        'name': self._name, 'pairs': joined_pairs}
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   244
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   246
    # else: fields were supplied, but there is no Entity that has it
396
6af084bd290c Improve the error message for getIfFields
Sverre Rabbelier <srabbelier@gmail.com>
parents: 390
diff changeset
   247
    raise out_of_band.ErrorResponse(msg, status=404)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   248
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   249
  def getKeyNameForFields(self, fields):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   250
    """Return a Datastore key_name for a Entity from the specified fields.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   251
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   252
    Args:
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   253
      fields: the fields of the entity that uniquely identifies it
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   255
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   256
    if not all(fields.values()):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   257
      return None
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   258
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   259
    return self._keyName(**fields)
308
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
  def getForLimitAndOffset(self, limit, offset=0):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
    """Returns entities for given offset and limit or None if not found.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   264
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   265
      limit: max amount of entities to return
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   266
      offset: optional number of results to skip first; default zero.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   267
    """
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
    query = self._model.all()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   270
    return query.fetch(limit, offset)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   272
  def getForFields(self, properties, unique=False, limit=1000, offset=0):
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   273
    """Returns all entities that have the specified properties
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   274
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   275
    Args:
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   276
      properties: the properties that the entity should have
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   277
      unique: if set, only the first item from the resultset will be returned
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   278
      limit: max amount of entities to return
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   279
      offset: optional number of results to skip first; default zero.
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   280
    """
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   281
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   282
    if not properties:
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   283
      raise Error("Properties did not contain any values")
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   284
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   285
    format_text = '%(key)s = :%(key)s'
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   286
    msg_pairs = [format_text % {'key': key} for key in properties.iterkeys()]
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   287
    joined_pairs = ' AND '.join(msg_pairs)
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   288
    condition = 'WHERE %s' % joined_pairs
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   289
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   290
    query = self._model.gql(condition, **properties)
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   291
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   292
    if unique:
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   293
      return query.get()
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   294
447
83b49d60effd Some indention fixes and remove not needed variable in soc.logic.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 442
diff changeset
   295
    return query.fetch(limit, offset)
433
001b981be45e Add a getForFields method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   296
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
   297
  def buildTypedQueryString(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
   298
    """Returns a GQL query string compatible with PolyModel.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   299
    """
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   300
    return ''.join(self._buildTypedQueryParts())
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   301
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   302
  def _buildTypedQueryParts(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
   303
    if self._base_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
   304
      return [
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   305
          'SELECT * FROM ', self._base_model.__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
   306
          " WHERE inheritance_line = '", self.getFullModelClassName(), "'"]
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   307
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   308
    return ['SELECT * FROM ', self._model.__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
   309
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   310
  def buildOrderedQueryString(self, order_by=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
   311
    """Returns a an ordered GQL query string compatible with PolyModel.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   312
  
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   313
    Args:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   314
      order_by: optional field name by which to order the query results;
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   315
        default is None, in which case no ORDER BY clause is placed in
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   316
        the query string
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   317
    """
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   318
    return ''.join(self._buildOrderedQueryParts(order_by=order_by))
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   319
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   320
  def _buildOrderedQueryParts(self, order_by=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
   321
    query_str_parts = self._buildTypedQueryParts()
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   322
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   323
    if order_by:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   324
      query_str_parts.extend([' ORDER BY ', order_by])
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   325
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   326
    return query_str_parts
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   327
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   328
  def getEntitiesForLimitAndOffset(self, limit, offset=0, order_by=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
   329
    """Returns entities for given offset and limit or None if not found.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   330
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   331
    Args:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   332
      limit: max amount of entities to return
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   333
      offset: optional offset in entities list which defines first entity to
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   334
        return; default is zero (first 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
   335
    """
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   336
    query_string = self.buildOrderedQueryString(order_by=order_by)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   337
    query = db.GqlQuery(query_string)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   338
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   339
    return query.fetch(limit, offset)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   340
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   341
  def getNearestEntities(self, fields_to_try):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   342
    """Get entities just before and just after the described 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
   343
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   344
    Args:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   345
      fields_to_try: ordered list of key/value pairs that "describe"
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   346
        the desired entity (which may not necessarily exist), where key is
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   347
        the name of the field, and value is an instance of that field
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   348
        used in the comparison; if value is None, that field is skipped
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   349
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   350
    Returns:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   351
      a two-tuple: ([nearest_entities], 'field_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
   352
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   353
      nearest_entities: list of entities being those just before and just
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   354
        after the (possibly non-existent) entity identified by the first
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   355
        of the supplied (non-None) fields
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   356
          OR
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   357
        possibly None if query had no results for the supplied field
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   358
        that was used.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   359
    """
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   360
    # SELECT * FROM base_class WHERE inheritance_line = 'derived_class'
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   361
    typed_query_parts = self._buildTypedQueryParts()
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   362
  
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   363
    if self._base_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
   364
      typed_query_parts.append(' AND %s > :1')
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   365
    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
   366
      typed_query_parts.append(' WHERE %s > :1')
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   367
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   368
    typed_query_fmt = ''.join(typed_query_parts)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   369
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   370
    for field, value in fields_to_try:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   371
      if value is 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
   372
        # skip this not-supplied field
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   373
        continue
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   374
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   375
      query = db.GqlQuery(typed_query_fmt % field, value)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   376
      return query.fetch(1), field
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   377
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   378
    # all fields exhausted, and all had None values
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   379
    return (None, 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
   380
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   381
  def findNearestEntitiesOffset(width, fields_to_try):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   382
    """Finds offset of beginning of a range of entities around the nearest.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   383
  
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   384
    Args:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   385
      width: the width of the "found" window around the nearest User found
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   386
      fields_to_try:  see getNearestEntities()
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   387
    
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   388
    Returns:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   389
      an offset into the list of entities that is width/2 less than the
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   390
      offset of the first entity returned by getNearestEntities(), or zero
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   391
      if that offset would be less than zero
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   392
        OR
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   393
      None if there are no nearest entities or the offset of the beginning of
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   394
      the range cannot be found for some reason 
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   395
    """
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   396
    # find entity "nearest" to supplied fields
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   397
    nearest_entities, field = self.getNearestEntities(fields_to_try)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   398
  
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   399
    if not nearest_entities:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   400
      # no "nearest" entity, so indicate that with 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
   401
      return 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
   402
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   403
    nearest_entity = nearest_entities[0]
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   404
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   405
    # start search for beginning of nearest Users range at offset zero
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   406
    offset = 0
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   407
    entities = self.getEntitiesForLimitAndOffset(width, offset=offset)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   408
  
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   409
    while True:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   410
      for entity in entities:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   411
        if getattr(nearest_entity, field) == getattr(entity, field):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   412
          # nearest User found in current search range, so return a range start
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   413
          return max(0, (offset - (width/2)))
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   414
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   415
        offset = offset + 1
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   416
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   417
      # nearest User was not in the current search range, so fetch the next set
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   418
      entities = self.getEntitiesForLimitAndOffset(width, offset=offset)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   419
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   420
      if not entities:
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   421
        # nearest User never found, so indicate that with 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
   422
        break
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   423
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   424
    return 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
   425
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   426
  def updateModelProperties(self, model, model_properties):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   427
    """Update existing model entity using supplied model properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   428
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   429
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   430
      model: a model entity
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   431
      model_properties: keyword arguments that correspond to model entity
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   432
        properties and their values
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   433
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   434
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   435
      the original model entity with any supplied properties changed
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   436
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   437
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   438
    def update():
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   439
      return self._unsafeUpdateModelProperties(model, model_properties)
308
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
    return db.run_in_transaction(update)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   442
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   443
  def _unsafeUpdateModelProperties(self, model, model_properties):
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   444
    """(see updateModelProperties)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   445
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   446
    Like updateModelProperties(), but not run within a transaction.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   447
    """
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
    properties = model.properties()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   450
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   451
    for prop in properties.values():
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   452
      name = prop.name
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   453
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   454
      if not name in self._skip_properties and name in model_properties:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   455
        value = model_properties[prop.name]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   456
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   457
        if self._updateField(model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   458
          prop.__set__(model, value)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   459
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   460
    model.put()
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   461
    return model
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   462
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   463
  def updateOrCreateFromKeyName(self, properties, key_name):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   464
    """Update existing entity, or create new one with supplied properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   465
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   466
    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
   467
      properties: dict with entity properties and their values
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   468
      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
   469
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   470
    Returns:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   471
      the entity corresponding to the key_name, with any supplied
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   472
      properties changed, or a new entity now associated with the
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   473
      supplied key_name and properties.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   474
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   475
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   476
    entity = self.getFromKeyName(key_name)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   477
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   478
    if not entity:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   479
      # entity did not exist, so create one in a transaction
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   480
      entity = self._model.get_or_insert(key_name, **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
    # there is no way to be sure if get_or_insert() returned a new entity or
354
b931c74abbee Fix mistakes in docs and comments in logic/models/base.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 317
diff changeset
   483
    # got an existing one due to a race, so update with properties anyway,
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   484
    # in a transaction
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   485
    return self.updateModelProperties(entity, properties)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   486
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   487
  def updateOrCreateFromFields(self, properties, fields):
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   488
    """Like updateOrCreateFromKeyName, but resolves fields to a key_name first.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   489
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   490
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   491
    # attempt to retrieve the existing entity
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 433
diff changeset
   492
    key_name  = self.getKeyNameForFields(fields)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   493
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   494
    return self.updateOrCreateFromKeyName(properties, key_name)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   495
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   496
  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
   497
    """Returns whether the specified entity can be deleted.
f90f9b22751a Move TODO about isDeletable Sponsor logic method to sponsor.py module and extend a little bit doc string.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 363
diff changeset
   498
    
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
   499
    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
   500
      entity: an existing entity in datastore
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   501
    """
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
   502
    
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   503
    return True
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 354
diff changeset
   504
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   505
  def delete(self, entity):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   506
    """Delete existing entity from datastore.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   507
    
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   508
    Args:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   509
      entity: an existing entity in datastore
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   510
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   511
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   512
    entity.delete()