Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Update the user's total calories via the API

Roland Geider286b81f5d7de
diff --git a/wger/nutrition/static/js/nutrition.js b/wger/nutrition/static/js/nutrition.js
index 2c76f0891..305fb6d9d 100644
--- a/wger/nutrition/static/js/nutrition.js
+++ b/wger/nutrition/static/js/nutrition.js
@@ -233,11 +233,17 @@ function init_calories_calculator()
$("#form-update-calories").click(function(e){
e.preventDefault();
- var form_url = $("#total-calories-form").attr('action');
- $.post(form_url,
- $("#total-calories-form").serialize(),
- function(data) {
- });
+
+ // Get own ID and update the user profile
+ $.get('/api/v2/userprofile', function(data) {
+ }).done(function(userprofile) {
+ var total_calories = $("#id_calories")[0].value;
+ $.ajax({
+ url:'/api/v2/userprofile/' + userprofile.results[0].id + '/',
+ type: 'PATCH',
+ data: {calories: total_calories}
+ });
+ });
});
$(".calories-autoform").click(function(e){
diff --git a/wger/nutrition/templates/mobile/rate/form.html b/wger/nutrition/templates/mobile/rate/form.html
index 5be185fd6..d84d56a04 100644
--- a/wger/nutrition/templates/mobile/rate/form.html
+++ b/wger/nutrition/templates/mobile/rate/form.html
@@ -145,7 +145,7 @@ needs based on your level of physical activity.{% endblocktrans %}</p>
might include e.g. a surplus or deficit if you are bulking or cutting. Alternatively
you might just enter yourself the amount of calories if you know the value.{% endblocktrans %}
</p>
-<form action="{% url 'calories-calculate-total' %}"
+<form action=""
method="post"
class="ym-form"
id="total-calories-form">
diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.html
index 9c1d5f274..5807cdb8a 100644
--- a/wger/nutrition/templates/rate/form.html
+++ b/wger/nutrition/templates/rate/form.html
@@ -109,7 +109,7 @@ needs based on your level of physical activity.{% endblocktrans %}</p>
might include e.g. a surplus or deficit if you are bulking or cutting. Alternatively
you might just enter yourself the amount of calories if you know the value.{% endblocktrans %}
</p>
-<form action="{% url 'calories-calculate-total' %}"
+<form action=""
method="post"
class="form-horizontal"
id="total-calories-form">
diff --git a/wger/nutrition/tests/test_calories_calculator.py b/wger/nutrition/tests/test_calories_calculator.py
index dd0f68173..673c85c10 100644
--- a/wger/nutrition/tests/test_calories_calculator.py
+++ b/wger/nutrition/tests/test_calories_calculator.py
@@ -112,23 +112,6 @@ class CaloriesCalculatorTestCase(WorkoutManagerTestCase):
result = json.loads(response.content)
self.assertEqual(result, {'bmr': '1780'})
- def test_total_calories_form(self):
- '''
- Tests the form with the total calories
- '''
-
- self.user_login('test')
- user = User.objects.get(username=self.current_user)
- self.assertEqual(user.userprofile.calories, 3500)
-
- response = self.client.post(reverse('calories-calculate-total'),
- {'additional_calories': 0,
- 'calories': 2000})
- self.assertEqual(response.status_code, 200)
-
- user = User.objects.get(username=self.current_user)
- self.assertEqual(user.userprofile.calories, 2000)
-
def test_automatic_weight_entry_bmr(self):
'''
Tests that weight entries are automatically created or updated
diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.py
index f220e9a10..b05ba084f 100644
--- a/wger/nutrition/urls.py
+++ b/wger/nutrition/urls.py
@@ -143,7 +143,4 @@ urlpatterns = patterns('wger.nutrition.views',
url(r'^calculator/calories/activities$',
calculator.calculate_activities,
name='calories-calculate-activities'), # JS
- url(r'^calculator/calories/total$',
- calculator.update_total,
- name='calories-calculate-total'), # JS
)
diff --git a/wger/nutrition/views/calculator.py b/wger/nutrition/views/calculator.py
index 3b7c9f8ed..7179caf32 100644
--- a/wger/nutrition/views/calculator.py
+++ b/wger/nutrition/views/calculator.py
@@ -100,27 +100,3 @@ def calculate_activities(request):
# Return the results to the client
return HttpResponse(data, 'application/json')
-
-
-def update_total(request):
- '''
- Calculates the total calories
-
- TODO: this really should be moved to an API so we can just update this
- setting in the user profile
- '''
-
- data = []
-
- form = DailyCaloriesForm(data=request.POST, instance=request.user.userprofile)
- if form.is_valid():
- form.save()
-
- # Save the total calories
- request.user.userprofile.calories = form.cleaned_data['calories']
- request.user.userprofile.save()
- else:
- logger.debug(form.errors)
-
- # Return the results to the client
- return HttpResponse(data, 'application/json')