app/soc/logic/models/base.py
changeset 410 2af7f84f4fc7
parent 407 3cf5630d86d1
child 429 e50e18936f06
equal deleted inserted replaced
409:9d24850db88f 410:2af7f84f4fc7
    46     method does not return a True value.
    46     method does not return a True value.
    47     """
    47     """
    48 
    48 
    49     return True
    49     return True
    50 
    50 
       
    51   def getKeyValues(self, entity):
       
    52     """Exctracts the key values from entity and returns them
       
    53 
       
    54     Args:
       
    55       entity: the entity from which to extract the key values
       
    56     """
       
    57 
       
    58     raise NotImplementedError
       
    59 
       
    60   def getKeyValuesFromFields(self, fields):
       
    61     """Exctracts the key values from a dict and returns them
       
    62 
       
    63     Args:
       
    64       fields: the dict from which to extract the key values
       
    65     """
       
    66 
       
    67     raise NotImplementedError
       
    68 
       
    69   def getKeyFieldNames(self):
       
    70     """Returns an array with the names of the Key Fields 
       
    71     """
       
    72 
       
    73     raise NotImplementedError
       
    74 
       
    75   def getKeySuffix(self, entity):
       
    76     """Returns a suffix for the specified entity or None if no entity specified
       
    77 
       
    78     Args:
       
    79       entity: the entity for which to get the suffix
       
    80     """
       
    81 
       
    82     if not entity:
       
    83       return None
       
    84 
       
    85     key_values = self.getKeyValues(entity)
       
    86     suffix = '/'.join(key_values)
       
    87 
       
    88     return suffix
       
    89 
    51   def getFromKeyName(self, key_name):
    90   def getFromKeyName(self, key_name):
    52     """"Returns User entity for key_name or None if not found.
    91     """"Returns User entity for key_name or None if not found.
    53 -
    92 -
    54 -    Args:
    93 -    Args:
    55 -      key_name: key name of entity
    94 -      key_name: key name of entity
   125     if not all(kwargs.values()):
   164     if not all(kwargs.values()):
   126       return None
   165       return None
   127 
   166 
   128     return self._keyName(**kwargs)
   167     return self._keyName(**kwargs)
   129 
   168 
   130   def getEmptyKeyFields(self):
       
   131     """Returns an dict with all the entities key_fields set to None
       
   132     """
       
   133 
       
   134     kwargs = {}
       
   135 
       
   136     for field in self._model.KEY_FIELDS:
       
   137       kwargs[field] = None
       
   138 
       
   139     return kwargs
       
   140 
       
   141   def constructKeyNameSuffix(self, entity):
       
   142     """Constructs a suffix from the specified fields
       
   143 
       
   144     The resulting suffix is constructed by adding a '/' after all
       
   145     key fields. The suffix is constructed in the order as specified
       
   146     in the model's key_fields property. The suffix will not have a
       
   147     trailing '/'.
       
   148 
       
   149     Args:
       
   150       fields: a dictionary with the values for all the key_fields
       
   151           of this entity.
       
   152     """
       
   153 
       
   154     if not entity:
       
   155       return None
       
   156     
       
   157     suffix = []
       
   158 
       
   159     for field in entity.KEY_FIELDS:
       
   160       # Four hours wasted on this line, because apparently passing in a dict
       
   161       # one time, and a db.Model the next time, things get rather hard to debug
       
   162       value = entity.__getattribute__(field)
       
   163       suffix.append(value)
       
   164 
       
   165     res = '/'.join(suffix)
       
   166     return res
       
   167 
       
   168   def extractKeyFields(self, fields):
       
   169     """Extracts all the fields from that are in the mode's key_fields property
       
   170 
       
   171     Args:
       
   172       fields: A dict from which the fields should be extracted
       
   173     """
       
   174 
       
   175     key_fields = {}
       
   176 
       
   177     for key, value in fields.iteritems():
       
   178       if key in self._model.KEY_FIELDS:
       
   179         key_fields[key] = value
       
   180 
       
   181     return key_fields
       
   182 
       
   183   def getForLimitAndOffset(self, limit, offset=0):
   169   def getForLimitAndOffset(self, limit, offset=0):
   184     """Returns entities for given offset and limit or None if not found.
   170     """Returns entities for given offset and limit or None if not found.
   185 
   171 
   186     Args:
   172     Args:
   187       limit: max amount of entities to return
   173       limit: max amount of entities to return