Пример #1
0
def get_bid(id):
    # TODO doc

    bid = Products.query.get(id)

    if bid is None:
        raise ProductException(str(id), "Product not found")

    if bid.bid_date is None:
        raise ProductException(str(id), "Product isnt a bid")

    max_bid = Bids.get_max(bid.id)

    if max_bid is None:
        max_bid_bid = 0
        max_bid_user_id = None
    else:
        max_bid_bid = max_bid.bid
        max_bid_user_id = max_bid.user_id

    item = {
        "id": int(bid.id),
        "title": str(bid.title),
        "bid_date": str(bid.bid_date),
        "main_img": str(bid.main_img),
        "max_bid": float(max_bid_bid),
        "max_bid_user": max_bid_user_id
    }

    return Response(json.dumps(item),
                    status=200,
                    content_type='application/json')
Пример #2
0
def post_bid(id):
    # TODO doc

    if not request.is_json:
        raise JSONExceptionHandler()

    content = request.get_json()

    bid = Products.query.get(id)

    if bid is None:
        raise ProductException(str(id), "Product not found")

    if bid.bid_date is None:
        raise ProductException(str(id), "Product isnt a bid")
    else:
        if bid.bid_date < datetime.datetime.utcnow():
            raise ProductException(str(id), "Bid out of time")

    money = float(content["bid"])
    Bids.add_bid(id, current_user.id, money)

    resp = api_resp(
        0, "info",
        "Successful bid with " + str(money) + " to " + str(id) + " bid")

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')
def update_prod_info(id):

    if not request.is_json:
        raise JSONExceptionHandler()

    product = Products.query.get(int(id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    if product.user_id != current_user.id:
        raise UserNotPermission(str(current_user.id), "This user doesnt own this product" + str(id))

    content = request.get_json()

    title = content["title"]
    price = float(content["price"])
    descript = content["descript"]
    bid = datetime.datetime.strptime(content["bid_date"], "%Y-%m-%d %H:%M:%S") if 'bid_date' in content else None
    categories = content["categories"]
    photo_urls = content["photo_urls"]
    place = content["place"]
    main_img = content["main_img"]

    if not isinstance(categories, list):
        raise JSONExceptionHandler("Bad format for categories, need an array")

    if not isinstance(photo_urls, list):
        raise JSONExceptionHandler("Bad format for photo_urls, need an array")

    CatProducts.delete_cats_by_prod(id)
    Images.delete_images_by_prod(id)

    for cat in categories:
        if len(cat) <= 1:
            raise ProductException(title, "Invalid categorie: " + cat)
        Categories.add_cat(cat)
        CatProducts.add_prod(cat, id)

    for photo in photo_urls:
        Images.add_photo(photo, id)

    # Notificaiones
    if product.price > price:
        users_ids = Follows.get_users_follow_prod(product.id)
        for user_id in users_ids:
            push_notify(user_id, "El precio del producto ha bajado! :D", int(product.id))
    elif product.price < price:
        users_ids = Follows.get_users_follow_prod(product.id)
        for user_id in users_ids:
            push_notify(user_id, "El precio del producto ha subido :(", int(product.id))

    product.update_me(title, price, descript, bid, place, main_img)

    resp = api_resp(0, "info", "Product: " + str(id) + ' (' + title + ') ' + "updated")

    return Response(json.dumps(resp), status=200, content_type='application/json')
def create_payment():
    if not request.is_json:
        raise JSONExceptionHandler()

    content = request.get_json()

    amount = content["amount"]
    iban = content["iban"]
    boost_date = datetime.datetime.strptime(content["boost_date"], "%Y-%m-%d")
    product_id = int(content["product_id"])

    product = Products.query.get(int(product_id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    payment_id = Payments.add(amount, iban, product_id, boost_date)

    # Notificaciones
    for cat in CatProducts.get_cat_names_by_prod(product_id):
        users_ids = Interests.get_users_interest_cat(cat)
        for user_id in users_ids:
            push_notify(user_id,
                        "Nuevo producto en una categoria que te interesa",
                        int(product_id), cat)

    resp = api_resp(0, "info", str(payment_id))

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')
Пример #5
0
def bid_up_prod(id):

    if not request.is_json:
        raise JSONExceptionHandler()

    product = Products.query.get(int(id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    if product.user_id != current_user.id:
        raise UserNotPermission(str(current_user.id),
                                "This user doesnt own this product" + str(id))

    content = request.get_json()

    bid = datetime.datetime.strptime(content["bid_until"], "%Y-%m-%d %H:%M:%S")

    product.bid_set(bid)

    resp = api_resp(
        0, "info", "Product: " + str(id) + ' (' + str(product.title) + ') ' +
        "set bid for " + bid.strftime("%Y-%m-%d %H:%M:%S"))

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')
def delete_product(id):
    # TODO doc
    product = Products.query.get(int(id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    if product.user_id != current_user.id:
        raise UserNotPermission(str(current_user.id), "This user doesnt own this product" + str(id))

    Products.query.get(int(id)).delete_me()
    resp = api_resp(0, "info", "Product: " + str(id) + " deleted")

    return Response(json.dumps(resp), status=200, content_type='application/json')
def payment_check(id):
    if not current_user.is_mod:
        raise UserNotPermission(str(current_user.nick))

    pay = Payments.query.get(int(id))

    if pay is None:
        raise ProductException(str(id), "Payment of product not found")

    Payments.query.get(int(id)).delete_me()
    resp = api_resp(0, "info", "Payment: " + str(id) + " deleted")

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')
def new_interest():

    if not request.is_json:
        raise JSONExceptionHandler()

    content = request.get_json()
    categories_list = content["list"]
    user = current_user.id

    for cat in categories_list:
        if not Categories.exist(cat):
            ProductException(cat, "Invalid categorie: " + cat)
        Interests.add_interest(cat, user)

    resp = api_resp(0, "info", "Interest pushed")

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')
def get_prod_info(id):

    product = Products.query.get(int(id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    categories = CatProducts.get_cat_names_by_prod(id)
    cats = []
    for cat in categories:
        cats.append(fix_str(str(cat)))

    product.increment_views()

    images = Images.get_images_by_prod(id)
    imgs = []
    for img in images:
        imgs.append(fix_str(str(img)))

    product_json = {

        "id": int(product.id),
        "descript": str(product.descript),
        "user_id": int(product.user_id),
        "user_nick": str(Users.get_nick(product.user_id)),
        "price": float(product.price),
        "categories": cats,
        "title": str(product.title),
        "bid_date": str(product.bid_date),
        "boost_date": str(product.id),
        "visits": int(product.visits),
        "followers": int(product.followers),
        "publish_date": str(product.publish_date),
        "main_img": str(product.main_img),
        "photo_urls": imgs,
        "sold": str(product.is_removed),
        "place": str(product.place),
        "ban_reason": str(product.ban_reason)

    }

    return Response(json.dumps(product_json), status=200, content_type='application/json')
Пример #10
0
def create_product():

    if not request.is_json:
        raise JSONExceptionHandler()

    content = request.get_json()

    title = content["title"]
    price = float(content["price"])
    user_id = str(current_user.id)
    descript = content["descript"]
    categories = content["categories"]
    photo_urls = content["photo_urls"]
    place = content["place"]
    main_img = content["main_img"]

    if not isinstance(categories, list):
        raise JSONExceptionHandler("Bad format for categories, need an array")

    if not isinstance(photo_urls, list):
        raise JSONExceptionHandler("Bad format for photo_urls, need an array")

    product_id = Products.new_product(user_id, title, descript, price, place, main_img)

    for cat in categories:
        if len(cat) <= 1:
            raise ProductException(title, "Invalid categorie: " + cat)
        Categories.add_cat(cat)
        CatProducts.add_prod(cat, product_id)

    for photo in photo_urls:
        Images.add_photo(photo, product_id)

    # Notificaciones
    for cat in CatProducts.get_cat_names_by_prod(product_id):
        users_ids = Interests.get_users_interest_cat(cat)
        for user_id in users_ids:
            push_notify(user_id, "Nuevo producto en una categoria que te interesa", int(product_id), cat)

    resp = api_resp(0, "info", str(product_id))

    return Response(json.dumps(resp), status=200, content_type='application/json')
Пример #11
0
def create_trade():
    if not request.is_json:
        raise JSONExceptionHandler()

    content = request.get_json()

    seller_id = content["seller_id"]
    buyer_id = content["buyer_id"]
    product_id = int(content["product_id"])

    product = Products.query.get(int(product_id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    trade_id = Trades.add(product_id, seller_id, buyer_id)

    resp = api_resp(0, "info", str(trade_id))

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')
Пример #12
0
def bid_down_prod(id):

    product = Products.query.get(int(id))

    if product is None:
        raise ProductException(str(id), "Product not found")

    if product.user_id != current_user.id:
        raise UserNotPermission(str(current_user.id),
                                "This user doesnt own this product" + str(id))

    bid = None

    product.bid_set(bid)

    resp = api_resp(
        0, "info", "Product: " + str(id) + ' (' + str(product.title) + ') ' +
        "bid finished")

    return Response(json.dumps(resp),
                    status=200,
                    content_type='application/json')