Beispiel #1
0
def get_orders():
    user = get_user()
    filters = get_header_json("orderType")

    if (user and filters):
        restaurant = Restaurant.get_from_id(user.restaurant_id)

        if (restaurant):
            response = {}
            response["orders"] = []
            restaurantOrders = Order.get_from_restaurant(restaurant)

            for restaurantOrder in restaurantOrders:
                if (restaurantOrder.status in filters):
                    response["orders"].append(get_order_data(restaurantOrder))

            response["orders"].reverse()

            response["restaurant"] = get_restaurant_data(restaurant)

            return jsonify(response)
        else:
            abort(404)
    else:
        abort(400)
Beispiel #2
0
def restaurant_add_update_category():
    user = get_user()

    if (user):
        restaurant = Restaurant.get_from_id(user.restaurant_id)

        if (restaurant):
            name = get_header("name")
            id = get_header_int("id")

            if (name and len(name) <= 50):
                if (is_int(id)):
                    category = Category.get_from_id(restaurant, id)

                    if (category):
                        if (get_header("remove")):
                            category.remove()
                        else:
                            category.name = name
                            get_session().commit()

                        return "true"
                    else:
                        abort(404)
                else:
                    Category.add(name, restaurant)

                    return "true"
            else:
                abort(400)

        else:
            abort(404)
    else:
        abort(404)
Beispiel #3
0
def get_data():
    client = get_client()

    if (client):
        response = {}

        if (get_header("orders")):
            clientOrders = Order.get_from_client(client)
            response["orders"] = []

            for clientOrder in clientOrders:
                response["orders"].append(get_order_data(clientOrder))

            response["orders"].reverse()

        if (get_header("restaurants")):
            restaurants = Restaurant.get_all()
            response["restaurants"] = []

            for restaurant in restaurants:
                response["restaurants"].append(get_restaurant_data(restaurant))

        return jsonify(response)
    else:
        abort(401)
Beispiel #4
0
def restaurant_add_update_product():
    user = get_user()

    if (user):
        restaurant = Restaurant.get_from_id(user.restaurant_id)

        if (restaurant):
            name = get_header("name")
            description = get_header("description")
            price = get_header_float("price")
            id = get_header_int("id")

            if (is_int(id)):
                product = Product.get_from_id(id)

                if (product):
                    if (get_header("remove")):
                        product.remove()
                    else:
                        if (name and description and is_float(price)
                                and len(name) <= 50
                                and len(description) <= 100):
                            product.name = name
                            product.description = description
                            product.price = price
                            get_session().commit()
                        else:
                            abort(400)

                    return "true"
                else:
                    abort(404)
            else:
                categoryId = get_header_int("category")

                if (is_int(categoryId) and len(name) <= 50
                        and len(description) <= 100):
                    category = Category.get_from_id(restaurant, categoryId)

                    if (category):
                        Product.add(name, description, price, restaurant,
                                    category)

                        return "true"
                    else:
                        abort(404)
                else:
                    abort(400)

        else:
            abort(404)
    else:
        abort(404)
Beispiel #5
0
    def post(self):
        print('Posting the restaurant')
        args = restaurant_args_parser.parse_args()
        name = args['name']
        description = args['description']
        print(
            'This is the name of the restaurant {} and this is the description {}'
            .format(name, description))

        print('Going further')

        new_restaurant = Restaurant()
        new_restaurant.name = name
        new_restaurant.description = description

        print('The model was created. Name: {}, Description: {}'.format(
            name, description))

        db.session.add(new_restaurant)
        db.session.commit()

        return "Restaurant is successfully added"
Beispiel #6
0
def restaurant_get_stats():
    user = get_user()

    if (user):
        restaurant = Restaurant.get_from_id(user.restaurant_id)

        if (restaurant):
            orders = Order.get_from_restaurant(restaurant)
            date = datetime.date.today()
            resp = {
                "stats": {
                    "paidFees": restaurant.paid_fees,
                    "month": {
                        'orderCount': 0,
                        'grossIncome': 0,
                        'fee': 0,
                        'netIncome': 0,
                    },
                    "all": {
                        'orderCount': 0,
                        'grossIncome': 0,
                        'fee': 0,
                        'netIncome': 0,
                    },
                }
            }

            for order in orders:
                if (order.status == 2):
                    orderDate = datetime.date.fromtimestamp(order.date)

                    resp["stats"]["all"]["orderCount"] += 1
                    resp["stats"]["all"]["grossIncome"] += order.price
                    resp["stats"]["all"]["fee"] += order.fee
                    resp["stats"]["all"]["netIncome"] += (order.price -
                                                          order.fee)

                    if (orderDate.month == date.month
                            and orderDate.year == date.year):
                        resp["stats"]["month"]["orderCount"] += 1
                        resp["stats"]["month"]["grossIncome"] += order.price
                        resp["stats"]["month"]["fee"] += order.fee
                        resp["stats"]["month"]["netIncome"] += (order.price -
                                                                order.fee)

            return jsonify(resp)
        else:
            abort(404)
    else:
        abort(401)
Beispiel #7
0
def get_my_restaurant():
    user = get_user()

    if (user):
        restaurant = Restaurant.get_from_id(user.restaurant_id)

        if (restaurant):
            response = {}
            response["restaurant"] = get_restaurant_data(restaurant)

            return jsonify(response)
        else:
            abort(404)
    else:
        abort(400)
Beispiel #8
0
def get_messages(data):
    client = get_client()
    user = get_user()
    orderId = data["orderId"]
    message = data["message"]

    if ((client or user) and isinstance(orderId, int) and message and len(message) <= 100):
        order = Order.get_from_id(orderId)

        if (order and ((client and order.client_id == client.id) or (user and order.restaurant_id == user.restaurant_id))):
            restaurant = Restaurant.get_from_id(order.restaurant_id)

            if (restaurant):
                if (order.status != 2 and order.status != -1):
                    order.add_chat(message, True if client else False)
                    pushMessages(client, restaurant, order)
