Exemple #1
0
def add_ride_request(transaction, ride_request, location, user_id, event=None):
    """ Description
        This method saves rideRequest and update user's eventSchedule.
        The method corresponds to use case "Create Ride Request".
        Note that transaction.commit() is not required after this method is called if this method is decorated
            by @transactional.

    :param transaction:
    :param ride_request:
    :param location:
    :param user_id:
    :return:
    """

    # Set the firestoreRef of the rideRequest
    RideRequestGenericDao().create(ride_request)
    # Saves RideRequest Object to Firestore
    RideRequestGenericDao().set_with_transaction(
        transaction, ride_request, ride_request.get_firestore_ref())
    # [START] Update the user's eventSchedule
    user_ref = UserDao().get_ref(user_id)
    # Build the eventSchedule for user
    event_schedule = gravitate.domain.event_schedule.actions.create_event_schedule(
        ride_request, location, event)
    UserDao.add_to_event_schedule_with_transaction(
        transaction,
        user_ref=user_ref,
        event_ref=ride_request.event_ref,
        event_schedule=event_schedule)
Exemple #2
0
def update_in_orbit_event_schedule(transaction: Transaction,
                                   ride_request: Type[RideRequest],
                                   orbit: Orbit, event: Event,
                                   location: Location):
    """ Populate eventSchedule (client view model)
    :param transaction:
    :param ride_request:
    :param orbit:
    :param event:
    :param location:
    :return:
    """

    # update eventSchedule
    user_id = ride_request.user_id
    user_ref = UserDao().get_ref(user_id)
    event_ref = event.get_firestore_ref()

    event_schedule = event_schedule_actions.create_event_schedule_orbit(
        ride_request=ride_request, location=location, orbit=orbit)
    UserDao().add_to_event_schedule_with_transaction(
        transaction,
        user_ref=user_ref,
        event_ref=event_ref,
        event_schedule=event_schedule)
Exemple #3
0
    def get(self, uid):
        """
        Returns the json representation of a user based on user id.

        ---
        tags:
          - users
        # operationId: find user by id
        parameters:
          - name: id
            in: path
            description: ID of the user to fetch
            required: true
            schema:
              type: string
        responses:
          '200':
            description: user response
          default:
            description: unexpected error

        :param uid:
        :return:
        """
        # Check Firestore to see if UID Already Exists
        if UserDao().user_id_exists(uid):
            user = UserDao().get_user_by_id(uid)
            userDict = user.to_dict()
            return userDict, 200
        else:
            errorResponseDict = {"error": "User Does not Exist"}
            return errorResponseDict, 400
    def testUserCollectionExists(self):
        """
        TODO: add setUp to create the user specified in uid
        :return:
        """
        uid = "1GFLeGxBaaUvudqh3XYbFv2sRHx2"
        user = UserDao().get_user_by_id(uid)
        self.assertEqual(user.uid, userDict["uid"])
        self.assertEqual(user.membership, userDict["membership"])
        self.assertEqual(user.phone_number, userDict["phone_number"])
        self.assertEqual(user.photo_url, userDict["photo_url"])
        self.assertEqual(user.display_name, userDict["display_name"])
        self.assertEqual(user.pickupAddress, userDict["pickupAddress"])

        print(json.dumps(user.to_dict()))
Exemple #5
0
def update_not_in_orbit_event_schedule(transaction: Transaction,
                                       ride_request: RideRequest, event: Event,
                                       location: Location):
    # update eventSchedule
    user_id = ride_request.user_id
    user_ref = UserDao().get_ref(user_id)
    event_ref = event.get_firestore_ref()

    event_schedule = event_schedule_actions.create_event_schedule(
        ride_request, location)
    UserDao().add_to_event_schedule_with_transaction(
        transaction,
        user_ref=user_ref,
        event_ref=event_ref,
        event_schedule=event_schedule)
 def testGetUserId(self):
     user = UserDao().get_user_by_id(userDict["uid"])
     self.assertEquals(userDict['display_name'], user.display_name)
     self.assertEquals(userDict['phone_number'], user.phone_number)
     self.assertEquals(userDict['uid'], user.uid)
     self.assertEquals(userDict['membership'], user.membership)
     self.assertEquals(userDict['photo_url'], user.photo_url)
     self.assertEquals(userDict['pickupAddress'], user.pickupAddress)
