Skip to content

Commit

Permalink
Refactor Overview Utilities and Add Food Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffshek committed Sep 13, 2020
1 parent a965e9b commit c716b0a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
76 changes: 75 additions & 1 deletion open/core/betterself/tests/views/test_aggregrate_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
SupplementFactory,
SupplementLogFactory,
DailyProductivityLogFactory,
FoodFactory,
FoodLogFactory,
)
from open.core.betterself.models.food_logs import FoodLog
from open.core.betterself.models.supplement_log import SupplementLog
from open.core.betterself.tests.mixins.resource_mixin import BaseTestCase
from open.core.betterself.utilities.user_date_utilities import (
serialize_date_to_user_localized_datetime,
Expand Down Expand Up @@ -78,7 +82,6 @@ def test_url(self):

def test_url_with_supplements_requested(self):
url = reverse(BetterSelfResourceConstants.AGGREGATE)

start_date = "2020-01-02"

supplements = SupplementFactory.create_batch(2, user=self.user_1)
Expand All @@ -103,3 +106,74 @@ def test_url_with_supplements_requested(self):
self.assertEqual(response.status_code, 200, response.data)

self.assertIsNotNone(response.data["supplements"])

def test_url_with_supplements_requested_filter(self):
url = reverse(BetterSelfResourceConstants.AGGREGATE)
start_date = "2020-01-02"

supplements = SupplementFactory.create_batch(2, user=self.user_1)
start_time = serialize_date_to_user_localized_datetime(
start_date, user=self.user_1
)

for supplement in supplements:
SupplementLogFactory(
user=self.user_1, supplement=supplement, time=start_time
)

# only do one uuid
supplement_uuids = [str(supplement.uuid) for supplement in supplements[:1]]

kwargs = {
"start_date": "2020-01-01",
"end_date": "2020-01-02",
"supplement_uuids": supplement_uuids,
}

response = self.client_1.post(url, data=kwargs, format="json")
self.assertEqual(response.status_code, 200, response.data)

expected_log_count = SupplementLog.objects.filter(
user=self.user_1, supplement__uuid__in=supplement_uuids
).count()

returned_log_count = len(response.data["supplements"]["logs"])
self.assertEqual(expected_log_count, returned_log_count)

def test_url_with_foods_requested_filter(self):
url = reverse(BetterSelfResourceConstants.AGGREGATE)
start_date = "2020-01-02"
key_type = "foods"
key_uuid_label = "food_uuids"
instanceFactory = FoodFactory
instanceLogFactory = FoodLogFactory
instanceLogModel = FoodLog

instances = instanceFactory.create_batch(2, user=self.user_1)
start_time = serialize_date_to_user_localized_datetime(
start_date, user=self.user_1
)

for instance in instances:
instanceLogFactory(user=self.user_1, food=instance, time=start_time)

# only do one uuid
instance_uuids = [str(instance.uuid) for instance in instances[:1]]

self.assertEqual(len(instance_uuids), 1)

kwargs = {
"start_date": "2020-01-01",
"end_date": "2020-01-02",
key_uuid_label: instance_uuids,
}

response = self.client_1.post(url, data=kwargs, format="json")
self.assertEqual(response.status_code, 200, response.data)

expected_log_count = instanceLogModel.objects.filter(
user=self.user_1, food__uuid__in=instance_uuids
).count()

returned_log_count = len(response.data[key_type]["logs"])
self.assertEqual(expected_log_count, returned_log_count)
23 changes: 20 additions & 3 deletions open/core/betterself/utilities/history_overview_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
FOOD_LOG_TYPE = "Food"


def get_overview_supplements_data(user, start_period, end_period, supplements=None):
def get_overview_supplements_data(
user, start_period, end_period, filter_supplements=None
):
response = {
"start_period": start_period.date().isoformat(),
"end_period": end_period.date().isoformat(),
Expand All @@ -81,6 +83,9 @@ def get_overview_supplements_data(user, start_period, end_period, supplements=No
if not supplement_logs.exists():
return response

if filter_supplements:
supplement_logs = supplement_logs.filter(supplement__in=filter_supplements)

supplement_name_cache = {}

for log in supplement_logs:
Expand Down Expand Up @@ -278,10 +283,11 @@ def get_overview_activity_data(user, start_period, end_period):
return response


def get_overview_food_data(user, start_period, end_period):
def get_overview_food_data(user, start_period, end_period, filter_foods=None):
response = {
"start_period": start_period.date().isoformat(),
"end_period": end_period.date().isoformat(),
"daily_logs": defaultdict(list),
"logs": [],
"log_type": FOOD_LOG_TYPE,
}
Expand All @@ -294,7 +300,18 @@ def get_overview_food_data(user, start_period, end_period):
if not logs.exists():
return response

response["logs"] = FoodLogReadSerializer(logs, many=True).data
if filter_foods:
logs = logs.filter(food__in=filter_foods)

for log in logs:
user_timezone = user.timezone
normalized_time = user_timezone.normalize(log.time)
log_date = normalized_time.date().isoformat()

serialized_log = FoodLogReadSerializer(log).data
response["logs"].append(serialized_log)
response["daily_logs"][log_date].append(serialized_log)

return response


Expand Down
17 changes: 13 additions & 4 deletions open/core/betterself/views/aggregrate_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from open.core.betterself.models.food import Food
from open.core.betterself.models.supplement import Supplement
from open.core.betterself.serializers.aggregrate_serializers import (
AggregrateViewParamsSerializer,
)
from open.core.betterself.utilities.history_overview_utilities import (
get_overview_supplements_data,
get_overview_food_data,
)
from open.core.betterself.utilities.user_date_utilities import (
serialize_date_to_user_localized_datetime,
Expand Down Expand Up @@ -49,12 +51,19 @@ def post(self, request):
user=user,
start_period=start_period,
end_period=end_period,
supplements=supplements,
filter_supplements=supplements,
)
response["supplements"] = supplements_data

# productivity_data = get_overview_productivity_data(
# user=user, start_period=start_period, end_period=end_period
# )
food_uuids = data.get("food_uuids")
if food_uuids:
filter_foods = Food.objects.filter(uuid__in=food_uuids)
foods_data = get_overview_food_data(
user=user,
start_period=start_period,
end_period=end_period,
filter_foods=filter_foods,
)
response["foods"] = foods_data

return Response(response)

0 comments on commit c716b0a

Please sign in to comment.