示例#1
0
    def delete(self, entity: T, transaction: Transaction = None):
        collection_ref = self.client.collection(self.path)
        document_ref = collection_ref.document(entity.id)

        if transaction:
            transaction.delete(document_ref)
        else:
            document_ref.delete()
示例#2
0
    def set(self, entity: T, transaction: Transaction = None) -> T:
        collection_ref = self.client.collection(self.path)
        document_ref = collection_ref.document(entity.id)

        if transaction:
            transaction.set(document_ref, entity.dict(exclude_none=True))
        else:
            document_ref.set(entity.dict(exclude_none=True))

        return entity
示例#3
0
    def delete(self,
               collection_path: str,
               id: str,
               transaction: Transaction = None):
        collection_ref = self.client.collection(collection_path)
        document_ref = collection_ref.document(id)

        if transaction:
            transaction.delete(document_ref)
        else:
            document_ref.delete()
示例#4
0
    def partial_update(self,
                       collection_path: str,
                       entity_id: str,
                       updates: Dict,
                       transaction: Transaction = None):
        collection_ref = self.client.collection(collection_path)
        document_ref = collection_ref.document(entity_id)

        if transaction:
            transaction.update(document_ref, updates)
        else:
            document_ref.update(updates)
示例#5
0
    def update(self,
               collection_path: str,
               entity: T,
               transaction: Transaction = None) -> T:
        collection_ref = self.client.collection(collection_path)
        document_ref = collection_ref.document(str(entity.id))

        if transaction:
            transaction.update(document_ref, entity.dict(exclude_none=True))
        else:
            document_ref.update(entity.dict(exclude_none=True))

        return entity
示例#6
0
文件: main.py 项目: LukasSlouka/demos
def increment_execution_counter(transaction: Transaction,
                                event_reference: DocumentReference,
                                finished_processing: bool):
    """Increments execution counter in a task

    :param transaction: transaction
    :param event_reference: event document reference
    :param finished_processing: flags whether the repeated task processing has been finished
    """
    event = event_reference.get().to_dict()
    transaction.update(
        event_reference, {
            'execution_counter': event.get('execution_counter', 0) + 1,
            'processed': finished_processing,
        })
示例#7
0
def _make_transaction_pb(txn_id, **txn_kwargs):
    from google.protobuf import empty_pb2
    from google.cloud.firestore_v1.services.firestore import client as firestore_client
    from google.cloud.firestore_v1.types import firestore
    from google.cloud.firestore_v1.types import write
    from google.cloud.firestore_v1.transaction import Transaction

    # Create a fake GAPIC ...
    firestore_api = mock.create_autospec(firestore_client.FirestoreClient,
                                         instance=True)
    # ... with a dummy ``BeginTransactionResponse`` result ...
    begin_response = firestore.BeginTransactionResponse(transaction=txn_id)
    firestore_api.begin_transaction.return_value = begin_response
    # ... and a dummy ``Rollback`` result ...
    firestore_api.rollback.return_value = empty_pb2.Empty()
    # ... and a dummy ``Commit`` result.
    commit_response = firestore.CommitResponse(
        write_results=[write.WriteResult()])
    firestore_api.commit.return_value = commit_response

    # Attach the fake GAPIC to a real client.
    client = _make_client()
    client._firestore_api_internal = firestore_api

    return Transaction(client, **txn_kwargs)
示例#8
0
    def where(self,
              collection_path: str,
              queries: Union[Query, List[Query]],
              transaction: Transaction = None):
        collection_ref = self.client.collection(collection_path)
        collection_query = collection_ref

        if not isinstance(queries, list):
            queries = [queries]

        for query in queries:
            collection_query = collection_query.where(query.field_path,
                                                      query.op_string,
                                                      query.value)

        if transaction:
            docs = transaction.get(collection_query)
        else:
            docs = collection_query.get()

        entities = []
        for doc in docs:
            entities.append(self.construct(doc.to_dict()))

        return entities