Exemple #7
0
def get_pickup_address(user_id) -> str:
    """
    This method returns the default pickup address of a user.
    :param user_id:
    :return:
    """
    user: User = UserDao().get_user_by_id(user_id)
    pickup_address = user.pickupAddress
    return pickup_address
 def testUpdateUser(self):
     auth.update_user(userDict["uid"],
                      phone_number="+17777777779",
                      display_name="Zixuan Rao",
                      disabled=False,
                      app=context.Context.firebaseApp)
     user = UserDao().get_user_by_id(userDict["uid"])
     self.assertEquals("Zixuan Rao", user.display_name)
     self.assertEquals("+17777777779", user.phone_number)
     self.assertEquals(userDict['uid'], user.uid)
     self.assertEquals(userDict['membership'], user.membership)
     self.assertEquals(userDict['photo_url'], user.photo_url)
     self.assertEquals(userDict['pickupAddress'], user.pickupAddress)
def create_fb_event(event_dict, uid) -> str:
    """ Creates event from a facebook event.

    :param event_dict: event json returned by Facebook graph api
    :param uid: user id
    :return: id of the event just created
    """
    b = event_builders.FbEventBuilder()
    # print(event_dict)
    b.build_with_fb_dict(event_dict)
    e: SocialEvent = b.export_as_class(SocialEvent)
    # Note that e.firestore_ref will not be set by create()
    ref = EventDao().create_fb_event(e)
    e.set_firestore_ref(ref)
    dict_view = e.to_dict_view()
    dict_view["eventId"] = ref.id
    # TODO: add error handling
    UserDao().add_user_event_dict(uid, dict_view["fbEventId"], dict_view)
    event_id = ref.id
    return event_id
Exemple #10
0
def getMemberProfilePhotoUrls(orbit: Orbit) -> [str]:
    """ Description
        [Assigned to Leon]
        Don't have to follow the method signature, but the signature is required to get other code working.
        Orbits can be obtained through any other ways, and buildEventSchedule can be called from elsewhere.

    :raises:

    :rtype:
    """
    # Must go through each userTicketPair (key = userIDs)
    photo_urls = []
    if CTX.testing:
        warnings.warn(
            "Using testing mode, skipping member profile photo urls evaluation. "
        )
        return photo_urls

    for uid in orbit.user_ticket_pairs:
        user = UserDao().get_user_by_id(uid)
        photo_url = user.photo_url
        photo_urls.append(photo_url)

    return photo_urls
 def testCreateTempTesting(self):
     userRef: firestore.DocumentReference = UserDao().create_user(self.user)
     self.user.set_firestore_ref(userRef)
     print("userRef = {}".format(userRef))
 def setUp(self):
     self.user = UserDao().get_user_by_id('SQytDq13q00e0N3H4agR')
 def testGetUser(self):
     uid = userDict["uid"]
     user = UserDao().get_user_by_id(uid)
     print(user.to_dict())
