Example #1
0
def populate_user():
    new_user = User()
    new_user.email = "*****@*****.**"
    new_user.phone = '3333333333'
    new_user.firstname = "firstname_test"
    new_user.lastname = "lastname_test"
    new_user.password = "******"
    new_user.dateofbirth = datetime.date(2020, 10, 5)
    new_user.role = "customer"

    return new_user
Example #2
0
def add_user(email, phone, firstname, lastname, password, date, role):
    new_user = User()
    new_user.email = email
    new_user.phone = phone
    new_user.firstname = firstname
    new_user.lastname = lastname
    new_user.password = password
    new_user.role = role
    new_user.dateofbirth = date

    db.session.add(new_user)
    db.session.commit()
    return db.session.query(User).filter(User.email == email).first()
Example #3
0
def insert_admin(db, app):
    with app.app_context():
        admin = db.session.query(User).filter_by(
            email='*****@*****.**').first()
        if admin is None:
            example = User()
            example.email = '*****@*****.**'
            example.phone = '3333333333'
            example.firstname = 'Admin'
            example.lastname = 'Admin'
            example.set_password('admin')
            example.dateofbirth = datetime.date(2020, 10, 5)
            example.is_admin = True
            example.role = 'admin'
            db.session.add(example)
            db.session.commit()
Example #4
0
def insert_ha(db, app):
    with app.app_context():
        ha = db.session.query(User).filter_by(
            email='*****@*****.**').first()
        if ha is None:
            example = User()
            example.email = '*****@*****.**'
            example.phone = '3333333333'
            example.firstname = 'ha'
            example.lastname = 'ha'
            example.set_password('ha')
            example.dateofbirth = datetime.date(2020, 10, 5)
            example.is_admin = True
            example.role = 'ha'
            db.session.add(example)
            db.session.commit()
Example #5
0
def create_app():
    app = Flask(__name__)
    app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY'
    app.config['SECRET_KEY'] = 'ANOTHER ONE'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///gooutsafe.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    for bp in blueprints:
        app.register_blueprint(bp)
        bp.app = app

    db.init_app(app)
    login_manager.init_app(app)
    db.create_all(app=app)

    # create a first admin user
    with app.app_context():
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        if user is None:
            example = User()
            example.firstname = 'Admin'
            example.lastname = 'Admin'
            example.email = '*****@*****.**'
            example.dateofbirth = datetime.datetime(2020, 10, 5)
            example.is_admin = True
            example.set_password('admin')
            db.session.add(example)
            db.session.commit()

        q = db.session.query(Restaurant).filter(Restaurant.id == 1)
        restaurant = q.first()
        if restaurant is None:
            example = Restaurant()
            example.name = 'Trial Restaurant'
            example.likes = 42
            example.phone = 555123456
            example.lat = 43.720586
            example.lon = 10.408347
            db.session.add(example)
            db.session.commit()

    return app
