示例#1
0
def subscribe(request):
    body = request.json
    properties = ["user_id", "subscribe"]
    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)
    user_id = body.get("user_id")
    subscribe = body.get("subscribe")
    for obj in subscribe:
        key = user_id + "_" + obj.get("product_id")
        value = obj.get("when")
        if value not in settings.ALLOWED_NOTIFICATION_TYPES:
            continue
        data = subscriptions.get(key)
        if not data:
            data = [value]
        else:
            if value not in data:
                data.append(value)
        product_subs_key = settings.PRODUCT_SUBSCRIBER_CACHE_KEY + obj.get("product_id")
        product_subscribers = cache.get(product_subs_key, set())
        product_subscribers.add(user_id)
        cache[product_subs_key] = product_subscribers
        subscriptions[key] = data
    response = {"success": True}
    return Response(json=response)
示例#2
0
def subscribe(request):
    body = request.json
    properties = ['user_id', 'subscribe']
    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)
    user_id = body.get('user_id')
    subscribe = body.get('subscribe')
    for obj in subscribe:
        key = user_id + '_' + obj.get('product_id')
        value = obj.get('when')
        if value not in settings.ALLOWED_NOTIFICATION_TYPES:
            continue
        data = subscriptions.get(key)
        if not data:
            data = [value]
        else:
            if value not in data:
                data.append(value)
        product_subs_key = settings.PRODUCT_SUBSCRIBER_CACHE_KEY + \
            obj.get('product_id')
        product_subscribers = cache.get(product_subs_key, set())
        product_subscribers.add(user_id)
        cache[product_subs_key] = product_subscribers
        subscriptions[key] = data
    response = {
        'success': True
    }
    return Response(json=response)
示例#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