Skip to content

Commit

Permalink
last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
The0nlyJuan committed Jul 28, 2024
1 parent a64923d commit 78a370a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
27 changes: 27 additions & 0 deletions food/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
from django.test import TestCase
from django.urls import reverse
from .models import Ingredient, Food
from django.contrib.auth import get_user_model

User = get_user_model()

class FoodTests(TestCase):

def setUp(self):
self.user = User.objects.create_user(username='testuser', password='password123')

def test_add_ingredient(self):
self.client.login(username='testuser', password='password123')
response = self.client.post(reverse('food:add'), {'name': 'Tomato'})
self.assertEqual(response.status_code, 302)
self.assertTrue(Ingredient.objects.filter(name='Tomato').exists())

def test_view_food(self):
self.client.login(username='testuser', password='password123')
ingredient = Ingredient.objects.create(name='Tomato')
food = Food.objects.create(title='Tomato Soup')
food.ingredients.add(ingredient)
food.save()

response = self.client.get(reverse('food:foods'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Tomato Soup')

# Create your tests here.
20 changes: 11 additions & 9 deletions food/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,23 @@ def search_ingredient_by_name(ingredient_name):
def fetch_nutritional_data(query):
url = 'https://trackapi.nutritionix.com/v2/natural/nutrients'
headers = {
'x-app-id': "53a454b3",
'x-app-key': "4371285aacc1cc0fbc6442107b2d1e8c",
'x-app-id': "3ca95e27",
'x-app-key': "96b8251cd05642682d9ceba27cbe58e5",
'Content-Type': 'application/json'
}
data = {
'query': query
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
else:
except requests.RequestException as e:
print(f"API request failed: {e}")
return None

def parse_and_sum_nutritionix_response(response):
if 'foods' not in response:
if response is None or 'foods' not in response:
return {}

total_nutrition_info = {
Expand All @@ -211,7 +213,7 @@ def parse_and_sum_nutritionix_response(response):
'phosphorus': 0
}

for food_data in response['foods']:
for food_data in response.get('foods', []):
total_nutrition_info['calories'] += food_data.get('nf_calories', 0) or 0
total_nutrition_info['total_fat'] += food_data.get('nf_total_fat', 0) or 0
total_nutrition_info['saturated_fat'] += food_data.get('nf_saturated_fat', 0) or 0
Expand Down Expand Up @@ -501,8 +503,8 @@ def add(request):
def fetch_food_image(query):
url = 'https://trackapi.nutritionix.com/v2/natural/nutrients'
headers = {
'x-app-id': "cd4669b1", # Your Nutritionix app ID
'x-app-key': "d0035d1ec68e6365495ae86e7b766a2b", # Your Nutritionix API key
'x-app-id': "3ca95e27", # Your Nutritionix app ID
'x-app-key': "96b8251cd05642682d9ceba27cbe58e5", # Your Nutritionix API key
'Content-Type': 'application/json'
}
data = {
Expand Down
4 changes: 2 additions & 2 deletions plateful_palette/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@



NUTRITIONIX_API_KEY = 'd0035d1ec68e6365495ae86e7b766a2b'
NUTRITIONIX_APP_ID = 'cd4669b1'
NUTRITIONIX_API_KEY = '96b8251cd05642682d9ceba27cbe58e5'
NUTRITIONIX_APP_ID = '3ca95e27'


# Password validation
Expand Down

0 comments on commit 78a370a

Please sign in to comment.