コード例 #1
0
def add_ingredient(request):
    """
    Adds an ingredient to a user's ingredient list
    """
    try:
        session_id = request.get_signed_cookie('session_id', default=None)
        user_id = crypto.decrypt(request.COOKIES.get('euid', ''))
        ingredient_id = request.POST.get('ingredient_id', '')
        ingredient_name = request.POST.get('ingredient_name', '')
        ingredient = Label.query(
            Label.user_id == user_id, Label.name == ingredient_name,
            Label.sub_id == ingredient_id).get(keys_only=True)

        if session_id and not ingredient:
            response = label_api.add_ingredient(session_id, ingredient_id)

            if response.get('result') == 'success':
                Label(user_id=user_id,
                      name=ingredient_name,
                      sub_id=ingredient_id).put_async()
                return HttpResponse('{"result": "success"}',
                                    content_type='application/json')

    except:
        pass

    return HttpResponse('{"result": "failure"}',
                        content_type='application/json')
コード例 #2
0
def add_allergen(request):
    """
    Adds an allergen to a user's allergen list
    """
    try:
        session_id = request.get_signed_cookie('session_id', default=None)
        euid = request.COOKIES.get('euid')
        user = User.get_by_id(crypto.decrypt(euid))
        allergen_name = request.POST.get('allergen', '')
        allergen = Label.query(Label.user_id == user.key.id(),
                               Label.name == allergen_name).get(keys_only=True)

        if session_id and not allergen:
            profile = user.get_profile()

            for a in profile['allergens']:
                if a['name'] == allergen_name: a['value'] = 'true'

            response = label_api.set_profile(session_id, profile)

            if response.get('result') == 'success':
                Label(user_id=user.key.id(), name=allergen_name).put_async()
                return HttpResponse('{"result": "success"}',
                                    content_type='application/json')
    except:
        pass

    return HttpResponse('{"result": "failure"}',
                        content_type='application/json')