Exemple #1
0
def _parse_batch_get(get_doc_response, reference_map, client):
    """Parse a `BatchGetDocumentsResponse` protobuf.

    Args:
        get_doc_response (~google.cloud.proto.firestore.v1.\
            firestore_pb2.BatchGetDocumentsResponse): A single response (from
            a stream) containing the "get" response for a document.
        reference_map (Dict[str, .DocumentReference]): A mapping (produced
            by :func:`_reference_info`) of fully-qualified document paths to
            document references.
        client (:class:`~google.cloud.firestore_v1.client.Client`):
            A client that has a document factory.

    Returns:
       [.DocumentSnapshot]: The retrieved snapshot.

    Raises:
        ValueError: If the response has a ``result`` field (a oneof) other
            than ``found`` or ``missing``.
    """
    result_type = get_doc_response.WhichOneof("result")
    if result_type == "found":
        reference = _get_reference(get_doc_response.found.name, reference_map)
        data = _helpers.decode_dict(get_doc_response.found.fields, client)
        snapshot = DocumentSnapshot(
            reference,
            data,
            exists=True,
            read_time=get_doc_response.read_time,
            create_time=get_doc_response.found.create_time,
            update_time=get_doc_response.found.update_time,
        )
    elif result_type == "missing":
        reference = _get_reference(get_doc_response.missing, reference_map)
        snapshot = DocumentSnapshot(
            reference,
            None,
            exists=False,
            read_time=get_doc_response.read_time,
            create_time=None,
            update_time=None,
        )
    else:
        raise ValueError(
            "`BatchGetDocumentsResponse.result` (a oneof) had a field other "
            "than `found` or `missing` set, or was unset"
        )
    return snapshot
Exemple #2
0
 def _cancel_game(self,
                  game_snap: DocumentSnapshot,
                  players: list,
                  reason: str = "insufficient players") -> None:
     # Cancel the game
     game_dict = game_snap.to_dict()
     field_updates = {
         'to_be_deleted': True,
     }
     game_snap.reference.update(field_updates)
     log_message(message="Cancelled the game (set to_be_deleted flag)",
                 game_id=game_dict.get("id"))
     notif_message = messages.NOTIFY_GAME_CANCELLED_EVENT_MSG_FMT.format(
         header=messages.game_sms_header(hashtag=game_dict.get('hashtag')),
         game=game_dict.get("hashtag"),
         reason=reason)
     self._notify_players(game_id=game_dict.get("id"),
                          players=players,
                          message=notif_message)
def build_document_snapshot(
    *,
    collection_name: str = "col",
    document_id: str = "doc",
    client: typing.Optional[BaseClient] = None,
    data: typing.Optional[typing.Dict] = None,
    exists: bool = True,
    create_time: typing.Optional[Timestamp] = None,
    read_time: typing.Optional[Timestamp] = None,
    update_time: typing.Optional[Timestamp] = None,
) -> DocumentSnapshot:
    return DocumentSnapshot(
        DocumentReference(collection_name, document_id, client=client),
        data or {"hello", "world"},
        exists=exists,
        read_time=read_time or build_timestamp(),
        create_time=create_time or build_timestamp(),
        update_time=update_time or build_timestamp(),
    )
Exemple #4
0
 def from_snapshot(cls, doc: firestore_document.DocumentSnapshot):
     data = doc.to_dict()
     return cls.from_dict(data)
Exemple #5
0
def _to_model(doc: DocumentSnapshot) -> Group:
    data: dict = doc.to_dict()
    data["id"] = doc.id
    return Group(**data)
def _to_model(doc: DocumentSnapshot) -> Stuff:
    data = doc.to_dict()
    data["doc_id"] = doc.id
    return Stuff(**data)
Exemple #7
0
def _make_document_snapshot(*args, **kwargs):
    from google.cloud.firestore_v1.document import DocumentSnapshot

    return DocumentSnapshot(*args, **kwargs)