Beispiel #1
0
def priceDataPoint(request):
    body = request.json
    properties = ['product_id', 'price', 'url']
    for a_property in properties:
        if not body.get(a_property):
            data = u'{} Missing in request'.format(a_property)
            return Response(json=data, status=400)
        if a_property is 'price':
            try:
                int(body.get('price'))
            except:
                error = {
                    'Price must be a integer'
                }
                return Response(json=error, status=400)
    product_id = body.get('product_id')
    price = int(body.get('price'))
    url = body.get('url')
    product = products.get(product_id)
    if not product:
        obj = dict()
        obj['product_id'] = product_id
        obj['price'] = price
        obj['url'] = url
        obj['alltimelow'] = price
        products[product_id] = obj
        notifications(product_id, ['ALWAYS'])
    else:
        events = list()
        price_diff = product['price'] - price
        ten_percent_of_product_price = float(product['price']) * float(0.1)
        if price < product['alltimelow']:
            events.append(settings.ALL_TIME_LOW)
        if price_diff > ten_percent_of_product_price:
            events.append(settings.MORE_THAN_10)
        if price < product['price']:
            events.append(settings.ALWAYS)

        product['price'] = price
        if events:
            if settings.ALL_TIME_LOW in events:
                product['alltimelow'] = price
            notifications(product_id, events)
        products[product_id] = product
    response = {
        'success': True
    }
    return Response(json=response)
Beispiel #2
0
def priceDataPoint(request):
    body = request.json
    properties = ["product_id", "price", "url"]
    for a_property in properties:
        if not body.get(a_property):
            data = u"{} Missing in request".format(a_property)
            return Response(json=data, status=400)
        if a_property is "price":
            try:
                int(body.get("price"))
            except:
                error = {"Price must be a integer"}
                return Response(json=error, status=400)
    product_id = body.get("product_id")
    price = int(body.get("price"))
    url = body.get("url")
    product = products.get(product_id)
    if not product:
        obj = dict()
        obj["product_id"] = product_id
        obj["price"] = price
        obj["url"] = url
        obj["alltimelow"] = price
        products[product_id] = obj
        notifications(product_id, ["ALWAYS"])
    else:
        events = list()
        price_diff = product["price"] - price
        ten_percent_of_product_price = float(product["price"]) * float(0.1)
        if price < product["alltimelow"]:
            events.append(settings.ALL_TIME_LOW)
        if price_diff > ten_percent_of_product_price:
            events.append(settings.MORE_THAN_10)
        if price < product["price"]:
            events.append(settings.ALWAYS)

        product["price"] = price
        if events:
            if settings.ALL_TIME_LOW in events:
                product["alltimelow"] = price
            notifications(product_id, events)
        products[product_id] = product
    response = {"success": True}
    return Response(json=response)
Beispiel #3
0
def notifications(product_id, typeList):
    if not typeList:
        return
    key = settings.PRODUCT_SUBSCRIBER_CACHE_KEY + product_id
    user_list = cache.get(key, list())
    product = products.get(product_id)
    if product:
        for a_user in user_list:
            user_key = a_user + '_' + product_id
            subs = subscriptions.get(user_key)
            common_events = [val for val in subs if val in typeList]
            if common_events:
                if settings.ALL_TIME_LOW in common_events:
                    data = {
                        'productId': product_id,
                        'url': product.get('url'),
                        'price': product.get('price'),
                        'reason': settings.ALL_TIME_LOW
                    }
                    # we can make a api call here
                    print a_user, data
                elif settings.MORE_THAN_10 in common_events:
                    data = {
                        'productId': product_id,
                        'url': product.get('url'),
                        'price': product.get('price'),
                        'reason': settings.MORE_THAN_10
                    }
                    # we can make a api call here
                    print a_user, data
                else:
                    data = {
                        'productId': product_id,
                        'url': product.get('url'),
                        'price': product.get('price'),
                        'reason': settings.ALWAYS
                    }
                    # we can make a api call here
                    print a_user, data