Exemplo n.º 1
0
 def remove_event_schedule_with_transaction(
         transaction: Transaction,
         userRef: DocumentReference = None,
         orbitId: str = None):
     eventScheduleRef: DocumentReference = userRef.collection(
         "eventSchedules").document(orbitId)
     transaction.delete(eventScheduleRef)
Exemplo n.º 2
0
        def update_in_transaction(transaction: firestore.Transaction,
                                  reference: firestore.DocumentReference,
                                  document_data: Dict, node_id: str):

            snapshot = reference.get(transaction=transaction)

            if not snapshot.exists:
                transaction.create(reference=reference,
                                   document_data=document_data)
                self.log.info('creating ' + reference.id)
                return True
            else:
                update_time = snapshot.update_time
                snapshot_data = snapshot.to_dict()
                snapshot_node_id = snapshot_data[NODE_ID_KEY]

                time_elapsed = int(time.time()) - update_time.seconds
                self.log.debug('lease created ' + str(time_elapsed) +
                               ' seconds ago')

                lease_expired = time_elapsed > LEASE_DURATION_SECONDS
                lease_owned = snapshot_node_id == node_id
                if lease_owned or lease_expired:
                    if lease_expired:
                        self.log.info('lease expired for ' + snapshot_node_id)
                    if lease_owned:
                        self.log.info('renewing lease')
                    transaction.update(reference=reference,
                                       field_updates=document_data,
                                       option=firestore.Client.write_option(
                                           last_update_time=update_time))
                    return True
                else:
                    self.log.info('lease held by ' + snapshot_node_id)
                    return False
Exemplo n.º 3
0
 def mark_as_complete(transaction: Transaction, task_id):
     ref: DocumentReference = db.collection('groupTasks').document(task_id)
     d = ref.get(ref, transaction)
     m = GroupTaskModel.from_dict(d)
     if not m.in_process:
         raise ValueError("task_id: {} is not in process. ".format(task_id))
     if m.completed:
         raise ValueError("task_id: {} is already completed. ".format(task_id))
     m.in_process = False
     m.completed = True
     transaction.set(m.to_dict())
Exemplo n.º 4
0
 def start_by_id(transaction: Transaction, task_id):
     ref: DocumentReference = db.collection('groupTasks').document(task_id)
     d = ref.get(ref, transaction)
     m = GroupTaskModel.from_dict(d)
     if m.in_process:
         raise ValueError("task_id: {} is already being processed. ".format(task_id))
     if m.completed:
         raise ValueError("task_id: {} is already completed. ".format(task_id))
     m.in_process = True
     transaction.set(m.to_dict())
     return m
Exemplo n.º 5
0
    def add_to_event_schedule_with_transaction(
            transaction: Transaction,
            user_ref: str = None,
            event_ref: DocumentReference = None,
            event_schedule: AirportEventSchedule = None):
        """ Description
            Add a event schedule to users/<userId>/eventSchedule
                Note that the toEventRideRequestRef will be overwritten without warning if already set.
                (Same for fromEventRideRequestRef.)
        :type transaction:Transaction:
        :param transaction:Transaction:
        :type user_ref:str:
        :param user_ref:str:
        :type event_ref:str:
        :param event_ref:str:
        :type event_schedule:dict:
        :param event_schedule:dict:
        :raises:
        :rtype:
        """

        # userRef: DocumentReference = db.collection(u'users').document(userRef)

        # Get the CollectionReference of the collection that contains AirportEventSchedule's
        event_schedules_ref: CollectionReference = user_ref.collection(
            u'eventSchedules')

        # Retrieve document id to be used as the key
        event_id = event_ref.id
        # eventId = 'testeventid1'
        # warnings.warn("Using mock/test event id. Must replace before release. ")

        # Get the DocumentReference for the AirportEventSchedule
        event_schedule_ref: DocumentReference = event_schedules_ref.document(
            event_id)
        event_schedule_dict = event_schedule.to_dict()
        transaction.set(
            event_schedule_ref, event_schedule_dict,
            merge=True)  # So that 'fromEventRideRequestRef' is not overwritten
Exemplo n.º 6
0
    def set_with_transaction(transaction: Transaction, newRideRequest: Type[RideRequest],
                             rideRequestRef: DocumentReference):
        """ Description
            Note that a read action must have taken place before anything is set with that transaction. 

        :type transaction:Transaction:
        :param transaction:Transaction:

        :type newRideRequest:Type[RideRequest]:
        :param newRideRequest:Type[RideRequest]:

        :type rideRequestRef:DocumentReference:
        :param rideRequestRef:DocumentReference:

        :raises:

        :rtype:
        """
        return transaction.set(rideRequestRef, newRideRequest.to_dict())
    def set_with_transaction(transaction: Transaction,
                             newEventSchedule: Type[AirportEventSchedule],
                             eventScheduleRef: DocumentReference):
        """ Description
            Note that a read action must have taken place before anything is set with that transaction.

        :type self:
        :param self:

        :type transaction:Transaction:
        :param transaction:Transaction:

        :type newEventSchedule:Type[AirportEventSchedule]:
        :param newEventSchedule:Type[AirportEventSchedule]:

        :type eventScheduleRef:DocumentReference:
        :param eventScheduleRef:DocumentReference:

        :raises:

        :rtype:
        """
        raise NotImplementedError  # TODO: fix
        return transaction.set(eventScheduleRef, newEventSchedule)
Exemplo n.º 8
0
    def set_with_transaction(transaction: Transaction,
                             newLocation: Type[Location],
                             locationRef: DocumentReference):
        """ Description
            Note that a read action must have taken place before anything is set with that transaction.

        :type self:
        :param self:

        :type transaction:Transaction:
        :param transaction:Transaction:

        :type newLocation:Type[Location]:
        :param newLocation:Type[Location]:

        :type locationRef:DocumentReference:
        :param locationRef:DocumentReference:

        :raises:

        :rtype:
        """
        locationDict = newLocation.to_dict()
        return transaction.set(locationRef, locationDict)
Exemplo n.º 9
0
 def _set_with_transaction(transaction: Transaction,
                           newLocation: Type[Location],
                           locationRef: DocumentReference):
     return transaction.set(locationRef, newLocation)
Exemplo n.º 10
0
 def set_with_transaction(self, transaction: Transaction, new_user: User,
                          user_ref: DocumentReference):
     transaction.set(user_ref, new_user)
Exemplo n.º 11
0
 def set_user_with_transaction(transaction: Transaction,
                               newUser: Type[User],
                               userRef: DocumentReference):
     transaction.set(userRef, newUser.to_firestore_dict())
Exemplo n.º 12
0
 def set_with_transaction(transaction: Transaction, newOrbit: Orbit,
                          orbitRef: DocumentReference):
     """ Description
         Note that a read action must have taken place before anything is set with that transaction. 
     """
     return transaction.set(orbitRef, newOrbit.to_dict())