Beispiel #1
0
def random_add_meal(request):
    args = [('food_types', True, str, '', 100000),
            ('name', True, str, '未定义', 50), ('year', True, int, -1),
            ('month', True, int, -1), ('day', True, int, -1)]
    args = get_post_args(request.POST, args)
    if not args[0]:
        return HttpResponse(Response(m='get args error: %s' % args[1],
                                     c='-1').toJson(),
                            content_type='application/json')
    args = args[1]
    user = User.objects.get(id=request.session['uid'])
    # 创建新的一餐
    newMeal = Meal(
        date=datetime.date(args['year'], args['month'], args['day']))
    newMeal.name = args['name']
    newMeal.user = user
    newMeal.save()
    FOOD_TYPE = map(lambda x: x.strip(), json.loads(args['food_types']))
    # 设置该餐中的食物
    for food_type in FOOD_TYPE:
        if food_type == '':
            continue
        foods = user_to_foods(user, food_type)
        print food_type, foods
        if len(foods) > 0:
            food = random.choice(foods)
            food_amount = RS_Food_Amount(food=food, meal=newMeal, amount=0)
            food_amount.save()
    return HttpResponse(Response(m='随机生成成功').toJson(),
                        content_type='application/json')
Beispiel #2
0
def register(request):
  if request.method == 'GET':
    return render(request, 'exhibit/user_register.html')
  if request.method != 'POST':
    return HttpResponseServerError('request method error: %s' % request.method)
  args = [('account', True, str, '', 50), ('nickname', True, str, '', 20), ('password', True, str, '', 50)]
  args = get_post_args(request.POST, args)
  if not args[0]:
    return HttpResponse(Response(m='get args error: %s' % args[1], c='-1').toJson(), content_type='application/json')
  args = args[1]
  # 判断账号是否存在
  if len(User.objects.filter(account=args['account'])) > 0:
    return HttpResponse(Response(m='该邮箱已注册过账号: %s' % args['account'], c=1).toJson(), content_type="application/json")
  # 判断昵称是否存在
  if len(User.objects.filter(nickname=args['nickname'])) > 0:
    return HttpResponse(Response(m='该昵称已存在: %s' % args['nickname'], c=2).toJson(), content_type="application/json")
  newUser = User()
  newUser.account = args['account']
  newUser.nickname = args['nickname']
  newUser.password = args['password']
  newUser.save()
  # 新建一个market
  market = Market(user=newUser)
  market.save()
  return HttpResponse(Response(m='注册成功').toJson(), content_type="application/json")
Beispiel #3
0
def exact_add_meal(request):
    args = [('fids', True, str, '[]', 100000), ('name', True, str, '未定义', 50),
            ('year', True, int, -1), ('month', True, int, -1),
            ('day', True, int, -1)]
    args = get_post_args(request.POST, args)
    if not args[0]:
        return HttpResponse(Response(m='get args error: %s' % args[1],
                                     c='-1').toJson(),
                            content_type='application/json')
    args = args[1]
    user = User.objects.get(id=request.session['uid'])
    # 创建新的一餐
    newMeal = Meal(
        date=datetime.date(args['year'], args['month'], args['day']))
    newMeal.name = args['name']
    newMeal.user = user
    newMeal.save()
    fids = map(lambda x: x.strip(), json.loads(args['fids']))
    # 设置该餐中的食物
    for fid in fids:
        food = Food.objects.get(id=fid)
        food_amount = RS_Food_Amount(food=food, meal=newMeal, amount=0)
        food_amount.save()
    return HttpResponse(Response(m='随机生成成功').toJson(),
                        content_type='application/json')
Beispiel #4
0
def add(request):
    if request.method != 'POST':
        raise Http404
    args = [('id', False, long, -1), ('name', True, str, '', 20),
            ('calorie', True, float, 0.0), ('category', True, str, '', 20),
            ('GI', False, float, 0.0)]
    args = Gymer.utils.get_post_args(request.POST, args)
    if not args[0]:
        return HttpResponse(Response(m='get args error: %s' % args[1],
                                     c='-1').toJson(),
                            content_type='application/json')
    args = args[1]
    food = None
    try:
        food = Food.objects.get(id=args['id'])
    except:
        food = Food()
    # 判断该名称的食物是否已经在数据库中
    if len(Food.objects.filter(name=args['name'])) > 0 and (
            not food.id or (food.id and food.id != args['id'])):
        return HttpResponse(Response(m='名称为 %s 的食物已存在数据库中' % args['name'],
                                     c=1).toJson(),
                            content_type='application/json')
    food.name = args['name']
    food.category = args['category']
    food.calorie = args['calorie']
    food.GI = args['GI']
    food.save()
    return HttpResponse(Response(m=food.id).toJson(),
                        content_type='application/json')
Beispiel #5
0
def queryByName(request):
  user = User.objects.get(id=request.session['uid'])
  args = [('name', True, str, '', 20)]
  args = get_post_args(request.POST, args)
  if not args[0]:
    return HttpResponse(Response(m='get args error: %s' % args[1], c='-1').toJson(), content_type='application/json')
  args = args[1]
  foods = user_to_foods(user)
  foods = filter(lambda x:x.name.startswith(args['name']), foods)
  return HttpResponse(Response(m=serializers.serialize("json", foods)).toJson(), content_type="application/json")
