Esempio n. 1
0
    def post(self, user, fields, **kwargs):
        """
        Create a new Site with a handle field
        """
        h = fields["handle"]

        if not isinstance(h, str) or len(h) == 0:
            return {
                "message": "handle must be a non-empty string"
            }, 400

        new_site = Site(**fields)
        new_site.user_id = user.id
        new_site.set_first_handle(h)

        return new_site.to_dict(), 201
Esempio n. 2
0
    def post(self, fields, **kwargs):
        """
        This endpoint signs in users with an apple_token field.

        Apple tokens sometimes contain emails (when it's a new user) and
        otherwise don't

        "sub" is Apple's user PK
        Name is passed to if it's available and should be treated as optional

        If the user's email exists, the existing user object will be returned
        If the user's email does not exist, a new User will be saved

        Either way, a new token will be issued
        """

        try:
            # Validate the apple_token passed in (retrieves apple user)
            apple_user = retrieve_user(fields["apple_token"])
        except Exception as e:
            # Handle exceptions
            return {"message": str(e)}, 400

        is_new_user = True

        if apple_user.full_user:
            # Make a new User
            user = User(email=apple_user.email,
                        name=fields["name"],
                        apple_id=apple_user.id)

            try:
                # Save the new user to the DB
                user.save()

            except IntegrityError:
                # That user already exists, rollback
                is_new_user = False
                db.session().rollback()

                # Find existing user
                user = User.query.filter_by(apple_id=apple_user.id).first()
                pass
        else:
            is_new_user = False
            user = User.query.filter_by(apple_id=apple_user.id).first()

        # # Mark previous tokens expired
        # TODO: This will expire shortcuts tokens which is bad
        # db.session.query(AuthToken).filter_by(
        #     user_id=user.id).update({AuthToken.expired: True})
        # db.session.commit()

        # Send back a new auth token
        new_token = AuthToken(user_id=user.id)
        new_token.save()

        return_payload = {"user": user.to_dict(), "token": new_token.to_dict()}
        status_code = 200

        # Create a user's first site
        if is_new_user:
            status_code = 201
            new_site = Site(user_id=user.id)
            new_site.set_first_handle(user.name)
            return_payload["sites"] = list(new_site.to_dict())
        else:
            # Look up existing sites
            sites = Site.query.filter_by(user_id=user.id)
            return_payload["sites"] = list(map(lambda s: s.to_dict(), sites))

        return return_payload, status_code