Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add link to create a demo user

Roland Geider545eb63d079d
diff --git a/manager/admin.py b/manager/admin.py
index 23e2805dd..f1b4cfbf8 100644
--- a/manager/admin.py
+++ b/manager/admin.py
@@ -1,19 +1,19 @@
# This file is part of Workout Manager.
-#
+#
# Workout Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
-#
+#
# Workout Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
-#
+#
# You should have received a copy of the GNU Affero General Public License
-from manager.models import Day, TrainingSchedule, Set, Setting, WorkoutLog
+from manager import models as manager_models
from django.contrib import admin
@@ -21,10 +21,11 @@ from django.contrib import admin
-admin.site.register(TrainingSchedule)
-admin.site.register(Set)
-admin.site.register(Day)
-admin.site.register(WorkoutLog)
+admin.site.register(manager_models.TrainingSchedule)
+admin.site.register(manager_models.Set)
+admin.site.register(manager_models.Day)
+admin.site.register(manager_models.WorkoutLog)
+admin.site.register(manager_models.UserProfile)
-admin.site.register(Setting)
+admin.site.register(manager_models.Setting)
diff --git a/manager/forms.py b/manager/forms.py
index e2791cc26..bb4d8aeb3 100644
--- a/manager/forms.py
+++ b/manager/forms.py
@@ -47,7 +47,7 @@ from workout_manager.constants import DATE_FORMATS
class UserPreferencesForm(ModelForm):
class Meta:
model = UserProfile
- exclude = ('user',)
+ exclude = ('user', 'is_temporary')
class UserEmailForm(ModelForm):
@@ -74,6 +74,11 @@ class RegistrationForm(UserCreationForm, UserEmailForm):
help_text=_('As a security measure, please enter the previous words'),)
+class DemoUserForm(Form):
+ captcha = ReCaptchaField(attrs={'theme': 'clean'},
+ label=_('Confirmation text'),
+ help_text=_('As a security measure, please enter the previous words'),)
+
class WorkoutForm(ModelForm):
class Meta:
model = TrainingSchedule
diff --git a/manager/management/__init__.py b/manager/management/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/manager/management/commands/__init__.py b/manager/management/commands/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/manager/management/commands/delete-temp-users.py b/manager/management/commands/delete-temp-users.py
new file mode 100644
index 000000000..af7d6d58d
--- /dev/null
+++ b/manager/management/commands/delete-temp-users.py
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 *-*
+
+# This file is part of Workout Manager.
+#
+# Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import datetime
+
+from django.utils.timezone import now
+from django.core.management.base import BaseCommand
+from manager.models import UserProfile
+
+
+class Command(BaseCommand):
+ '''
+ Helper admin command to clean up demo users, to be called e.g. by cron
+ '''
+
+ help = 'Deletes all temporary users older than 1 week'
+
+ def handle(self, *args, **options):
+
+ user_list = UserProfile.objects.filter(is_temporary=True)
+ for profile in user_list:
+ delta = now() - profile.user.date_joined
+
+ if (delta >= datetime.timedelta(7)):
+ self.stdout.write("Deleting user %s, joined %s days ago\n" % (profile,
+ delta.days))
+ profile.user.delete()
diff --git a/manager/models.py b/manager/models.py
index 763c9b49d..55e5974f8 100644
--- a/manager/models.py
+++ b/manager/models.py
@@ -199,12 +199,12 @@ class WorkoutLog(models.Model):
Processes and regroups a list of log entries so they can be rendered
and passed to the D3 library to render a chart
'''
-
+
reps = []
entry_log = SortedDict()
chart_data = []
max_weight = {}
-
+
# Group by date
for entry in logs:
if entry.reps not in reps:
@@ -220,7 +220,7 @@ class WorkoutLog(models.Model):
# higher weight is shown in the chart
if not max_weight.get(entry.date):
max_weight[entry.date] = {entry.reps: entry.weight}
-
+
if not max_weight[entry.date].get(entry.reps):
max_weight[entry.date][entry.reps] = entry.weight
@@ -231,17 +231,17 @@ class WorkoutLog(models.Model):
for entry in logs:
temp = {'date': '%s' % entry.date,
'id': 'workout-log-%s' % entry.id}
-
+
for rep in reps:
if entry.reps == rep:
-
+
# Only add if entry is the maximum for the day
- if entry.weight == max_weight[entry.date][entry.reps]:
+ if entry.weight == max_weight[entry.date][entry.reps]:
temp[rep] = entry.weight
else:
temp[rep] = 0
chart_data.append(temp)
-
+
return (entry_log, json.dumps(chart_data, cls=DecimalJsonEncoder))
@@ -249,6 +249,9 @@ class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
+ # Flag to mark a temporary user (demo account)
+ is_temporary = models.BooleanField(default=False)
+
#
# User preferences
#
@@ -265,6 +268,12 @@ a nutritional plan. These ingredients are extracted from a list provided
by the US Department of Agriculture. It is extremely complete, with around
7000 entries, but can be somewhat overwhelming and make the search difficult.'''))
+ def __unicode__(self):
+ '''
+ Return a more human-readable representation
+ '''
+ return "Profile for user %s" % (self.user)
+
# Every new user gets a profile
def create_user_profile(sender, instance, created, **kwargs):
diff --git a/manager/static/js/workout-manager.js b/manager/static/js/workout-manager.js
index ff6160764..e6836ed49 100644
--- a/manager/static/js/workout-manager.js
+++ b/manager/static/js/workout-manager.js
@@ -466,8 +466,8 @@ function weight_chart(data)
var minDate = getDate(data[0].x),
maxDate = getDate(data[data.length-1].x);
- var margin = {top: 10, right: 10, bottom: 150, left: 30},
- margin2 = {top: 290, right: 10, bottom: 50, left: 30},
+ var margin = {top: 10, right: 10, bottom: 150, left: 40},
+ margin2 = {top: 290, right: 10, bottom: 50, left: 40},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
height2 = 390 - margin2.top - margin2.bottom;
diff --git a/manager/templates/template.html b/manager/templates/template.html
index efdcd1e2f..a6c023828 100644
--- a/manager/templates/template.html
+++ b/manager/templates/template.html
@@ -158,6 +158,11 @@
<div class="ym-wbox" id="content-wrapper">
<section class="ym-grid linearize-level-1">
<div id="current-url" data-current-url="{{request.get_full_path}}"></div>
+ {% if user.get_profile.is_temporary %}
+ <div class="box info" style="padding:0.6em; margin: 0em;">
+ {% trans "You are using a temporary account, this user will be deleted after some time."%}
+ </div>
+ {% endif %}
{% block template %}{% endblock %}
</section>
</div>
diff --git a/manager/templates/user/login.html b/manager/templates/user/login.html
index 9cc9428cd..9ba54cbff 100644
--- a/manager/templates/user/login.html
+++ b/manager/templates/user/login.html
@@ -73,6 +73,15 @@
{% trans "Register" %}</a>
</p>
+<p>
+ <a href="{% url demo-account %}">
+ <img src="{% static 'images/icons/register.svg' %}"
+ width="48"
+ height="48"
+ alt="{% trans 'Demo access' %}">
+ {% trans "Demo access" %}</a>
+</p>
+
<h3>{% trans "Forgot password?" %}</h3>
<p>
<a href="{% url django.contrib.auth.views.password_reset %}">
diff --git a/manager/urls.py b/manager/urls.py
index 227767073..785d4963d 100644
--- a/manager/urls.py
+++ b/manager/urls.py
@@ -2,6 +2,8 @@ from django.conf.urls import patterns, url
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse_lazy
+from django.views.generic import TemplateView
+
from manager.views import WorkoutEditView
from manager.views import WorkoutDeleteView
from manager.views import WorkoutLogDetailView
@@ -21,6 +23,10 @@ urlpatterns = patterns('manager.views',
# User
url(r'^logout$', 'logout', name='logout'),
url(r'^user/registration$', 'registration'),
+ url(r'^user/demo-account$',
+ 'create_demo_user',
+ name='demo-account'),
+
url(r'^user/preferences$', 'preferences'),
# Workout
diff --git a/manager/views.py b/manager/views.py
index 4750fff52..eb59e9c4e 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -17,6 +17,7 @@
import logging
import uuid
import datetime
+import random
from django.template import RequestContext
@@ -71,6 +72,9 @@ from manager.forms import WorkoutCopyForm
from manager.forms import SetForm
from manager.forms import HelperDateForm
from manager.forms import WorkoutLogForm
+from manager.forms import DemoUserForm
+
+from manager.utils import load_language
from workout_manager.constants import WORKOUT_TAB
from workout_manager.constants import USER_TAB
@@ -199,6 +203,95 @@ def registration(request):
context_instance=RequestContext(request))
+def create_demo_user(request):
+ '''
+ Creates a demo user and adds some initial data
+ '''
+
+ template_data = {}
+ template_data.update(csrf(request))
+ template_data['active_tab'] = USER_TAB
+
+ if request.method == 'POST':
+ form = DemoUserForm(data=request.POST)
+
+ # If the data is valid, create a user, log in and redirect
+ if form.is_valid():
+ username = uuid.uuid4().hex[:-2]
+ password = uuid.uuid4().hex[:-2]
+ email = ''
+ user = Django_User.objects.create_user(username,
+ email,
+ password)
+ user.save()
+ user_profile = user.get_profile()
+ user_profile.is_temporary = True
+ user_profile.save()
+
+ #
+ # Create some initial data
+ #
+
+ # Workout and exercises
+ workout = TrainingSchedule(user=user,
+ comment=_('Sample workout'))
+ workout.save()
+ monday = DaysOfWeek.objects.get(pk=1)
+ day = Day(training=workout,
+ description=_('Sample day'))
+ day.save()
+ day.day.add(monday)
+
+ # Biceps curls with dumbbell
+ if(load_language().short_name == 'de'):
+ exercise = Exercise.objects.get(pk=26)
+ else:
+ exercise = Exercise.objects.get(pk=81)
+ day_set = Set(exerciseday=day,
+ sets=4)
+ day_set.save()
+ day_set.exercises.add(exercise)
+
+ setting = Setting(set=day_set,
+ exercise=exercise,
+ reps=8,
+ order=1)
+ setting.save()
+
+ # Weight log entries
+ for reps in (7, 8, 9, 10):
+ for i in range(1, 8):
+ log = WorkoutLog(user=user,
+ exercise=exercise,
+ workout=workout,
+ reps=reps,
+ weight=30 - reps + random.randint(1, 5),
+ date=datetime.date.today() - datetime.timedelta(weeks=i))
+ log.save()
+
+ # Weight entries
+ for i in range(1, 20):
+ entry = WeightEntry(user=user,
+ weight=80 + 0.5*i + random.randint(1, 3),
+ creation_date=datetime.date.today() - datetime.timedelta(days=i))
+ entry.save()
+
+
+
+ user = authenticate(username=username, password=password)
+ django_login(request, user)
+ return HttpResponseRedirect(reverse('index'))
+ else:
+ form = DemoUserForm()
+
+ template_data['form'] = form
+ template_data['submit_text'] = _('Register')
+
+ return render_to_response('user/demo.html',
+ template_data,
+ context_instance=RequestContext(request))
+
+
def preferences(request):
"""An overview of all user preferences
"""