示例#1
0
文件: api.py 项目: naqada/Cartouche
def saveRecipe():
	if request.method == "POST":
		recipe = request.json['recipe']
		user_id = int(request.json['user_id'])
		
		name = recipe['name']
		ingredients = recipe['ingredients']
		directions = recipe['directions']
		
		recipe_id = database.performDo('INSERT INTO recipes (name, directions, user_owner) VALUES ("%s", "%s", %d)' % (name, directions, user_id))
		
		for item in ingredients:
			ingredient_name = item['name']
			quantity = int(item['quantity'])
			units_id = int(item['units_id'])
			ingredient_result = database.performQueryOne('SELECT id FROM ingredients WHERE name="%s"' % ingredient_name)
			if ingredient_result == None:
				ingredient_id = database.performDo('INSERT INTO ingredients (name) VALUES ("%s")' % ingredient_name)
			else:
				ingredient_id = int(ingredient_result['id'])
			database.performDo('INSERT INTO l_recipes_to_ingredients (recipe_id, ingredient_id, quantity, units) VALUES (%d,%d,%d,%d)' % (recipe_id, ingredient_id, quantity, units_id))
		
		return jsonify({'success': True})

	return jsonify({'success': False})
示例#2
0
文件: api.py 项目: naqada/Cartouche
def getUserInformation(user_id):
	user_id = int(user_id)
	
	result = database.performQueryOne('SELECT id, email, full_name FROM users WHERE id=%d' % user_id)
	
	email = result['email']
	full_name = result['full_name']
	
	return jsonify({'id': user_id, 'email': email, 'full_name': full_name})
示例#3
0
文件: api.py 项目: naqada/Cartouche
def loginUser():
	if request.method == "POST":
		user_email = request.json['user_email']
		password = request.json['password']
			
		entered_password = database.performQueryOne('SELECT md5("%s") AS password' % password )['password']
		user_result = database.performQueryOne('SELECT id, full_name, password FROM users WHERE email="%s"' % user_email)
		
		correct_password = user_result['password']
		user_id = user_result['id']
		full_name = user_result['full_name']
		
		if entered_password == correct_password:
			resp = make_response(jsonify({'success': True}))
			resp.set_cookie('user_email', user_email)
			resp.set_cookie('full_name', full_name)
			resp.set_cookie('user_id', user_id)
			return resp
			
	return jsonify({'success': False})