app/soc/logic/models/base.py
changeset 1115 0a723ff3d27c
parent 1106 e14b0995cf29
child 1165 4db62684ce13
equal deleted inserted replaced
1114:65c8d1bfe94b 1115:0a723ff3d27c
   129     Classes that override this can use it to do any post-deletion operations.
   129     Classes that override this can use it to do any post-deletion operations.
   130     """
   130     """
   131     
   131     
   132     pass
   132     pass
   133 
   133 
   134   def _keyName(self, **kwargs):
   134   def getKeyNameFromFields(self, fields):
   135     """Returns the KeyName constructed from kwargs for this type of entity.
   135     """Returns the KeyName constructed from kwargs for this type of entity.
   136 
   136 
   137     The KeyName is in the following format:
   137     The KeyName is in the following format:
   138     <key_value1>:<key_value2>:...:<key_valueN>
   138     <key_value1>:<key_value2>:...:<key_valueN>
   139     """
   139     """
   140 
   140 
   141     # get the KeyFieldNames for this entity
       
   142     key_field_names = self.getKeyFieldNames()
   141     key_field_names = self.getKeyFieldNames()
   143 
   142 
   144     # check if all given KeyFieldNames are valid for this entity
   143     # check if all given KeyFieldNames are valid for this entity
   145     if not all(key in key_field_names for key in kwargs.keys()):
   144     if not all(key in key_field_names for key in fields.keys()):
   146       raise Error("Some of the provided arguments are not key fields")
   145       raise Error("Some of the provided arguments are not key fields")
   147 
   146 
   148     # check if all key_field_names for this entity are present in kwargs
   147     # check if all key_field_names for this entity are present in fields
   149     if not all(field in kwargs.keys() for field in key_field_names):
   148     if not all(field in fields.keys() for field in key_field_names):
   150       raise Error("Not all the required key fields are present")
   149       raise Error("Not all the required key fields are present")
   151 
   150 
   152     # check if all kwargs.values() are non-false
   151     if not all(fields.values()):
   153     if not all(kwargs.values()):
       
   154       raise Error("Not all KeyValues are non-false")
   152       raise Error("Not all KeyValues are non-false")
   155 
   153 
   156     # construct the KeyValues in the order given by getKeyFieldNames()
   154     # construct the KeyValues in the order given by getKeyFieldNames()
   157     keyvalues = []
   155     keyvalues = []
   158     for key_field_name in key_field_names:
   156     for key_field_name in key_field_names:
   159       keyvalues.append(kwargs[key_field_name])
   157       keyvalues.append(fields[key_field_name])
   160 
   158 
   161     # construct the KeyName in the appropriate format
   159     # construct the KeyName in the appropriate format
   162     return '/'.join(keyvalues)
   160     return '/'.join(keyvalues)
   163 
   161 
   164   def getFullModelClassName(self):
   162   def getFullModelClassName(self):
   165     """Returns fully-qualified model module.class name string.
   163     """Returns fully-qualified model module.class name string.
   166     """ 
   164     """ 
   167     return '%s.%s' % (self._model.__module__, self._model.__name__)
   165     return '%s.%s' % (self._model.__module__, self._model.__name__)
   168 
   166 
   169   def getKeyValues(self, entity):
   167   def getKeyValuesFromEntity(self, entity):
   170     """Extracts the key values from entity and returns them.
   168     """Extracts the key values from entity and returns them.
   171 
   169 
   172     The default implementation uses the scope and link_id as key values.
   170     The default implementation uses the scope and link_id as key values.
   173 
   171 
   174     Args:
   172     Args:
   204     """
   202     """
   205 
   203 
   206     if not entity:
   204     if not entity:
   207       return None
   205       return None
   208 
   206 
   209     key_values = self.getKeyValues(entity)
   207     key_values = self.getKeyValuesFromEntity(entity)
   210     suffix = '/'.join(key_values)
   208     suffix = '/'.join(key_values)
   211 
   209 
   212     return suffix
   210     return suffix
   213 
   211 
   214   def getKeyFieldsFromDict(self, dictionary):
   212   def getKeyFieldsFromFields(self, dictionary):
   215     """Does any required massaging and filtering of dictionary.
   213     """Does any required massaging and filtering of dictionary.
   216 
   214 
   217     The resulting dictionary contains just the key names, and has any
   215     The resulting dictionary contains just the key names, and has any
   218     required translations/modifications performed.
   216     required translations/modifications performed.
   219 
   217 
   234 -      key_name: key name of entity
   232 -      key_name: key name of entity
   235     """
   233     """
   236 
   234 
   237     return self._model.get_by_key_name(key_name)
   235     return self._model.get_by_key_name(key_name)
   238 
   236 
   239   def getFromFields(self, **kwargs):
   237   def getFromKeyFields(self, fields):
   240     """Returns the entity for the specified key names, or None if not found.
   238     """Returns the entity for the specified key names, or None if not found.
   241 
   239 
   242     Args:
   240     Args:
   243       **kwargs: the fields of the entity that uniquely identifies it
   241       **kwargs: the fields of the entity that uniquely identifies it
   244     """
   242     """
   245 
   243 
   246     key_name = self.getKeyNameForFields(kwargs)
   244     if all(fields.values()):
   247 
   245       key_name = self.getKeyNameFromFields(fields)
   248     if key_name:
       
   249       entity = self.getFromKeyName(key_name)
   246       entity = self.getFromKeyName(key_name)
   250     else:
   247     else:
   251       entity = None
   248       entity = None
   252 
   249 
   253     return entity
   250     return entity
   254 
   251 
   255   def getFromFieldsOr404(self, **fields):
   252   def getFromKeyFieldsOr404(self, fields):
   256     """Like getFromFields but expects to find an entity.
   253     """Like getFromKeyFields but expects to find an entity.
   257 
   254 
   258     Raises:
   255     Raises:
   259       out_of_band.Error if no User entity is found
   256       out_of_band.Error if no User entity is found
   260     """
   257     """
   261 
   258 
   262     entity = self.getFromFields(**fields)
   259     entity = self.getFromKeyFields(fields)
   263 
   260 
   264     if entity:
   261     if entity:
   265       return entity
   262       return entity
   266 
   263 
   267     format_text = ugettext('"%(key)s" is "%(value)s"')
   264     format_text = ugettext('"%(key)s" is "%(value)s"')
   274     msg = ugettext(
   271     msg = ugettext(
   275       'There is no "%(name)s" where %(pairs)s.') % {
   272       'There is no "%(name)s" where %(pairs)s.') % {
   276         'name': self._name, 'pairs': joined_pairs}
   273         'name': self._name, 'pairs': joined_pairs}
   277 
   274 
   278     raise out_of_band.Error(msg, status=404)
   275     raise out_of_band.Error(msg, status=404)
   279 
       
   280   def getIfFields(self, fields):
       
   281     """Like getFromFieldsOr404 but returns None if not all fields are set.
       
   282 
       
   283     Raises:
       
   284       out_of_band.Error if no User entity is found and all fields were set
       
   285     """
       
   286 
       
   287     if not all(fields.values()):
       
   288       return None
       
   289 
       
   290     return self.getFromFieldsOr404(**fields)
       
   291 
       
   292   def getKeyNameForFields(self, fields):
       
   293     """Return a Datastore key_name for a Entity from the specified fields.
       
   294 
       
   295     Args:
       
   296       fields: the fields of the entity that uniquely identifies it
       
   297     """
       
   298 
       
   299     if not all(fields.values()):
       
   300       return None
       
   301 
       
   302     return self._keyName(**fields)
       
   303 
   276 
   304   def getForLimitAndOffset(self, limit, offset=0):
   277   def getForLimitAndOffset(self, limit, offset=0):
   305     """Returns entities for given offset and limit or None if not found.
   278     """Returns entities for given offset and limit or None if not found.
   306 
   279 
   307     Args:
   280     Args:
   436   def updateOrCreateFromFields(self, properties, fields):
   409   def updateOrCreateFromFields(self, properties, fields):
   437     """Like updateOrCreateFromKeyName, but resolves fields to a key_name first.
   410     """Like updateOrCreateFromKeyName, but resolves fields to a key_name first.
   438     """
   411     """
   439 
   412 
   440     # attempt to retrieve the existing entity
   413     # attempt to retrieve the existing entity
   441     key_name  = self.getKeyNameForFields(fields)
   414     key_name  = self.getKeyNameFromFields(fields)
   442 
   415 
   443     return self.updateOrCreateFromKeyName(properties, key_name)
   416     return self.updateOrCreateFromKeyName(properties, key_name)
   444 
   417 
   445   def isDeletable(self, entity):
   418   def isDeletable(self, entity):
   446     """Returns whether the specified entity can be deleted.
   419     """Returns whether the specified entity can be deleted.