Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Update the userprofile via the API

Roland Geider7384450d4528
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index 5af10879a..9e3bfa9fe 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -15,10 +15,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+from django.contrib.auth.models import User
+
from rest_framework import viewsets
-from rest_framework.permissions import AllowAny
-from wger.core.models import UserProfile, Language, DaysOfWeek, License
+from wger.core.models import UserProfile
+from wger.core.models import Language
+from wger.core.models import DaysOfWeek
+from wger.core.models import License
class UserProfileViewSet(viewsets.ModelViewSet):
@@ -38,6 +42,12 @@ class UserProfileViewSet(viewsets.ModelViewSet):
def create(self, request):
pass
+ def get_owner_objects(self):
+ '''
+ Return objects to check for ownership permission
+ '''
+ return [User.objects.get(pk=self.request.DATA['user'])]
+
class LanguageViewSet(viewsets.ReadOnlyModelViewSet):
'''
diff --git a/wger/core/models.py b/wger/core/models.py
index 5a951c1da..29786246e 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -387,6 +387,12 @@ by the US Department of Agriculture. It is extremely complete, with around
entry.save()
return entry
+ def get_owner_object(self):
+ '''
+ Returns the object that has owner information
+ '''
+ return self
+
# Every new user gets a profile
@disable_for_loaddata
diff --git a/wger/core/static/js/workout-manager.js b/wger/core/static/js/workout-manager.js
index 012a0ea83..0772beb19 100644
--- a/wger/core/static/js/workout-manager.js
+++ b/wger/core/static/js/workout-manager.js
@@ -25,6 +25,48 @@
"use strict";
+
+/*
+ * AJAX related functions
+ *
+ * See https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax for
+ * more information
+ */
+function getCookie(name) {
+ var cookieValue = null;
+ if (document.cookie && document.cookie != '') {
+ var cookies = document.cookie.split(';');
+ for (var i = 0; i < cookies.length; i++) {
+ var cookie = jQuery.trim(cookies[i]);
+ // Does this cookie string begin with the name we want?
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+ break;
+ }
+ }
+ }
+ return cookieValue;
+}
+
+
+function csrfSafeMethod(method) {
+ // these HTTP methods do not require CSRF protection
+ return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
+}
+
+$.ajaxSetup({
+ crossDomain: false, // obviates need for sameOrigin test
+ beforeSend: function(xhr, settings) {
+ if (!csrfSafeMethod(settings.type)) {
+ xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
+ }
+ }
+});
+
+
+/*
+ * Others
+ */
function get_current_language() {
/* Returns a short name, like 'en' or 'de' */
return $('#current-language').data('currentLanguage');
@@ -89,6 +131,8 @@ function setup_sortable() {
}
+
+
/*
*
* Functions related to the user's preferences
@@ -106,7 +150,15 @@ function toggle_comments() {
showComment = 0;
}
- $("#ajax-info").load('/' + get_current_language() + "/workout/api/user-preferences?do=set_show-comments&show=" + showComment);
+ // Get own ID and update the user profile
+ $.get('/api/v2/userprofile', function(data) {
+ }).done(function(userprofile) {
+ $.ajax({
+ url:'/api/v2/userprofile/' + userprofile.results[0].id + '/',
+ type: 'PATCH',
+ data: {show_comments: showComment}
+ });
+ });
});
}
diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.py
index 1d13ef4b3..4a875a852 100644
--- a/wger/core/tests/test_preferences.py
+++ b/wger/core/tests/test_preferences.py
@@ -178,21 +178,6 @@ class AjaxPreferencesTestCase(WorkoutManagerTestCase):
Helper function to test the preferences page
'''
- # Set the 'show comments' option
- response = self.client.get(reverse('core:user-api-preferences'),
- {'do': 'set_show-comments',
- 'show': '1'},
- HTTP_X_REQUESTED_WITH='XMLHttpRequest')
-
- self.assertEqual('Success', response.content)
- self.assertEqual(response.status_code, 200)
-
- response = self.client.get(reverse('core:preferences'))
- profile = response.context['user'].userprofile
- self.assertTrue(profile.show_comments)
- self.assertFalse(profile.show_english_ingredients)
- self.assertEqual(response.context['user'].email, '[email protected]')
-
# Set the 'english ingredients' option
response = self.client.get(reverse('core:user-api-preferences'),
{'do': 'set_english-ingredients',
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index 6a88cb5f7..93c1aa30d 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -172,18 +172,8 @@ def api_user_preferences(request):
if request.is_ajax():
- # Show comments on workout view
- if request.GET.get('do') == 'set_show-comments':
- new_value = int(request.GET.get('show'))
-
- profile = request.user.userprofile
- profile.show_comments = new_value
- profile.save()
-
- return HttpResponse(_('Success'))
-
# Show ingredients in english
- elif request.GET.get('do') == 'set_english-ingredients':
+ if request.GET.get('do') == 'set_english-ingredients':
new_value = int(request.GET.get('show'))
profile = request.user.userprofile