427 if not silent: |
427 if not silent: |
428 self._onUpdate(entity) |
428 self._onUpdate(entity) |
429 |
429 |
430 return entity |
430 return entity |
431 |
431 |
432 def updateOrCreateFromKeyName(self, properties, key_name): |
432 def updateOrCreateFromKeyName(self, properties, key_name, silent=False): |
433 """Update existing entity, or create new one with supplied properties. |
433 """Update existing entity, or create new one with supplied properties. |
434 |
434 |
435 Args: |
435 Args: |
436 properties: dict with entity properties and their values |
436 properties: dict with entity properties and their values |
437 key_name: the key_name of the entity that uniquely identifies it |
437 key_name: the key_name of the entity that uniquely identifies it |
|
438 silent: if True, do not run the _onCreate hook |
438 |
439 |
439 Returns: |
440 Returns: |
440 the entity corresponding to the key_name, with any supplied |
441 the entity corresponding to the key_name, with any supplied |
441 properties changed, or a new entity now associated with the |
442 properties changed, or a new entity now associated with the |
442 supplied key_name and properties. |
443 supplied key_name and properties. |
450 for property_name in properties: |
451 for property_name in properties: |
451 self._createField(properties, property_name) |
452 self._createField(properties, property_name) |
452 |
453 |
453 # entity did not exist, so create one in a transaction |
454 # entity did not exist, so create one in a transaction |
454 entity = self._model.get_or_insert(key_name, **properties) |
455 entity = self._model.get_or_insert(key_name, **properties) |
|
456 |
|
457 if not silent: |
|
458 # a new entity has been created call _onCreate |
|
459 self._onCreate(entity) |
455 else: |
460 else: |
456 # If someone else already created the entity (due to a race), we |
461 # If someone else already created the entity (due to a race), we |
457 # should not update the propties (as they 'won' the race). |
462 # should not update the propties (as they 'won' the race). |
458 entity = self.updateEntityProperties(entity, properties, silent=True) |
463 entity = self.updateEntityProperties(entity, properties, silent=silent) |
459 |
|
460 if create_entity: |
|
461 # a new entity has been created call _onCreate |
|
462 self._onCreate(entity) |
|
463 else: |
|
464 # the entity has been updated call _onUpdate |
|
465 self._onUpdate(entity) |
|
466 |
464 |
467 return entity |
465 return entity |
468 |
466 |
469 def updateOrCreateFromFields(self, properties, silent=False): |
467 def updateOrCreateFromFields(self, properties, silent=False): |
470 """Creates a new entity with the supplied properties. |
468 """Update existing entity or creates a new entity with the supplied |
|
469 properties containing the key fields. |
471 |
470 |
472 Args: |
471 Args: |
473 properties: dict with entity properties and their values |
472 properties: dict with entity properties and their values |
474 silent: if True, do not run the _onCreate hook |
473 silent: if True, do not run the _onCreate hook |
475 """ |
474 """ |
478 self._createField(properties, property_name) |
477 self._createField(properties, property_name) |
479 |
478 |
480 if self._id_based: |
479 if self._id_based: |
481 entity = self._model(**properties) |
480 entity = self._model(**properties) |
482 entity.put() |
481 entity.put() |
|
482 |
|
483 if not silent: |
|
484 # call the _onCreate hook |
|
485 self._onCreate(entity) |
483 else: |
486 else: |
484 key_name = self.getKeyNameFromFields(properties) |
487 key_name = self.getKeyNameFromFields(properties) |
485 entity = self._model.get_or_insert(key_name, **properties) |
488 entity = self.updateOrCreateFromKeyName(properties, key_name, |
486 |
489 silent=silent) |
487 if not silent: |
|
488 self._onCreate(entity) |
|
489 |
490 |
490 return entity |
491 return entity |
491 |
492 |
492 def isDeletable(self, entity): |
493 def isDeletable(self, entity): |
493 """Returns whether the specified entity can be deleted. |
494 """Returns whether the specified entity can be deleted. |