Esempio n. 1
0
def add_user(new_user):
    simple_user = SimpleUser.query.filter_by(email=new_user["email"]).first()
    if not simple_user:
        simple_user = add_simple_user({"email": new_user["email"]})

    address = Address(**new_user["address"])
    db.session.add(address)
    db.session.commit()
    del new_user["address"]

    new_user["simple_user_id"] = simple_user.id
    new_user["main_address_id"] = address.id

    new_user["password_hash"] = new_user["password"]
    del new_user["password"]

    user = User(**new_user)
    user.set_password_hash(new_user["password_hash"])
    user.addresses = [address]
    user.address_history = [address.id]
    db.session.add(user)

    simple_user.user = user

    db.session.commit()
    return user
Esempio n. 2
0
    def post(self):
        post_data = request.get_json()
        if not post_data:
            return {"message": "Invalid payload."}, 400
        try:
            print('enter getJSONResult', flush=True)

            username = post_data["username"]
            email = post_data["email"]
            if not validate_email(email):
                return {"message": f"{email} is not a valid email."}, 400
            if User.query.filter_by(email=email).first():
                return {"message": f"User {email} already exists."}, 400
            elif User.query.filter_by(username=username).first():
                return {"message": f"User {username} already exists."}, 400
            simple_user = SimpleUser.query.filter_by(email=email).first()
            if simple_user:
                simple_user.signed_up = True
            else:
                simple_user = SimpleUser(email=email, signed_up=True)
                db.session.add(simple_user)
                db.session.commit()
            post_data["simple_user_id"] = simple_user.id

            print('enter getJSONResult', flush=True)

            new_address = Address(**post_data["address"])

            print('enter getJSONResult', flush=True)
            db.session.add(new_address)
            db.session.commit()

            post_data["main_address_id"] = new_address.id
            del post_data["address"]

            post_data["password_hash"] = post_data["password"]
            del post_data["password"]
            user = User(**post_data)
            user.set_password_hash(post_data["password_hash"])
            db.session.add(user)

            simple_user.user = user
            user.addresses = [new_address]
            user.address_history = [new_address.id]

            db.session.commit()

            auth_token = user.encode_auth_token(user.id)
            return {
                "message": f"{username} has signed up!",
                "auth_token": auth_token.decode()
            }, 201
        except TypeError as te:
            return {"message": f"Type Error: {te}"}, 400
        except KeyError as ke:
            return {"message": f"Key Error: {ke}"}, 400
        except exc.IntegrityError as ie:
            db.session.rollback()
            return {"message": f"Integrity Error: {ie}"}, 400