Beispiel #9
0
def create_order():
    client = get_client()

    if (client):
        restaurantId = get_header_int("restaurantId")
        address = get_header("address")
        comments = get_header("comments")
        coords = json.loads(get_header("coords"))
        products = json.loads(get_header("products"))

        if (restaurantId and address and products):
            if (len(products) > 0):
                price = 0

                for productData in products:
                    product = Product.get_from_id(productData["id"])

                    if (product):
                        price = price + (product.price * productData["count"])
                    else:
                        abort(402)

                restaurant = Restaurant.get_from_id(restaurantId)

                if (restaurant):
                    order = Order.add(address, price, comments, products,
                                      coords, client, restaurant)

                    if (order):
                        return "true"
                    else:
                        abort(402)
                else:
                    abort(404)
            else:
                abort(401)
        else:
            abort(400)
    else:
        abort(401)
Beispiel #10
0
def restaurant_update_profile():
    user = get_user()

    if (user):
        restaurant = Restaurant.get_from_id(user.restaurant_id)

        if (restaurant):
            name = get_header("name")
            description = get_header("description")

            if (name and len(name) <= 50 and description
                    and len(description) <= 100):
                restaurant.name = name
                restaurant.description = description

                get_session().commit()

                return "true"
            else:
                abort(400)
        else:
            abort(404)
    else:
        abort(404)
Beispiel #11
0
def first_run():
    from models.client import Client
    from models.category import Category
    from models.orders import Order
    from models.product import Product
    from models.restaurants import Restaurant
    from models.user import User
    meta = sqlalchemy.MetaData(engine)
    meta.reflect()
    #meta.drop_all()

    print("First run, please wait while the db is being populated...")

    # Create tables
    Base.metadata.create_all(engine)

    testClient = Client.add("6977988551")
    testClient2 = Client.add("8891155521")

    restaurant = Restaurant.add(
        "Restaurant 1",
        "The best food you'll find in the city\nWe make sandwiches, salads and burgers"
    )

    sandwichesCategory = Category.add("Sandwiches", restaurant)
    Product.add("Pulled Pork", "With tangy barbecue sauce on an onion knot",
                9.50, restaurant, sandwichesCategory)
    Product.add(
        "Turkey Club",
        "Roasted turkey breast, bacon, lettuce, avocado and tomato on baguette",
        8, restaurant, sandwichesCategory)
    Product.add(
        "Reuben",
        "Corned beef, melted swiss, sauerkraut and thousand island on marbled rye",
        8, restaurant, sandwichesCategory)
    Product.add(
        "Shrimp Cilantro Wrap",
        "Shrimp, avocado, mixed greens, salsa, cilantro and may on a tomato tortilla",
        8.5, restaurant, sandwichesCategory)

    burgerCategory = Category.add("Burgers", restaurant)
    Product.add(
        "Grass-fed Beef Burger",
        "With sharp cheddar, heirloom tomatoes and caramelized onions", 9.5,
        restaurant, burgerCategory)
    Product.add(
        "Mushroom Swiss Burger",
        "With sautéed mushrooms and melted swiss on a home-baked roll", 10,
        restaurant, burgerCategory)
    Product.add(
        "Hickory Burger",
        "Topped with cheddar, hickory smoked bacon and smoked barbecue sauce",
        10, restaurant, burgerCategory)
    Product.add(
        "Chicken Burger",
        "Grilled chicken breast with heirloom tomatoes, avocado and sprouts on a home-baked roll",
        9, restaurant, burgerCategory)

    saladCategory = Category.add("Salads", restaurant)
    Product.add(
        "Caesar Salad",
        "Romaine, fresh parmesan, seasoned croutons and black pepper with garlic anchovy dressing",
        6.75, restaurant, saladCategory)
    Product.add("Red Iceberg Salad",
                "With sweet corn, blackberries, goat cheese and fresh basil",
                9.25, restaurant, saladCategory)
    Product.add(
        "House Salad",
        "With green olives, green peppers, onions, cucumbers, and tomato",
        6.75, restaurant, saladCategory)
    Product.add(
        "Blue Chicken Salad",
        "Mesclun greens, apple, grilled chicken, gorgonzola, chesse and balsamic vinagrette",
        9.25, restaurant, saladCategory)

    # Add an user for the restaurant we just created
    User.add("restaurant1", "restaurant1", 0, restaurant)

    streets = [
        "Oak Street", "Madison Avenue", "Bridle Lane", "Jefferson Street",
        "Lafayette Avenue", "Grove Street", "Chestnut Avenue"
    ]

    # Simulate some orders on a different client
    originalDate = time.time() - 57600000
    for i in range(87):
        productCount = randint(1, 2)
        used = {}
        products = []
        price = 0

        originalDate += randint(376000, 576000)

        for y in range(productCount):
            id = randint(0, 11)
            while (id in used):
                id = randint(0, 11)

            used[id] = True
            amount = randint(1, 2)
            products.append({"id": id + 1, "count": amount})
            product = Product.get_from_id(id + 1)
            price += amount * product.price

        order = Order.add(
            random.choice(streets) + " " + str(randint(1, 5000)), price, "",
            products, [-34.601874, -58.432611], testClient2, restaurant)
        order.date = originalDate
        order.status = 2
        get_session().commit()

    Order.add("Bridle Lane 1775", 9.50, "", [{
        'id': 1,
        'count': 1
    }], [-34.601874, -58.432611], testClient, restaurant)