Beispiel #1
0
def login():
    # get user infos from request
    email = request.json["email"]
    hash = hashlib.sha256()
    hash.update(request.json["password"].encode())
    password = hash.hexdigest()

    session = Session()
    try:
        # get user matching login infos
        user = (session.query(User).filter(User.email == email,
                                           User.password == password).one())
        # create access token from user infos
        user_data = user.get_small_data()
        access_token = create_access_token(identity=user_data)
        user_data["access_token"] = access_token
    except NoResultFound:
        session.rollback()
        return (
            jsonify(
                {"error": "Unknown email / Email and password not matching"}),
            403,
        )
    except Exception as e:
        print(e)
        session.rollback()
        return jsonify({"error": "An unexpected error occured"}), 500
    finally:
        session.close()

    return jsonify(**user_data), 200
Beispiel #2
0
def get_user(user_id: int):
    session = Session()
    user = session.query(User).get(user_id)
    if user is not None:
        user = user.get_small_data()
    session.close()
    return user
Beispiel #3
0
def register():
    # get user infos from request
    user_infos = dict()
    user_infos["email"] = request.json["email"]
    hash = hashlib.sha256()
    hash.update(request.json["password"].encode())
    password = hash.hexdigest()
    user_infos["password"] = password
    user_infos["first_name"] = request.json["first_name"]
    user_infos["last_name"] = request.json["last_name"]
    user_infos["phone"] = request.json.get("phone")

    session = Session()
    try:
        # create user
        user = User(**user_infos)
        session.add(user)
        session.query(User).filter(User.email == user_infos["email"]).one()
        # create access token from user infos
        user_data = user.get_small_data()
        access_token = create_access_token(identity=user_data)
        user_data["access_token"] = access_token
        session.commit()
    except IntegrityError:
        session.rollback()
        return jsonify({"error":
                        f"User {user_infos['email']} already exists"}), 400
    except:
        session.rollback()
        return jsonify({"error": "An unexpected error occured"}), 500
    finally:
        session.close()

    return jsonify(**user_data), 200
Beispiel #4
0
def get_company_byId(company_id: int):
    session = Session()
    company = session.query(Company).get(company_id)
    if company is not None:
        company = company.get_small_data()
    session.close()
    return company
Beispiel #5
0
def reset_db():
    session = Session()
    if config["env"] == "development":
        Base.metadata.drop_all()
    Base.metadata.create_all()
    session.commit()
    session.close()
Beispiel #6
0
def delete_company(company_id: int):
    session = Session()
    try:
        company = session.query(Company).get(company_id)
        session.delete(company)
        session.commit()
        return True
    except:
        session.rollback()
        return False
    finally:
        session.close()
Beispiel #7
0
def update_user_data(user_id: int, phone: str = None):
    session = Session()
    try:
        user = session.query(User).get(user_id)
        if phone is not None:
            user.phone = phone
        session.commit()
        return user.get_small_data()
    except:
        session.rollback()
        return False
    finally:
        session.close()
Beispiel #8
0
def update_company_data(company_id: int, phone: str = None):
    session = Session()
    try:
        company = session.query(Company).get(company_id)
        if phone is not None:
            company.phone = phone
        session.commit()
        return company.get_small_data()
    except:
        session.rollback()
        return False
    finally:
        session.close()
Beispiel #9
0
def create_company(name: str, siren: str, email: str, phone: str, made_by_id: int):
    session = Session()
    try:
        creator = session.query(User).get(made_by_id)
        if creator is None:
            raise Exception("Company creator not found")
        company = Company(name=name, siren=siren, email=email, phone=phone)
        session.add(company)
        session.query(Company).filter(
            CompanyRestaurateur.siren == siren,
        ).one()  # update company instance with id from database
        creator.company_id = company.id
        session.commit()
        return company.get_small_data()
    except:
        session.rollback()
        return False
    finally:
        session.close()
Beispiel #10
0
def get_all_users():
    session = Session()
    users = [user.get_small_data() for user in session.query(User).all()]
    session.close()
    return users