def update_document(self, doc_ref: DocumentReference, update_dict: Dict):
     """
     Update a Firestore document
     :param doc_ref: a reference to the document
     :param update_dict: a dict with keys to be created/updated and values for them
     :return: Nothing
     """
     doc_ref.update(update_dict)
 def delete_doc(self, doc_ref: DocumentReference):
     """
     Deletes a document from Firestore
     :param doc_ref: a reference to the document
     :return: Nothing
     """
     try:
         doc_ref.delete()
     except Exception as e:
         logger.exception(
             UTILS, f'Error while trying to delete document. Error {e}')
Esempio n. 3
0
        def update_in_transaction(transaction: Transaction,
                                  doc_ref: DocumentReference):
            credentials_doc = doc_ref.get(transaction=transaction)
            transaction.update(doc_ref, {
                IN_USE_KEY: True,
                USED_BY_KEY: used_by_id
            })

            return credentials_doc.id, credentials_doc.to_dict()[VALUE_KEY]
            def update_in_transaction(transaction: Transaction,
                                      doc_ref: DocumentReference):
                already_processed = doc_ref.get(
                    transaction=transaction).to_dict()[field_name]
                if already_processed:
                    return None

                transaction.update(doc_ref, {field_name: True})
                runners_dict[doc_ref.id] = doc_ref
                return doc_ref
 def get_doc_content(self, doc_ref: DocumentReference) -> Optional[Dict]:
     """
     Return a dict with values from a Firestore document
     :param doc_ref: a reference to the document
     :return: the dict
     """
     try:
         return doc_ref.get().to_dict()
     except Exception as e:
         # Document doesn't exist
         return None
 def update_in_transaction(transaction: Transaction,
                           doc_ref: DocumentReference):
     content = doc_ref.get(transaction=transaction).to_dict()
     messages = content[MESSAGES_KEY]
     messages.append({
         INDEX_KEY:
         max(content[LAST_MESSAGE_READ_KEY] + 1,
             messages[-1][INDEX_KEY] + 1 if messages else 0),
         MESSAGE_KEY:
         message,
     })
     transaction.update(doc_ref, {
         MESSAGES_KEY: messages,
     })
        def update_in_transaction(transaction: Transaction,
                                  doc_ref: DocumentReference):
            try:
                current_num_of_fails = doc_ref.get(
                    transaction=transaction).get(AGENT_HANDLER_NUM_FAILS_KEY)
            except Exception as e:
                logger.info(UTILS, f'Handler is already deleted. Error {e}')
                return None

            if current_num_of_fails >= MAX_NUMBER_OF_HANDLER_FAILS:
                transaction.delete(doc_ref)
            else:
                transaction.update(
                    doc_ref, {
                        AGENT_HANDLER_NUM_FAILS_KEY: current_num_of_fails + 1,
                    })
 def finish_monitoring_task(self,
                            doc_ref: DocumentReference,
                            listener: bool = None,
                            worker: bool = None,
                            update: bool = None):
     """
     Set proper keys after finishing work with a task
     :param update: set update key to false
     :param worker: release a task from worker
     :param listener: release a task from listener
     :param doc_ref: a reference to the document
     :return: Nothing
     """
     try:
         if not listener:
             doc_ref.update({
                 TIME_TO_UPDATE_KEY:
                 datetime.now(pytz.UTC) + timedelta(
                     seconds=doc_ref.get().to_dict()[UPDATE_PERIOD_KEY])
             })
         if listener:
             doc_ref.update({
                 LISTENER_SET_KEY: False,
             })
         if worker:
             doc_ref.update({
                 WORKER_SET_KEY: False,
             })
         if update:
             doc_ref.update({
                 UPDATING_KEY: False,
             })
     except Exception as e:
         # Document doesn't exist
         logger.warning(
             UTILS, f'Unsuccessful finish to task monitoring. Error: {e}')
         return None
Esempio n. 9
0
def parse_cursor(cursor, client):
    from google.cloud.firestore_v1beta1 import DocumentReference
    from google.cloud.firestore_v1beta1 import DocumentSnapshot

    if cursor.HasField("doc_snapshot"):
        path = parse_path(cursor.doc_snapshot.path)
        doc_ref = DocumentReference(*path, client=client)

        return DocumentSnapshot(
            reference=doc_ref,
            data=json.loads(cursor.doc_snapshot.json_data),
            exists=True,
            read_time=None,
            create_time=None,
            update_time=None,
        )

    values = [json.loads(value) for value in cursor.json_values]
    return convert_data(values)