示例#9
0
    def create(self, entity: T, transaction: Transaction = None) -> T:
        collection_ref = self.client.collection(self.path)

        if entity.id is not None:
            document_ref = collection_ref.document(entity.id)
        else:
            document_ref = collection_ref.document()

        entity.id = document_ref.id

        if transaction:
            transaction.create(document_ref, entity.dict(exclude_none=True))
        else:
            document_ref.create(entity.dict(exclude_none=True))

        return entity
示例#10
0
    def transaction(self, **kwargs):
        """Get a transaction that uses this client.

        See :class:`~.firestore_v1.transaction.Transaction` for
        more information on transactions and the constructor arguments.

        Args:
            kwargs (Dict[str, Any]): The keyword arguments (other than
                ``client``) to pass along to the
                :class:`~.firestore_v1.transaction.Transaction`
                constructor.

        Returns:
            ~.firestore_v1.transaction.Transaction: A transaction
            attached to this client.
        """
        return Transaction(self, **kwargs)
示例#11
0
def create_tr(transaction: Transaction, word_create: models.WordCreate,
              tags: List[models.TagAct], taught: str, collect_word: str,
              collect_unknown: str, collect_session: str):
    """ 新規単語の追加で呼び出すトランザクション処理
    """
    # unknownsコレクションにある場合は削除する
    docs = db.collection(collect_unknown).where("word", "==",
                                                word_create.word).stream()
    for doc in docs:
        transaction.delete(doc._reference)
        system_service.dec_unknown()

    set_like = 0
    set_tags = []
    set_tags_cnt = {}
    for tag in tags:
        set_tags.append(tag["text"])
        set_tags_cnt[tag["text"]] = firestore.Increment(1)
        set_like = set_like + tag["pnt"]

    # wordsコレクションに追加
    word_data = models.WordAll(**word_create.dict()).dict()
    word_data["taught"] = taught
    word_data["like"] = set_like
    word_data["tags"] = set_tags
    word_data["tags_cnt"] = set_tags_cnt
    word_data["cnt"] = firestore.Increment(1)
    word_data["index"] = system_service.get_system_data()["word_cnt"]
    word_ref = db.collection(collect_word).document()
    transaction.set(word_ref, word_data)

    # sessionsコレクション更新
    session_ref = db.collection(collect_session).document(document_id=taught)
    session_data = {
        "teach_words": firestore.ArrayUnion([word_create.word]),
        "teach_refs": firestore.ArrayUnion([word_ref]),
        "teach_cnt": firestore.Increment(1),
    }
    transaction.set(session_ref, session_data, merge=True)

    return word_ref
示例#12
0
def _make_transaction(*args, **kwargs):
    from google.cloud.firestore_v1.transaction import Transaction

    return Transaction(*args, **kwargs)
