Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Make user activate external access before sharing

Roland Geiderd4705f54ed5a
diff --git a/wger/core/models.py b/wger/core/models.py
index f9d063cb8..c77864175 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -297,10 +297,10 @@ by the US Department of Agriculture. It is extremely complete, with around
default=UNITS_KG)
'''Preferred weight unit'''
- ro_access = models.BooleanField(verbose_name=_('Allow read-only access'),
- help_text=_('Allow anonymous users to access your workouts and '
- 'logs in a read-only mode. You can then share the '
- 'links to social media.'),
+ ro_access = models.BooleanField(verbose_name=_('Allow external access'),
+ help_text=_('Allow external users to access your workouts and '
+ 'logs in a read-only mode. You need to set this '
+ 'before you can share links e.g. to social media.'),
default=False)
'''Allow anonymous read-only access'''
diff --git a/wger/core/static/js/workout-manager.js b/wger/core/static/js/workout-manager.js
index ca2979fe2..4fd5f84ab 100644
--- a/wger/core/static/js/workout-manager.js
+++ b/wger/core/static/js/workout-manager.js
@@ -147,6 +147,43 @@ function setup_sortable() {
* Functions related to the user's preferences
*
*/
+function set_profile_field(field, newValue) {
+ /*
+ * Updates a single field in the user profile
+ */
+
+ var dataDict = new Object;
+ dataDict[field] = newValue;
+ $.get('/api/v2/userprofile', function(data) {
+ }).done(function(userprofile) {
+ console.log('Updating profile field "' + field + '" to value: ' + newValue);
+ $.ajax({
+ url:'/api/v2/userprofile/' + userprofile.results[0].id + '/',
+ type: 'PATCH',
+ data: dataDict
+ });
+ });
+}
+
+function get_profile_field(field) {
+ /*
+ * Get a single field from the user's profile
+ *
+ * Syncronous request, use sparingly!
+ */
+
+ var result;
+ $.ajax({
+ url:'/api/v2/userprofile/',
+ type: 'GET',
+ async: false,
+ success: function(userprofile) {
+ result = userprofile.results[0][field];
+ }
+ });
+ return result;
+}
+
function toggle_comments() {
$("#exercise-comments-toggle").click(function (e) {
e.preventDefault();
@@ -159,19 +196,32 @@ function toggle_comments() {
showComment = 0;
}
- // 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}
- });
- });
+ // Update user profile
+ set_profile_field('show_comments', showComment);
});
}
+function toggle_ro_access() {
+ $("#toggle-ro-access").click(function (e) {
+ e.preventDefault();
+ var ro_access = get_profile_field('ro_access');
+
+ // Update user profile
+ set_profile_field('ro_access', !ro_access);
+
+ // Hide and show appropriate divs
+ if(!ro_access) {
+ $('#shariffModal .shariff').removeClass('hidden');
+ $('#shariffModal .noRoAccess').addClass('hidden');
+ }
+ else {
+ $('#shariffModal .shariff').addClass('hidden');
+ $('#shariffModal .noRoAccess').removeClass('hidden');
+ }
+ });
+}
+
/*
* Init calls for tinyMCE editor
diff --git a/wger/core/templates/misc/fragments/shariff_modal.html b/wger/core/templates/misc/fragments/shariff_modal.html
index ca059451e..4b22edc5d 100644
--- a/wger/core/templates/misc/fragments/shariff_modal.html
+++ b/wger/core/templates/misc/fragments/shariff_modal.html
@@ -7,7 +7,19 @@
<h4 class="modal-title">{% trans "Share this page" %}</h4>
</div>
<div class="modal-body">
- <div class="shariff"></div>
+ <div class="shariff {% if is_owner and not owner_user.userprofile.ro_access %}hidden{% endif %}"></div>
+
+ {% if owner_user %}
+ <div class="noRoAccess {% if user.is_authenticated and is_owner and owner_user.userprofile.ro_access %}hidden{% endif %}">
+ <p>{% blocktrans %}Before you share links and on social media you need to allow external access.{% endblocktrans %}</p>
+ <p>
+ <a href="{% url 'core:user:preferences' %}" id="toggle-ro-access" class="btn btn-success btn-sm">
+ <span class="glyphicon glyphicon-ok"></span>
+ {% trans "That's done here" %}
+ </a>
+ </p>
+ </div>
+ {% endif %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
diff --git a/wger/core/templates/mobile/template.html b/wger/core/templates/mobile/template.html
index 0915892d6..3845cba36 100644
--- a/wger/core/templates/mobile/template.html
+++ b/wger/core/templates/mobile/template.html
@@ -67,6 +67,9 @@
// Init the modal dialog for editing forms
form_modal_dialog();
+
+ // Check for RO-access before allowing sharing
+ toggle_ro_access();
});
</script>
diff --git a/wger/core/templates/template.html b/wger/core/templates/template.html
index dafe9417e..ac7d605a8 100644
--- a/wger/core/templates/template.html
+++ b/wger/core/templates/template.html
@@ -67,13 +67,15 @@
custom_page_init();
}
-
// Init the modal dialog for editing forms
form_modal_dialog();
// Initialise the hook to reload the main-content
load_maincontent();
+ // Check for RO-access before allowing sharing
+ toggle_ro_access();
+
$('input[type="date"]').datepicker({
autoclose: true,
language: $('html').attr('lang')
diff --git a/wger/manager/templates/mobile/tags/render_day.html b/wger/manager/templates/mobile/tags/render_day.html
index 33545695f..2a26fb42d 100644
--- a/wger/manager/templates/mobile/tags/render_day.html
+++ b/wger/manager/templates/mobile/tags/render_day.html
@@ -175,7 +175,7 @@
{% if not editable %}
{% if exercise.comment_list %}
- <p class="text-muted">
+ <p class="text-muted exercise-comments">
{% for comment in exercise.comment_list %}
{{comment}}<br>
{% endfor %}
diff --git a/wger/manager/templates/tags/render_day.html b/wger/manager/templates/tags/render_day.html
index 301ecf2ae..a8d5e4b12 100644
--- a/wger/manager/templates/tags/render_day.html
+++ b/wger/manager/templates/tags/render_day.html
@@ -126,7 +126,7 @@ $(document).ready(function() {
{% endif %}
{% if exercise.comment_list %}
- <p class="text-muted">
+ <p class="text-muted exercise-comments">
{% for comment in exercise.comment_list %}
{{comment}}<br>
{% endfor %}
diff --git a/wger/manager/views/log.py b/wger/manager/views/log.py
index 2f20836f9..62e0fb05f 100644
--- a/wger/manager/views/log.py
+++ b/wger/manager/views/log.py
@@ -297,7 +297,7 @@ class WorkoutLogDetailView(DetailView, WgerPermissionMixin):
context['reps'] = _("Reps")
context['owner_user'] = self.owner_user
context['is_owner'] = is_owner
- context['show_shariff'] = is_owner and self.owner_user.userprofile.ro_access
+ context['show_shariff'] = is_owner
return context
diff --git a/wger/manager/views/workout.py b/wger/manager/views/workout.py
index f1b9b78a9..a1c1aa5db 100644
--- a/wger/manager/views/workout.py
+++ b/wger/manager/views/workout.py
@@ -102,7 +102,8 @@ def view(request, pk):
template_data['uid'] = uid
template_data['token'] = token
template_data['is_owner'] = is_owner
- template_data['show_shariff'] = is_owner and user.userprofile.ro_access
+ template_data['owner_user'] = user
+ template_data['show_shariff'] = is_owner
return render(request, 'workout/view.html', template_data)
diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.py
index 732b678f6..786c1534f 100644
--- a/wger/nutrition/views/plan.py
+++ b/wger/nutrition/views/plan.py
@@ -149,7 +149,7 @@ def view(request, id):
template_data['token'] = token
template_data['owner_user'] = user
template_data['is_owner'] = is_owner
- template_data['show_shariff'] = is_owner and user.userprofile.ro_access
+ template_data['show_shariff'] = is_owner
return render(request, 'plan/view.html', template_data)
diff --git a/wger/weight/views.py b/wger/weight/views.py
index d56516593..ceba6675c 100644
--- a/wger/weight/views.py
+++ b/wger/weight/views.py
@@ -142,7 +142,7 @@ def overview(request, username=None):
template_data['is_owner'] = is_owner
template_data['owner_user'] = user
- template_data['show_shariff'] = is_owner and user.userprofile.ro_access
+ template_data['show_shariff'] = is_owner
return render(request, 'weight_overview.html', template_data)