Exemple #14
0
    def post(self, uid):
        """
        Creates a new user. (user must be registered in firebaseApp first)

        ---
        tags:
          - users
        parameters:
          - in: body
            name: body
            schema:
              id: UserCreationForm
              required:
                - uid
                - phone_number
                - membership
                - display_name
                - photo_url
                - pickupAddress
              properties:
                uid:
                  description: UID
                  type: string
                phone_number:
                  description: Phone Number
                  type: string
                membership:
                  description: Membership
                display_name:
                  description: Name
                photo_url:
                  description: (Profile) Photo URL
                pickupAddress:
                  description: (Default) Pickup Address

        responses:
          200:
            description: user created
          400:
            description: form fields error

        :param uid:
        :return:
        """
        # TODO: change to put/update
        requestJson = request.get_json()
        requestForm = json.loads(requestJson) if (
            type(requestJson) != dict) else requestJson

        validateForm = UserCreationValidateForm(data=requestForm)

        # POST REQUEST
        if validateForm.validate():

            # Transfer data from validateForm to an internal representation of the form
            form = UserCreationForm()
            validateForm.populate_obj(form)
            userDict = fillUserDictWithForm(form)

            # Create User Object
            newUser: User = User.from_dict(userDict)

            userId = newUser.uid
            userRef = UserDao().userCollectionRef.document(document_id=userId)
            newUser.set_firestore_ref(userRef)
            transaction = db.transaction()

            # Saves User Object to Firestore
            saveUser(newUser, transaction=transaction)
            userRef = UserDao().userCollectionRef.document(userId)
            transaction.commit()

            responseDict = {"userId": newUser.get_firestore_ref().id}

            return responseDict, 200
        else:
            print(validateForm.errors)
            return validateForm.errors, 400
    def delete(self, rideRequestId, uid):
        """
        Deletes a ride request.

        ---
        tags:
          - rideRequests

        parameters:
          - name: id
            in: path
            description: ID of the ride request to delete
            required: true
            schema:
              type: string

        responses:
          '200':
            description: ride request deleted
          default:
            description: unexpected error
            # content:
            #   application/json:
            #     schema:
            #       $ref: '#/components/schemas/Error'

        """

        user_id = uid
        user_ref = UserDao().get_ref(user_id)
        ride_request_ref = RideRequestGenericDao(
        ).rideRequestCollectionRef.document(rideRequestId)

        response_dict = {}
        ride_request = RideRequestGenericDao().get(ride_request_ref)
        event_id = ride_request.event_ref.id

        print("userId: {}, rideRequestId: {}, eventId: {}".format(
            user_id, rideRequestId, event_id))

        # Validate that the ride request is not matched to an orbit
        request_completion = ride_request.request_completion
        if request_completion:
            raise service_errors.RequestAlreadyMatchedError

        try:
            # Delete in User's Event Schedule
            EventScheduleGenericDao(
                userRef=user_ref).delete_event_by_id(event_id)
            # Delete in RideRequest Collection
            RideRequestGenericDao().delete(ride_request_ref)
            response_dict = {"success": True}

        except Exception as e:
            err_str = str(e)
            response_dict = {
                "error":
                "Error occurred deleting rideRequest and eventSchedule: " +
                err_str
            }
            print(response_dict)
            return response_dict, 500

        return response_dict, 200  # TODO: change to 204
    def post(self, uid):
        """
        Creates a new event with Facebook event JSON (that is obtained from Facebook Graph API).

        ---
        tags:
          - events
        parameters:
          - in: body
            name: body
            schema:
              id: UserEventJSON
              type: object
              required:
                - description
                - end_time
                - start_time
                - place
                - id
              properties:
                description:
                  type: string
                  example: "Advance Sale begins Friday, 6/1 at 11AM PDT\nwww.coachella.com"
                end_time:
                  type: string
                  example: "2019-04-14T23:59:00-0700"
                start_time:
                  type: string
                  example: "2019-04-12T12:00:00-0700"
                place:
                    type: object
                    properties:
                      name:
                        type: string
                        example: "Coachella"
                      location:
                        type: object
                        properties:
                          latitude:
                            type: number
                            example: 33.679974
                          longitude:
                            type: number
                            example: -116.237221
                      id:
                        example: "20281766647"
                id:
                  type: string
                  example: "137943263736990"
        responses:
          200:
            description: user created
          400:
            description: form fields error

        :param uid:
        :return:
        """
        json_data = request.get_json()
        b = event_builders.FbEventBuilder()
        b.build_with_fb_dict(json_data)
        e: event_models.SocialEvent = b.export_as_class(event_models.SocialEvent)

        # Note that e.firestore_ref will not be set by create()
        ref: DocumentReference = EventDao().create_fb_event(e)
        e.set_firestore_ref(ref)
        dict_view = e.to_dict_view()
        dict_view["eventId"] = ref.id

        # TODO: add error handling
        UserDao().add_user_event_dict(uid, dict_view["fbEventId"], dict_view)

        return {
            "id": e.get_firestore_ref().id
        }