Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Some refactoring of methods and models for submitted ingredients/exercises

Roland Geideread1649ce823
diff --git a/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py b/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py
index bb2eee627..5821304e8 100644
--- a/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py
+++ b/wger/exercises/migrations/0002_auto__add_field_exercise_user__add_field_exercise_status.py
@@ -3,7 +3,7 @@ import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
-from wger.exercises.models import EXERCISE_STATUS_SYSTEM
+from wger.exercises.models import Exercise
class Migration(SchemaMigration):
@@ -20,7 +20,7 @@ class Migration(SchemaMigration):
keep_default=False)
# Set the status 'system' for all existing exercises in the database
if not db.dry_run:
- orm.Exercise.objects.all().update(status=EXERCISE_STATUS_SYSTEM)
+ orm.Exercise.objects.all().update(status=Exercise.EXERCISE_STATUS_SYSTEM)
# Adding field 'Exercise.creation_date'
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 7d0c4ba00..0ef4438f6 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -17,10 +17,13 @@
from django.db import models
from django.template.defaultfilters import slugify # django.utils.text.slugify in django 1.5!
-
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ugettext
from django.core.urlresolvers import reverse
+from django.core import mail
+
+from wger.utils.constants import EMAIL_FROM
class Language(models.Model):
@@ -99,30 +102,29 @@ class ExerciseCategory(models.Model):
return False
-EXERCISE_STATUS_PENDING = '1'
-EXERCISE_STATUS_ACCEPTED = '2'
-EXERCISE_STATUS_DECLINED = '3'
-EXERCISE_STATUS_ADMIN = '4'
-EXERCISE_STATUS_SYSTEM = '5'
-
-EXERCISE_STATUS = (
- (EXERCISE_STATUS_PENDING, _('Pending')),
- (EXERCISE_STATUS_ACCEPTED, _('Accepted')),
- (EXERCISE_STATUS_DECLINED, _('Declined')),
- (EXERCISE_STATUS_ADMIN, _('Submitted by administrator')),
- (EXERCISE_STATUS_SYSTEM, _('System exercise')),
-)
-
-EXERCISE_STATUS_OK = (EXERCISE_STATUS_ACCEPTED,
- EXERCISE_STATUS_ADMIN,
- EXERCISE_STATUS_SYSTEM)
-
-
class Exercise(models.Model):
'''
Model for an exercise
'''
+ EXERCISE_STATUS_PENDING = '1'
+ EXERCISE_STATUS_ACCEPTED = '2'
+ EXERCISE_STATUS_DECLINED = '3'
+ EXERCISE_STATUS_ADMIN = '4'
+ EXERCISE_STATUS_SYSTEM = '5'
+
+ EXERCISE_STATUS = (
+ (EXERCISE_STATUS_PENDING, _('Pending')),
+ (EXERCISE_STATUS_ACCEPTED, _('Accepted')),
+ (EXERCISE_STATUS_DECLINED, _('Declined')),
+ (EXERCISE_STATUS_ADMIN, _('Submitted by administrator')),
+ (EXERCISE_STATUS_SYSTEM, _('System exercise')),
+ )
+
+ EXERCISE_STATUS_OK = (EXERCISE_STATUS_ACCEPTED,
+ EXERCISE_STATUS_ADMIN,
+ EXERCISE_STATUS_SYSTEM)
+
# The user that submitted the exercise
user = models.ForeignKey(User, verbose_name=_('User'), null=True, blank=True)
@@ -165,18 +167,39 @@ class Exercise(models.Model):
return reverse('wger.exercises.views.exercises.view',
kwargs={'id': self.id, 'slug': slugify(self.name)})
- def __unicode__(self):
- '''
- Return a more human-readable representation
- '''
- return self.name
-
def get_owner_object(self):
'''
Exercise has no owner information
'''
return False
+ def send_email(self, request):
+ '''
+ Sends an email after being sucessfully added to the database (for user
+ submitted exercises only)
+ '''
+ if self.user and self.user.email:
+ url = request.build_absolute_uri(self.get_absolute_url())
+ subject = _('Exercise was sucessfully added to the general database')
+ message = (ugettext("Your exercise '{0}' was successfully added to the general"
+ "database.\n\n"
+ "It is now available on the exercise and muscle overview and can be\n"
+ "added to workouts. You can access it on this address:\n"
+ "{1}\n\n").format(self.name, url) +
+ ugettext("Thank you for contributing and making this site better!\n"
+ " the wger.de team"))
+ mail.send_mail(subject,
+ message,
+ EMAIL_FROM,
+ [self.user.email],
+ fail_silently=True)
+
+ def __unicode__(self):
+ '''
+ Return a more human-readable representation
+ '''
+ return self.name
+
class ExerciseComment(models.Model):
'''
diff --git a/wger/exercises/sitemap.py b/wger/exercises/sitemap.py
index 1da481a15..3faecacb9 100644
--- a/wger/exercises/sitemap.py
+++ b/wger/exercises/sitemap.py
@@ -16,7 +16,6 @@
from django.contrib.sitemaps import Sitemap
from wger.exercises.models import Exercise
-from wger.exercises.models import EXERCISE_STATUS_OK
from wger.utils.language import load_language
@@ -26,4 +25,4 @@ class ExercisesSitemap(Sitemap):
def items(self):
return (Exercise.objects.filter(category__language=load_language())
- .filter(status__in=EXERCISE_STATUS_OK))
+ .filter(status__in=Exercise.EXERCISE_STATUS_OK))
diff --git a/wger/exercises/templates/view.html b/wger/exercises/templates/view.html
index e70820375..269243984 100644
--- a/wger/exercises/templates/view.html
+++ b/wger/exercises/templates/view.html
@@ -30,15 +30,13 @@ function custom_modal_init()
-->
{% block content %}
-{% if exercise.status == EXERCISE_STATUS_PENDING %}
+{% if exercise.status == exercise.EXERCISE_STATUS_PENDING %}
<div class="alert">
<h3>{% trans "Exercise is pending review" %}</h3>
<p>{% blocktrans %}This user submitted exercise is pending a decision to be
included in the database. Till then, it will not be shown in the overview or
the search.{% endblocktrans %}
-{% if perms.exercises %}
-{% trans "Please select one of the options below." %}
-{% endif %}
+{% if perms.exercises %}{% trans "Please select one of the options below." %}{% endif %}
</p>
{% if perms.exercises %}
@@ -53,7 +51,7 @@ the search.{% endblocktrans %}
alt="{% trans 'Accept' %}">
{% trans "Accept" %}
</a>
- </div>
+ </div>
</div>
</div>
<div class="ym-g50 ym-gl">
@@ -66,16 +64,17 @@ the search.{% endblocktrans %}
alt="{% trans 'Decline' %}">
{% trans "Decline" %}
</a>
- </div>
+ </div>
</div>
</div>
</div>
-{% endif %}
+{% endif %} {# end check permissions #}
</div>
{% endif %} {# end exercise is pending review #}
+
<p>{% trans "Category" %}: <strong>{{ exercise.category.name }}</strong></p>
<p>{{ exercise.description|safe }}</p>
@@ -230,7 +229,7 @@ weight_log_chart({{json|safe}}, "{{svg_uuid}}", "{{reps}}")
</p>
<hr>
-{% if exercise.status == EXERCISE_STATUS_ACCEPTED %}
+{% if exercise.status == exercise.EXERCISE_STATUS_ACCEPTED %}
<div class="alert" style="margin-top:1em;">
<h3>{% trans "User submitted exercise" %}</h3>
{% with username=exercise.user.username %}
diff --git a/wger/exercises/tests/exercise.py b/wger/exercises/tests/exercise.py
index 0a8517540..0f4c2efaf 100644
--- a/wger/exercises/tests/exercise.py
+++ b/wger/exercises/tests/exercise.py
@@ -19,10 +19,6 @@ from django.core.urlresolvers import reverse
from wger.exercises.models import Exercise
from wger.exercises.models import ExerciseCategory
-from wger.exercises.models import EXERCISE_STATUS_PENDING
-from wger.exercises.models import EXERCISE_STATUS_ACCEPTED
-from wger.exercises.models import EXERCISE_STATUS_DECLINED
-from wger.exercises.models import EXERCISE_STATUS_ADMIN
from wger.manager.tests.testcase import WorkoutManagerTestCase
from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
@@ -251,10 +247,10 @@ class ExercisesTestCase(WorkoutManagerTestCase):
exercise = Exercise.objects.get(pk=exercise_id)
if admin:
self.assertEqual(exercise.user_id, 1)
- self.assertEqual(exercise.status, EXERCISE_STATUS_ADMIN)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_ADMIN)
else:
self.assertEqual(exercise.user_id, 2)
- self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_PENDING)
response = self.client.get(reverse('wger.exercises.views.exercises.view',
kwargs={'id': exercise_id}))
diff --git a/wger/exercises/tests/submitted_exercise.py b/wger/exercises/tests/submitted_exercise.py
index 73576eb5a..8cac3ad91 100644
--- a/wger/exercises/tests/submitted_exercise.py
+++ b/wger/exercises/tests/submitted_exercise.py
@@ -18,10 +18,6 @@ from django.core import mail
from django.core.urlresolvers import reverse
from wger.exercises.models import Exercise
-from wger.exercises.models import EXERCISE_STATUS_PENDING
-from wger.exercises.models import EXERCISE_STATUS_ACCEPTED
-from wger.exercises.models import EXERCISE_STATUS_DECLINED
-from wger.exercises.models import EXERCISE_STATUS_ADMIN
from wger.manager.tests.testcase import WorkoutManagerTestCase
from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
@@ -124,19 +120,19 @@ class ExerciseAcceptTestCase(WorkoutManagerTestCase):
Helper function
'''
exercise = Exercise.objects.get(pk=4)
- self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_PENDING)
response = self.client.get(reverse('exercise-accept', kwargs={'pk': 4}))
exercise = Exercise.objects.get(pk=4)
self.assertEqual(response.status_code, 302)
if not fail:
- self.assertEqual(exercise.status, EXERCISE_STATUS_ACCEPTED)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_ACCEPTED)
response = self.client.get(response['Location'])
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
else:
- self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_PENDING)
self.assertEqual(len(mail.outbox), 0)
def test_accept_admin(self):
@@ -173,18 +169,18 @@ class ExerciseRejectTestCase(WorkoutManagerTestCase):
Helper function
'''
exercise = Exercise.objects.get(pk=4)
- self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_PENDING)
response = self.client.get(reverse('exercise-decline', kwargs={'pk': 4}))
exercise = Exercise.objects.get(pk=4)
self.assertEqual(response.status_code, 302)
if not fail:
- self.assertEqual(exercise.status, EXERCISE_STATUS_DECLINED)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_DECLINED)
response = self.client.get(response['Location'])
self.assertEqual(response.status_code, 200)
else:
- self.assertEqual(exercise.status, EXERCISE_STATUS_PENDING)
+ self.assertEqual(exercise.status, Exercise.EXERCISE_STATUS_PENDING)
def test_reject_admin(self):
'''
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 96ffbafcc..16aa86c04 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -45,17 +45,10 @@ from wger.exercises.models import Exercise
from wger.exercises.models import ExerciseComment
from wger.exercises.models import ExerciseCategory
from wger.exercises.models import Muscle
-from wger.exercises.models import EXERCISE_STATUS_PENDING
-from wger.exercises.models import EXERCISE_STATUS_ACCEPTED
-from wger.exercises.models import EXERCISE_STATUS_DECLINED
-from wger.exercises.models import EXERCISE_STATUS_ADMIN
-from wger.exercises.models import EXERCISE_STATUS_SYSTEM
-from wger.exercises.models import EXERCISE_STATUS_OK
from wger.utils.generic_views import YamlFormMixin
from wger.utils.generic_views import YamlDeleteMixin
from wger.utils.language import load_language
-from wger.utils.constants import EMAIL_FROM
logger = logging.getLogger('workout_manager.custom')
@@ -71,7 +64,7 @@ def overview(request):
template_data.update(csrf(request))
categories = (ExerciseCategory.objects.filter(language=language.id)
- .filter(exercise__status__in=EXERCISE_STATUS_OK)
+ .filter(exercise__status__in=Exercise.EXERCISE_STATUS_OK)
.distinct())
template_data['categories'] = categories
@@ -87,8 +80,6 @@ def view(request, id, slug=None):
template_data = {}
template_data['comment_edit'] = False
- template_data['EXERCISE_STATUS_PENDING'] = EXERCISE_STATUS_PENDING
- template_data['EXERCISE_STATUS_ACCEPTED'] = EXERCISE_STATUS_ACCEPTED
# Load the exercise itself
exercise = get_object_or_404(Exercise, pk=id)
@@ -204,7 +195,7 @@ class ExerciseAddView(ExercisesEditAddView, CreateView):
# set the submitter, if admin, set approrpiate status
form.instance.user = self.request.user
if self.request.user.has_perm('exercises.add_exercise'):
- form.instance.status = EXERCISE_STATUS_ADMIN
+ form.instance.status = Exercise.EXERCISE_STATUS_ADMIN
else:
subject = _('New user submitted exercise')
message = _('''The user {0} submitted a new exercise "{1}".'''.format(
@@ -258,36 +249,20 @@ class PendingExerciseListView(ListView):
'''
Only show pending exercises
'''
- return Exercise.objects.filter(status=EXERCISE_STATUS_PENDING)
+ return Exercise.objects.filter(status=Exercise.EXERCISE_STATUS_PENDING)
@permission_required('exercise.add_exercise')
def accept(request, pk):
'''
- Accepts a pending user submitted exercise
+ Accepts a pending user submitted exercise and emails the user, if possible
'''
exercise = get_object_or_404(Exercise, pk=pk)
- exercise.status = EXERCISE_STATUS_ACCEPTED
+ exercise.status = Exercise.EXERCISE_STATUS_ACCEPTED
exercise.save()
+ exercise.send_email(request)
messages.success(request, _('Exercise was sucessfully added to the general database'))
- # Notify the user if possible
- if exercise.user.email:
- url = request.build_absolute_uri(exercise.get_absolute_url())
- subject = _('Exercise was sucessfully added to the general database')
- message = _("Your exercise '{0}' was successfully added to the general database.\n"
- "\n"
- "It is now available on the exercise and muscle overview and can be\n"
- "added to workouts. You can access it on this address:\n"
- "{1}\n"
- "\n"
- "Thank you for contributing and making this site better!\n"
- " the wger.de team".format(exercise.name, url))
- mail.send_mail(subject,
- message,
- EMAIL_FROM,
- [exercise.user.email],
- fail_silently=True)
return HttpResponseRedirect(exercise.get_absolute_url())
@@ -297,7 +272,7 @@ def decline(request, pk):
Declines and deletes a pending user submitted exercise
'''
exercise = get_object_or_404(Exercise, pk=pk)
- exercise.status = EXERCISE_STATUS_DECLINED
+ exercise.status = Exercise.EXERCISE_STATUS_DECLINED
exercise.save()
messages.success(request, _('Exercise was sucessfully marked as rejected'))
return HttpResponseRedirect(exercise.get_absolute_url())
@@ -313,7 +288,7 @@ def search(request):
user_language = load_language()
exercises = (Exercise.objects.filter(name__icontains=q)
.filter(category__language_id=user_language)
- .filter(status__in=EXERCISE_STATUS_OK)
+ .filter(status__in=Exercise.EXERCISE_STATUS_OK)
.order_by('category__name', 'name')
.distinct())
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index 3f1b85a44..24dda746b 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -24,11 +24,13 @@ from django.template.defaultfilters import slugify # django.utils.text.slugify
from django.core.validators import MaxValueValidator
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
+from django.core import mail
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ugettext
from wger.exercises.models import Language
-
+from wger.utils.constants import EMAIL_FROM
MEALITEM_WEIGHT_GRAM = '1'
MEALITEM_WEIGHT_UNIT = '2'
@@ -255,6 +257,27 @@ class Ingredient(models.Model):
return True
logger.debug('equal, not doing anything')
+ def send_email(self, request):
+ '''
+ Sends an email after being sucessfully added to the database (for user
+ submitted ingredients only)
+ '''
+ if self.user and self.user.email:
+ url = request.build_absolute_uri(self.get_absolute_url())
+ subject = _('Ingredient was sucessfully added to the general database')
+ message = (ugettext("Your ingredient '{0}' was successfully added to the general "
+ "database.\n\n"
+ "It is now available on the exercise and muscle overview and can be\n"
+ "added to workouts. You can access it on this address:\n"
+ "{1}\n\n").format(self.name, url) +
+ ugettext("Thank you for contributing and making this site better!\n"
+ " the wger.de team"))
+ mail.send_mail(subject,
+ message,
+ EMAIL_FROM,
+ [self.user.email],
+ fail_silently=True)
+
def get_owner_object(self):
'''
Ingredient has no owner information
diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.html
index 5c6e20209..ba41eacf3 100644
--- a/wger/nutrition/templates/ingredient/view.html
+++ b/wger/nutrition/templates/ingredient/view.html
@@ -56,15 +56,13 @@ $(document).ready(function() {
{% block content %}
-{% if ingredient.status == INGREDIENT_STATUS_PENDING %}
+{% if ingredient.status == ingredient.INGREDIENT_STATUS_PENDING %}
<div class="alert">
<h3>{% trans "Ingredient is pending review" %}</h3>
<p>{% blocktrans %}This user submitted ingredient is pending a decision to be
included in the database. Till then, it will not be shown in the overview or
the search.{% endblocktrans %}
-{% if perms.exercises %}
-{% trans "Please select one of the options below." %}
-{% endif %}
+{% if perms.exercises %}{% trans "Please select one of the options below." %}{% endif %}
</p>
{% if perms.exercises %}
@@ -79,7 +77,7 @@ the search.{% endblocktrans %}
alt="{% trans 'Accept' %}">
{% trans "Accept" %}
</a>
- </div>
+ </div>
</div>
</div>
<div class="ym-g50 ym-gl">
@@ -92,14 +90,13 @@ the search.{% endblocktrans %}
alt="{% trans 'Decline' %}">
{% trans "Decline" %}
</a>
- </div>
+ </div>
</div>
</div>
</div>
-{% endif %}
+{% endif %} {# end check permissions #}
</div>
-
{% endif %} {# end ingredient is pending review #}
@@ -227,7 +224,7 @@ the search.{% endblocktrans %}
{% block sidebar %}
-{% if ingredient.status == INGREDIENT_STATUS_ACCEPTED %}
+{% if ingredient.status == ingredient.INGREDIENT_STATUS_ACCEPTED %}
<div class="alert" style="margin-top:1em;">
<h3>{% trans "User submitted ingredient" %}</h3>
{% with username=ingredient.user.username %}
diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.py
index 9caf650a6..7ec4ef94f 100644
--- a/wger/nutrition/views/ingredient.py
+++ b/wger/nutrition/views/ingredient.py
@@ -98,8 +98,6 @@ def view(request, id, slug=None):
ingredient = get_object_or_404(Ingredient, pk=id)
template_data['ingredient'] = ingredient
- template_data['INGREDIENT_STATUS_PENDING'] = Ingredient.INGREDIENT_STATUS_PENDING
- template_data['INGREDIENT_STATUS_ACCEPTED'] = Ingredient.INGREDIENT_STATUS_ACCEPTED
template_data['form'] = UnitChooserForm(data={'ingredient_id': ingredient.id,
'amount': 100,
'unit': None})
@@ -216,26 +214,9 @@ def accept(request, pk):
ingredient = get_object_or_404(Ingredient, pk=pk)
ingredient.status = Ingredient.INGREDIENT_STATUS_ACCEPTED
ingredient.save()
+ ingredient.send_email(request)
messages.success(request, _('Ingredient was sucessfully added to the general database'))
- # Notify the user if possible
- if ingredient.user.email:
- url = request.build_absolute_uri(ingredient.get_absolute_url())
- subject = _('Ingredient was sucessfully added to the general database')
- message = _("Your ingredient '{0}' was successfully added to the general database.\n"
- "\n"
- "It is now available on the ingredient and muscle overview and can be\n"
- "added to nutritional plans. You can access it on this address:\n"
- "{1}\n"
- "\n"
- "Thank you for contributing and making this site better!\n"
- " the wger.de team".format(ingredient.name, url))
- mail.send_mail(subject,
- message,
- EMAIL_FROM,
- [ingredient.user.email],
- fail_silently=True)
-
return HttpResponseRedirect(ingredient.get_absolute_url())