Example #6
0
def create_app(tests=False):
    app = Flask(__name__)
    app.config["WTF_CSRF_SECRET_KEY"] = "A SECRET KEY"
    app.config["SECRET_KEY"] = "ANOTHER ONE"
    if tests is False:
        app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///gooutsafe.db"
    else:
        app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///tests/gooutsafe.db"
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

    for bp in blueprints:
        app.register_blueprint(bp)
        bp.app = app

    db.init_app(app)
    login_manager.init_app(app)
    db.create_all(app=app)

    # create a first admin user
    with app.app_context():

        # create the user roles
        q = db.session.query(Role).filter(Role.id == 1)
        role = q.first()
        if role is None:
            role = Role()
            role.value = "ADMIN"
            role.label = "Admin role"
            db.session.add(role)
            role = Role()
            role.value = "OPERATOR"
            role.label = "Operator role"
            db.session.add(role)
            role = Role()
            role.value = "CUSTOMER"
            role.label = "Customer role"
            db.session.add(role)
            role = Role()
            role.value = "HEALTH"
            role.label = "Health role"
            db.session.add(role)
            db.session.commit()

        # an Admin user
        q = db.session.query(User).filter(User.email == "*****@*****.**")
        user = q.first()
        if user is None:
            admin_user = User()
            admin_user.firstname = "Admin"
            admin_user.lastname = "Admin"
            admin_user.email = "*****@*****.**"
            admin_user.phone = "3334455678"
            admin_user.dateofbirth = datetime.datetime(2020, 10, 5)
            admin_user.is_admin = True
            admin_user.set_password("admin")
            admin_user.role_id = 1
            db.session.add(admin_user)
            db.session.commit()

        # an operator
        q = db.session.query(User).filter(User.email == "*****@*****.**")
        user = q.first()
        if user is None:
            first_operator = User()
            first_operator.firstname = "Ham"
            first_operator.lastname = "Burger"
            first_operator.email = "*****@*****.**"
            first_operator.phone = "222333567"
            first_operator.is_admin = False
            first_operator.set_password("operator")
            first_operator.role_id = 2
            db.session.add(first_operator)
            db.session.commit()

        # a customer
        q = db.session.query(User).filter(User.email == "*****@*****.**")
        user = q.first()
        if user is None:
            first_customer = User()
            first_customer.firstname = "John"
            first_customer.lastname = "Doe"
            first_customer.email = "*****@*****.**"
            first_customer.phone = "111234765"
            first_customer.is_admin = False
            first_customer.set_password("customer")
            first_customer.role_id = 3
            db.session.add(first_customer)
            db.session.commit()

        # health autority
        q = db.session.query(User).filter(
            User.email == "*****@*****.**")
        user = q.first()
        if user is None:
            health_authority = User()
            health_authority.firstname = "Health"
            health_authority.lastname = "Authority"
            health_authority.email = "*****@*****.**"
            health_authority.phone = "321456783"
            health_authority.is_admin = False
            health_authority.set_password("nocovid")
            health_authority.role_id = 4
            db.session.add(health_authority)
            db.session.commit()

        # a restaurant
        q = db.session.query(Restaurant).filter(Restaurant.id == 1)
        restaurant = q.first()
        if restaurant is None:
            # load the first operator
            q = db.session.query(User).filter(
                User.email == "*****@*****.**")
            user = q.first()
            first_restaurant = Restaurant()
            first_restaurant.name = "Trial Restaurant"
            first_restaurant.likes = 42
            first_restaurant.phone = 555123456
            first_restaurant.covid_measures = "Distance between tables 2mt; MenĂ¹ touch; Alcohol Gel; Only Electronic Payment"
            first_restaurant.lat = 43.720586
            first_restaurant.lon = 10.408347
            first_restaurant.owner_id = user.id
            db.session.add(first_restaurant)
            db.session.commit()

        # a table
        q = db.session.query(RestaurantTable).filter(RestaurantTable.id == 1)
        table = q.first()
        if table is None:
            # insert the first table
            q = db.session.query(Restaurant).filter(
                Restaurant.name == "Trial Restaurant")
            restaurant = q.first()
            first_table = RestaurantTable()
            first_table.restaurant_id = restaurant.id
            first_table.name = "Table 1"
            first_table.max_seats = 6
            first_table.available = True
            db.session.add(first_table)
            db.session.commit()

        # another table
        q = db.session.query(RestaurantTable).filter(RestaurantTable.id == 2)
        table = q.first()
        if table is None:
            # insert the first table
            q = db.session.query(Restaurant).filter(
                Restaurant.name == "Trial Restaurant")
            restaurant = q.first()
            second_table = RestaurantTable()
            second_table.restaurant_id = restaurant.id
            second_table.name = "Table 2"
            second_table.max_seats = 4
            second_table.available = True
            db.session.add(second_table)
            db.session.commit()

        # insert some opening hours
        q = (db.session.query(OpeningHours).filter(
            OpeningHours.restaurant_id == 1).filter(
                OpeningHours.week_day == 0))
        openinghour = q.first()
        if openinghour is None:
            q = db.session.query(Restaurant).filter(
                Restaurant.name == "Trial Restaurant")
            restaurant = q.first()
            first_opening_hours = OpeningHours()
            first_opening_hours.restaurant_id = restaurant.id
            first_opening_hours.week_day = 0
            first_opening_hours.open_lunch = datetime.time(hour=12)
            first_opening_hours.close_lunch = datetime.time(hour=15)
            first_opening_hours.open_dinner = datetime.time(hour=20)
            first_opening_hours.close_dinner = datetime.time(hour=22)
            db.session.add(first_opening_hours)
            db.session.commit()

        # insert some opening hours
        q = (db.session.query(OpeningHours).filter(
            OpeningHours.restaurant_id == 1).filter(
                OpeningHours.week_day == 2))
        openinghour = q.first()
        if openinghour is None:
            q = db.session.query(Restaurant).filter(
                Restaurant.name == "Trial Restaurant")
            restaurant = q.first()
            second_opening_hours = OpeningHours()
            second_opening_hours.restaurant_id = restaurant.id
            second_opening_hours.week_day = 2
            second_opening_hours.open_lunch = datetime.time(hour=12)
            second_opening_hours.close_lunch = datetime.time(hour=15)
            second_opening_hours.open_dinner = datetime.time(hour=20)
            second_opening_hours.close_dinner = datetime.time(hour=22)
            db.session.add(second_opening_hours)
            db.session.commit()

        # insert some opening hours
        q = (db.session.query(OpeningHours).filter(
            OpeningHours.restaurant_id == 1).filter(
                OpeningHours.week_day == 4))
        openinghour = q.first()
        if openinghour is None:
            q = db.session.query(Restaurant).filter(
                Restaurant.name == "Trial Restaurant")
            restaurant = q.first()
            third_opening_hours = OpeningHours()
            third_opening_hours.restaurant_id = restaurant.id
            third_opening_hours.week_day = 4
            third_opening_hours.open_lunch = datetime.time(hour=12)
            third_opening_hours.close_lunch = datetime.time(hour=15)
            third_opening_hours.open_dinner = datetime.time(hour=20)
            third_opening_hours.close_dinner = datetime.time(hour=22)
            db.session.add(third_opening_hours)
            db.session.commit()

        # a reservation
        q = db.session.query(Reservation).filter(Reservation.id == 1)
        reservation = q.first()
        if reservation is None:
            # insert the first table
            q = db.session.query(User).filter(
                User.email == "*****@*****.**")
            customer = q.first()
            q = db.session.query(RestaurantTable).filter(
                RestaurantTable.id == 1)
            table = q.first()
            q = db.session.query(Restaurant).filter(Restaurant.id == 1)
            restaurant = q.first()
            first_reservation = Reservation()
            first_reservation.reservation_date = datetime.datetime(2020,
                                                                   10,
                                                                   28,
                                                                   hour=12)
            first_reservation.reservation_end = (
                first_reservation.reservation_date +
                datetime.timedelta(minutes=restaurant.avg_time))
            first_reservation.customer_id = customer.id
            first_reservation.table_id = table.id
            first_reservation.people_number = 2
            db.session.add(first_reservation)
            db.session.commit()

        # insert a review
        q = db.session.query(Review).filter_by(id=1).first()
        if q is None:
            review = Review()
            q = db.session.query(Restaurant).filter(
                Restaurant.name == "Trial Restaurant")
            restaurant = q.first()

            q = db.session.query(User).filter(
                User.email == "*****@*****.**")
            user = q.first()
            review.restaurant_id = restaurant.id
            review.reviewer_id = user.id
            review.review = "ciao"
            review.stars = decimal.Decimal(4.5)

            db.session.add(review)
            db.session.commit()
    # CALCULATE_RATING_RESTAURANTS
    return app