Beispiel #6
0
def add(request):
  user = User.objects.get(id=request.session['uid'])
  args = [('fid', True, long, -1)]
  args = get_post_args(request.POST, args)
  if not args[0]:
    return HttpResponse(Response(m='get args error: %s' % args[1], c='-1').toJson(), content_type='application/json')
  args = args[1]
  food = None
  try:
    food = Food.objects.get(id=args['fid'])
  except:
    return HttpResponse(Response(m='query food not exist: %s' % args['fid'], c='-2').toJson(), content_type='application/json')
  if len(RS_Preference.objects.filter(food=food).filter(market=user.market)) > 0:
    return HttpResponse(Response(m='食物 %s 已经存在你的食材库中' % food.name, c='1').toJson(), content_type='application/json')
  preference = RS_Preference(food=food, market=user.market)
  preference.save()
  return HttpResponse(Response(m='成功将 %s 添加到食材库中' % food.name).toJson(), content_type='application/json')
Beispiel #7
0
def set(request):
  user = User.objects.get(id=request.session['uid'])
  if request.method == 'GET':
    return render(request, 'exhibit/user_set.html', {'user' : user, 'active_page' : 'user'})
  args = [('height', True, int, 160), ('weight', True, int, 40), ('gender', True, int, 0), ('age', True, int, 18), ('exercise_level', True, int, 0)]
  args = get_post_args(request.POST, args)
  if not args[0]:
    return HttpResponse(Response(m='get args error: %s' % args[1], c='-1').toJson(), content_type='application/json')
  args = args[1]
  user.height = args['height']
  user.weight = args['weight']
  user.gender = args['gender']
  user.age = args['age']
  user.exercise_level = args['exercise_level']
  user.BMI = get_bmi(user)
  user.BMR = get_bmr(user)
  user.save()
  return HttpResponse(Response(m='修改用户信息成功').toJson(), content_type="application/json")
Beispiel #8
0
def login(request):
  if request.method == 'GET':
    return render(request, 'exhibit/user_login.html')
  args = [('account', True, str, '', 50), ('password', True, str, '', 50)]
  args = get_post_args(request.POST, args)
  if not args[0]:
    return HttpResponse(Response(m='get args error: %s' % args[1], c='-1').toJson(), content_type='application/json')
  args = args[1]
  user = None
  try:
    user = User.objects.get(account=args['account'])
  except:
    return HttpResponse(Response(m='该账号不存在: %s' % args['account'], c=-2).toJson(), content_type="application/json")
  if user.password == args['password']:
    request.session['uid'] = user.id
    back_url = '/'
    if request.session.has_key('back_url'):
      back_url = request.session['back_url']
      del request.session['back_url']
    return HttpResponse(Response(m='登陆成功').toJson(), content_type="application/json")
  return HttpResponse(Response(m='密码错误', c=3).toJson(), content_type="application/json")
Beispiel #9
0
def update(request):
    args = [('foods', True, str, '[]', 100000), ('mid', True, long, -1)]
    args = get_post_args(request.POST, args)
    if not args[0]:
        return HttpResponse(Response(m='get args error: %s' % args[1],
                                     c='-1').toJson(),
                            content_type='application/json')
    args = args[1]
    meal = None
    try:
        meal = Meal.objects.get(id=args['mid'])
    except:
        return HttpResponse(Response(m='query meal not exist: %s' %
                                     args['mid'],
                                     c='-2').toJson(),
                            content_type='application/json')
    newFoods = json.loads(args['foods'])
    fa_set = []
    for food in newFoods:
        r_food = Food.objects.get(id=food[0])
        food_amount = food[1]
        rs_food_amount = meal.rs_food_amount_set.filter(food=r_food).filter(
            meal=meal)
        if len(rs_food_amount) > 0:
            rs_food_amount = rs_food_amount[0]
        else:
            rs_food_amount = RS_Food_Amount(food=r_food, meal=meal)
        rs_food_amount.amount = food_amount
        rs_food_amount.save()
        fa_set.append(rs_food_amount.id)
    # 将多于的rs_food_amount删除
    for fa in meal.rs_food_amount_set.all():
        if fa.id not in fa_set:
            fa.delete()
    # 计算一餐的卡路里
    meal.calorie, meal.GI = get_meal_calorie_GI(meal)
    meal.save()
    return HttpResponse(Response(m='更新餐信息成功').toJson(),
                        content_type="application/json")
Beispiel #10
0
def delete(request):
    if request.method != 'POST':
        raise Http404
    args = [('id', True, long, -1)]
    args = Gymer.utils.get_post_args(request.POST, args)
    if not args[0]:
        return HttpResponse(Response(m='get args error: %s' % args[1],
                                     c='-1').toJson(),
                            content_type='application/json')
    args = args[1]
    print args
    food = None
    try:
        food = Food.objects.get(id=args['id'])
    except:
        return HttpResponse(Response(m='query object not exist: %s' %
                                     args['id'],
                                     c='-2').toJson(),
                            content_type='application/json')
    food.delete()
    return HttpResponse(Response(m='delete food').toJson(),
                        content_type='application/json')
Beispiel #11
0
def delete(request):
    user = User.objects.get(id=request.session['uid'])
    args = [('mid', True, long, -1)]
    args = get_post_args(request.POST, args)
    if not args[0]:
        return HttpResponse(Response(m='get args error: %s' % args[1],
                                     c='-1').toJson(),
                            content_type='application/json')
    args = args[1]
    meal = None
    try:
        meal = Meal.objects.get(id=args['mid'])
    except:
        return HttpResponse(Response(m='query meal not exist: %s' %
                                     args['mid'],
                                     c='-2').toJson(),
                            content_type='application/json')
    if (user != meal.user):
        return HttpResponse(Response(m='待删除餐不输入当前登陆用户', c='1').toJson(),
                            content_type='application/json')
    meal.delete()
    return HttpResponse(Response(m='删除成功').toJson(),
                        content_type='application/json')
Beispiel #12
0
def logout(request):
  del request.session['uid']
  return HttpResponse(Response(m='登出成功').toJson(), content_type="application/json")