Ejemplo n.º 1
0
def update(doc_ref: DocumentReference, *args, **kwargs) -> StatusCode:
    """Update a Firestore document.

    Args:
        doc_ref: Firestore reference to the document that will be updated.

    Raises:
        FirestoreConnectorError: if an arbitrary exception occurred after retrying.
            Caught by decorator to return error code.

    Returns:
        0 success (1st or 2nd time tried) or -1 exception (error after retrying
        a second time - from decorator).
    """
    try:
        doc_ref.update(*args, **kwargs)
        _update_last_edit(doc_ref)
        return StatusCode.SUCCESS

    except Exception:
        __log_exception(2, doc_ref)
        sleep(EXCEPTION_SLEEP_TIME)

        try:
            # Retry
            doc_ref.update(*args, **kwargs)
            _update_last_edit(doc_ref)
            return StatusCode.SUCCESS

        except Exception as exc:
            __log_exception(2, doc_ref, True)
            raise FirestoreConnectorError("update", exc)
Ejemplo n.º 2
0
def _update_last_edit(doc_ref: DocumentReference) -> None:
    """If the document reference points to the history collection
    then update its "last_edit" field to the currrent datetime. This
    datetame is parsed as a Timestamp in the document.

    Args:
        doc_ref: Firestore object reference to the document.
    """
    _path = doc_ref.path.split("/")
    if len(_path) == 2 and _path[0] == "history":
        # why is this using bare firestore update instead of update function?
        doc_ref.update({"last_edit": datetime.now(timezone.utc)})