示例#13
0
    def _get_helper(
        self,
        field_paths=None,
        use_transaction=False,
        not_found=False,
        # This should be an impossible case, but we test against it for
        # completeness
        return_empty=False,
        retry=None,
        timeout=None,
    ):
        from google.cloud.firestore_v1 import _helpers
        from google.cloud.firestore_v1.types import common
        from google.cloud.firestore_v1.types import document
        from google.cloud.firestore_v1.types import firestore
        from google.cloud.firestore_v1.transaction import Transaction

        # Create a minimal fake GAPIC with a dummy response.
        create_time = 123
        update_time = 234
        read_time = 345
        firestore_api = mock.Mock(spec=["batch_get_documents"])
        response = mock.create_autospec(firestore.BatchGetDocumentsResponse)
        response.read_time = read_time
        response.found = mock.create_autospec(document.Document)
        response.found.fields = {}
        response.found.create_time = create_time
        response.found.update_time = update_time

        client = _make_client("donut-base")
        client._firestore_api_internal = firestore_api
        document_reference = self._make_one("where", "we-are", client=client)

        response.found.name = None if not_found else document_reference._document_path
        response.missing = document_reference._document_path if not_found else None

        def WhichOneof(val):
            return "missing" if not_found else "found"

        response._pb = response
        response._pb.WhichOneof = WhichOneof
        firestore_api.batch_get_documents.return_value = iter(
            [response] if not return_empty else [])

        if use_transaction:
            transaction = Transaction(client)
            transaction_id = transaction._id = b"asking-me-2"
        else:
            transaction = None

        kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)

        snapshot = document_reference.get(field_paths=field_paths,
                                          transaction=transaction,
                                          **kwargs)

        self.assertIs(snapshot.reference, document_reference)
        if not_found or return_empty:
            self.assertIsNone(snapshot._data)
            self.assertFalse(snapshot.exists)
            self.assertIsNotNone(snapshot.read_time)
            self.assertIsNone(snapshot.create_time)
            self.assertIsNone(snapshot.update_time)
        else:
            self.assertEqual(snapshot.to_dict(), {})
            self.assertTrue(snapshot.exists)
            self.assertIs(snapshot.read_time, read_time)
            self.assertIs(snapshot.create_time, create_time)
            self.assertIs(snapshot.update_time, update_time)

        # Verify the request made to the API
        if field_paths is not None:
            mask = common.DocumentMask(field_paths=sorted(field_paths))
        else:
            mask = None

        if use_transaction:
            expected_transaction_id = transaction_id
        else:
            expected_transaction_id = None

        firestore_api.batch_get_documents.assert_called_once_with(
            request={
                "database": client._database_string,
                "documents": [document_reference._document_path],
                "mask": mask,
                "transaction": expected_transaction_id,
            },
            metadata=client._rpc_metadata,
            **kwargs,
        )
    async def _get_helper(
        self,
        field_paths=None,
        use_transaction=False,
        not_found=False,
        retry=None,
        timeout=None,
    ):
        from google.api_core.exceptions import NotFound
        from google.cloud.firestore_v1 import _helpers
        from google.cloud.firestore_v1.types import common
        from google.cloud.firestore_v1.types import document
        from google.cloud.firestore_v1.transaction import Transaction

        # Create a minimal fake GAPIC with a dummy response.
        create_time = 123
        update_time = 234
        firestore_api = AsyncMock(spec=["get_document"])
        response = mock.create_autospec(document.Document)
        response.fields = {}
        response.create_time = create_time
        response.update_time = update_time

        if not_found:
            firestore_api.get_document.side_effect = NotFound("testing")
        else:
            firestore_api.get_document.return_value = response

        client = _make_client("donut-base")
        client._firestore_api_internal = firestore_api

        document = self._make_one("where", "we-are", client=client)

        if use_transaction:
            transaction = Transaction(client)
            transaction_id = transaction._id = b"asking-me-2"
        else:
            transaction = None

        kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)

        snapshot = await document.get(
            field_paths=field_paths,
            transaction=transaction,
            **kwargs,
        )

        self.assertIs(snapshot.reference, document)
        if not_found:
            self.assertIsNone(snapshot._data)
            self.assertFalse(snapshot.exists)
            self.assertIsNone(snapshot.read_time)
            self.assertIsNone(snapshot.create_time)
            self.assertIsNone(snapshot.update_time)
        else:
            self.assertEqual(snapshot.to_dict(), {})
            self.assertTrue(snapshot.exists)
            self.assertIsNone(snapshot.read_time)
            self.assertIs(snapshot.create_time, create_time)
            self.assertIs(snapshot.update_time, update_time)

        # Verify the request made to the API
        if field_paths is not None:
            mask = common.DocumentMask(field_paths=sorted(field_paths))
        else:
            mask = None

        if use_transaction:
            expected_transaction_id = transaction_id
        else:
            expected_transaction_id = None

        firestore_api.get_document.assert_called_once_with(
            request={
                "name": document._document_path,
                "mask": mask,
                "transaction": expected_transaction_id,
            },
            metadata=client._rpc_metadata,
            **kwargs,
        )