Example #7
0
def create_app():
    app = Flask(__name__)
    app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY'
    app.config['SECRET_KEY'] = 'ANOTHER ONE'
    #app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@postgres:5432/postgres'
    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URI']
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    
    
    # Flask-Mail configuration
    app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
    app.config['MAIL_PORT'] = 587
    app.config['MAIL_USE_TLS'] = True
    app.config['MAIL_USERNAME'] = '******'
    app.config['MAIL_PASSWORD'] = '******'
    app.config['MAIL_DEFAULT_SENDER'] = '*****@*****.**'
    for bp in blueprints:
        app.register_blueprint(bp)
        bp.app = app

    db.init_app(app)
    login_manager.init_app(app)
    try:
        db.create_all(app=app)
    except Exception as e:
        print(e)


    # TODO THIS SECTION MUST BE REMOVED, ONLY FOR DEMO
    # already tested EndPoints are used to create examples
    app.config['WTF_CSRF_ENABLED'] = False

    with app.app_context():
        
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        adm = q.first()
        if adm is None:
            try: 
                # create a first admin user 
                # test for a user defined in database.db
                example = User()
                example.email = '*****@*****.**'
                example.phone = '3333333333'
                example.firstname = 'Admin'
                example.lastname = 'Admin'
                example.set_password('admin')
                example.dateofbirth = datetime.date(2020, 10, 5)
                example.role = 'admin'           
                example.is_admin = True
                db.session.add(example)
                db.session.commit()

        

                test_client = app.test_client()

                insert_ha(db, app)
                
                for user in customers_example:
                    create_user_EP(test_client,**user)

                for user in restaurant_owner_example:
                    create_user_EP(test_client,**user)

                for usr_idx,restaurant in enumerate(restaurant_example):
                    user_login_EP(test_client, restaurant_owner_example[usr_idx]['email'], 
                                                restaurant_owner_example[usr_idx]['password'])

                    create_restaurant_EP(test_client,restaurant)

                    user_logout_EP(test_client)

            except Exception as e:
                print(e)

        

    app.config['WTF_CSRF_ENABLED'] = True

    

    return app