Skip to content

Commit

Permalink
diet records view added.
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaafshar79 committed Jun 12, 2020
1 parent 2649fd7 commit 85510f7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
53 changes: 41 additions & 12 deletions create_elastic_index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"metadata": {
"pycharm": {
"is_executing": false
Expand All @@ -11,11 +11,10 @@
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\dailydiet\\dailydiet-api\\venv\\lib\\site-packages\\whitenoise\\base.py:115: UserWarning: No directory at: D:\\dailydiet\\dailydiet-api\\admin\\static\\\n",
" warnings.warn(u\"No directory at: {}\".format(root))\n"
]
"d:\\dailydiet\\dailydiet-api\\venv\\lib\\site-packages\\whitenoise\\base.py:115: UserWarning: No directory at: D:\\dailydiet\\dailydiet-api\\admin\\static\\\n warnings.warn(u\"No directory at: {}\".format(root))\n"
],
"output_type": "stream"
}
],
"source": [
Expand All @@ -26,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {
"pycharm": {
"is_executing": false,
Expand All @@ -35,7 +34,7 @@
},
"outputs": [],
"source": [
"from foods.models import Food\n",
"from foods.models import Food,DietRecord\n",
"from extentions import elastic"
]
},
Expand Down Expand Up @@ -396,12 +395,42 @@
"s[0].execute().to_dict()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/plain": "[]"
},
"metadata": {},
"output_type": "execute_result",
"execution_count": 4
}
],
"source": [
"DietRecord.query.filter(DietRecord.ownerId == 1).order_by(DietRecord.generatedAt.desc()) \\\n",
" .limit(10).all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"\n"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
Expand All @@ -425,13 +454,13 @@
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
},
"source": []
}
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
}
32 changes: 28 additions & 4 deletions foods/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from extentions import db


def submit_diet_record(food_ids,jwt_identity):
def submit_diet_record(food_ids, jwt_identity):
user = User.query.filter_by(Email=jwt_identity).first()
if user is None:
return
Expand All @@ -33,7 +33,7 @@ def get_yevade(calorie):
if catdog is None:
return jsonify({'error': 'Not Found'}), 404
else:
submit_diet_record([catdog[0].id],get_jwt_identity())
submit_diet_record([catdog[0].id], get_jwt_identity())
return jsonify({'diet': [catdog[0].simple_view, catdog[1]]}), 200
else:
return jsonify({'error': 'I\'m a teapot'}), 418
Expand All @@ -54,7 +54,7 @@ def get_dovade(calorie):
if catdog is None:
return jsonify({'error': 'Not Found'}), 404
else:
submit_diet_record([catdog[0].id, catdog[1].id],get_jwt_identity())
submit_diet_record([catdog[0].id, catdog[1].id], get_jwt_identity())
return jsonify({'diet': [catdog[0].simple_view, catdog[1].simple_view, catdog[2]]}), 200
else:
return jsonify({'error': 'I\'m a teapot'}), 418
Expand All @@ -77,7 +77,7 @@ def get_sevade(calorie):
if catdog is None:
return jsonify({'error': 'Not Found'}), 404
else:
submit_diet_record([catdog[0].id, catdog[1].id, catdog[2].id],get_jwt_identity())
submit_diet_record([catdog[0].id, catdog[1].id, catdog[2].id], get_jwt_identity())
return jsonify(
{'diet': [catdog[0].simple_view, catdog[1].simple_view, catdog[2].simple_view, catdog[3]]}), 200
else:
Expand Down Expand Up @@ -143,3 +143,27 @@ def food_search():
'results': [set_placeholder(result.simple_view) for result in results.all()],
'total_results_count': count
})


@jwt_required
@foods.route('/diets')
def get_diet_records():
page = int(request.args.get('page', 1))
per_page = int(request.args.get('per_page', 10))
user = User.query.filter_by(Email=get_jwt_identity()).first()
if user is None:
return {
"error": "user not found"
}, 404
results = DietRecord.query.filter(DietRecord.ownerId == user.id).order_by('generated_at desc') \
.limit(per_page) \
.offset((page - 1) * per_page).all()

payload = []
for diet_record in results:
payload.append({
"diet": [Food.query.get_or_404(food_id).simple_view for food_id in diet_record.diet],
"time": diet_record.generatedAt
})

return payload

0 comments on commit 85510f7

Please sign in to comment.