Skip to content

Commit

Permalink
update fatsecret views and cache work
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniy-Golodnykh committed Jun 9, 2024
1 parent abfd3c7 commit b128f68
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
Binary file modified blackfox/db.sqlite3
Binary file not shown.
8 changes: 5 additions & 3 deletions blackfox/fatsecret_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from django.urls import path

from fatsecret_api.views import (
AccessTokenView, FoodDiaryView, RequestTokenView, WeightDiaryView
AccessTokenView, FoodDiaryDailyView, FoodDiaryMonthlyView,
RequestTokenView, WeightDiaryView
)

urlpatterns = [
path('request/', RequestTokenView.as_view(), name='get_request_token'),
path('access/', AccessTokenView.as_view(), name='get_access_token'),
path('foods/', FoodDiaryView.as_view(), name='get_foods'),
path('weights/', WeightDiaryView.as_view(), name='get_weights'),
path('weights/', WeightDiaryView.as_view(), name='weights'),
path('foods_daily/', FoodDiaryDailyView.as_view(), name='foods_daily'),
path('foods_mothly/', FoodDiaryMonthlyView.as_view(), name='foods_monthly')
]
42 changes: 32 additions & 10 deletions blackfox/fatsecret_api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import datetime as dt

from django.core.cache import cache
from django.shortcuts import redirect
Expand All @@ -16,6 +17,7 @@
BASE_URL = 'https://platform.fatsecret.com/rest/server.api'
CALLBACK_URL = os.getenv('FATSECRET_CALLBACK_URL')

error_date_message = 'Incorrect date format, should be YYYY-MM-DD or YYMMDD'
error_request_message = 'Missing FatSecret verification code or request tokens'
success_message = 'FatSecret account successfully linked'

Expand All @@ -29,16 +31,21 @@
)


def unix_date_converter(date):
epoch = dt.date.fromtimestamp(0)
if type(date) is int:
return epoch + dt.timedelta(date)
return (dt.date.fromisoformat(date) - epoch).days


class RequestTokenView(APIView):

def get(self, request):
request_token, request_token_secret = fatsecret.get_request_token(
method='GET', params={'oauth_callback': CALLBACK_URL}
)
authorize_url = fatsecret.get_authorize_url(request_token)
cache.set('request_token', request_token)
cache.set('request_token_secret', request_token_secret)
cache.set('user', request.user)
cache.set(request_token, (request_token_secret, request.user), 900)

return Response(
{'authorize_url': authorize_url},
Expand All @@ -51,10 +58,9 @@ class AccessTokenView(APIView):

def get(self, request):
verifier = request.query_params.get('oauth_verifier')
request_token = cache.get('request_token')
request_token_secret = cache.get('request_token_secret')
user = cache.get('user')
cache.clear()
request_token = request.query_params.get('oauth_token')
request_token_secret, user = cache.get(request_token, (None, None))
cache.delete(request_token)
if not verifier or not request_token or not request_token_secret:
return Response(
{'message': error_request_message},
Expand Down Expand Up @@ -88,15 +94,31 @@ def get(self, request):
session = fatsecret.get_session(
token=(access_token, access_token_secret)
)
data = session.get(BASE_URL, params=self.params).json()

date = request.query_params.get('date')
print(date)
if date:
try:
self.params['date'] = unix_date_converter(date)
except ValueError:
return Response(
{'message': error_date_message},
status=status.HTTP_400_BAD_REQUEST
)

fatsecret_data = session.get(BASE_URL, params=self.params).json()
session.close()

return Response(data, status=status.HTTP_200_OK)
return Response(fatsecret_data, status=status.HTTP_200_OK)


class FoodDiaryView(FatsecretDataView):
class FoodDiaryMonthlyView(FatsecretDataView):
params = {'method': 'food_entries.get_month.v2', 'format': 'json'}


class FoodDiaryDailyView(FatsecretDataView):
params = {'method': 'food_entries.get.v2', 'format': 'json'}


class WeightDiaryView(FatsecretDataView):
params = {'method': 'weights.get_month.v2', 'format': 'json'}

0 comments on commit b128f68

Please sign in to comment.