Self hosted FLOSS fitness/workout, nutrition and weight tracker
Render exercise thumbnails with easy_thumbnails
Roland Geider8b00c94bad31
diff --git a/requirements.txt b/requirements.txtindex 08c660efa..f6e73d363 100644--- a/requirements.txt+++ b/requirements.txt@@ -10,3 +10,4 @@ south < 2.0django-tastypiepython-mimeparsepillow+easy-thumbnailsdiff --git a/setup.py b/setup.pyindex b1aaa95fc..2a4d2bb82 100644--- a/setup.py+++ b/setup.py@@ -54,7 +54,8 @@ setup('south < 2.0','django-tastypie','python-mimeparse',- 'pillow'+ 'pillow',+ 'easy-thumbnails',],entry_points={'console_scripts': [diff --git a/wger/exercises/models.py b/wger/exercises/models.pyindex 3d8209014..c17bc5c81 100644--- a/wger/exercises/models.py+++ b/wger/exercises/models.py@@ -15,6 +15,8 @@# 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/>.+import logging+from django.db import modelsfrom django.template.defaultfilters import slugify # django.utils.text.slugify in django 1.5!from django.contrib.auth.models import User@@ -23,12 +25,20 @@ from django.utils.translation import ugettextfrom django.core.urlresolvers import reversefrom django.core import mailfrom django.core.cache import cache+from django.db.models.signals import pre_save+from django.db.models.signals import pre_delete+from django.db.models.signals import post_delete++from easy_thumbnails.files import get_thumbnailerfrom wger.utils.constants import EMAIL_FROMfrom wger.utils.cache import delete_template_fragment_cachefrom wger.utils.cache import cache_mapper+logger = logging.getLogger('wger.custom')++class Language(models.Model):'''Language of an item (exercise, workout, etc.)@@ -292,6 +302,48 @@ class ExerciseImage(models.Model):upload_to=exercise_image_upload_dir)'''Uploaded image'''+ def get_owner_object(self):+ '''+ Image has no owner information+ '''+ return False+++def delete_exercise_image_on_delete(sender, instance, **kwargs):+ '''+ Delete the image, along with its thumbnails, from the disk+ '''++ thumbnailer = get_thumbnailer(instance.image)+ thumbnailer.delete_thumbnails()+ instance.image.delete(save=False)+++post_delete.connect(delete_exercise_image_on_delete, sender=ExerciseImage)+++def delete_exercise_image_on_update(sender, instance, **kwargs):+ '''+ Delete the corresponding image from the filesystem when the an ExerciseImage+ object was changed+ '''+ if not instance.pk:+ return False++ try:+ old_file = ExerciseImage.objects.get(pk=instance.pk).image+ except ExerciseImage.DoesNotExist:+ return False++ new_file = instance.image+ if not old_file == new_file:+ thumbnailer = get_thumbnailer(instance.image)+ thumbnailer.delete_thumbnails()+ instance.image.delete(save=False)+++pre_save.connect(delete_exercise_image_on_update, sender=ExerciseImage)+class ExerciseComment(models.Model):'''diff --git a/wger/exercises/templates/mobile/view.html b/wger/exercises/templates/mobile/view.htmlindex 99a35c735..ea19dddd0 100644--- a/wger/exercises/templates/mobile/view.html+++ b/wger/exercises/templates/mobile/view.html@@ -3,6 +3,7 @@{% load i18n %}{% load staticfiles %}{% load wger_extras %}+{% load thumbnail %}{% load cache %}{% block header %}@@ -111,6 +112,36 @@ included in the database.{% endblocktrans %}{% endif %}+{% with images=exercise.exerciseimage_set.all %}+{% if images %}+ <h4>{% trans "Images" %}</h4>++ <div style="width:100%; overflow-x: scroll;">+ {% for image in images %}+ <img src="{{ image.image|thumbnail_url:'medium' }}"+ width="200"+ height="200"+ class="float-left">+ {% endfor %}+ </div>++{% endif %}+{% endwith %}++{% if perms.exercises %}+<p>+ <a href="{% url 'exerciseimage-add' exercise.id %}"+ title="{% trans 'Add' %}"+ data-ajax="false">+ <img src="{% static 'images/icons/add.svg' %}"+ width="16"+ height="16"+ alt="{% trans 'Add new image' %}">+ {% trans "Add new image" %}</a>+</p>+{% endif %}++{% with muscles=exercise.muscles.all %}{% with muscles_secondary=exercise.muscles_secondary.all %}diff --git a/wger/exercises/templates/view.html b/wger/exercises/templates/view.htmlindex db1456dfe..d9cec94c1 100644--- a/wger/exercises/templates/view.html+++ b/wger/exercises/templates/view.html@@ -3,6 +3,7 @@{% load i18n %}{% load staticfiles %}{% load wger_extras %}+{% load thumbnail %}{% load cache %}{% block header %}@@ -18,6 +19,91 @@ function custom_modal_init()init_weight_log_datepicker();}</script>+<style type="text/css">+ .wrap {+ overflow: hidden;+ margin: 10px;+ }+ .image-box {+ float: left;+ position: relative;+ width: 20%;+ padding-bottom: 20%;+ }+ .boxInner {+ position: absolute;+ left: 10px;+ right: 10px;+ top: 10px;+ bottom: 10px;+ overflow: hidden;+ }+ .boxInner img.gallery-image {+ width: 100%;+ }+ .boxInner .titleBox {+ position: absolute;+ bottom: 0;+ left: 0;+ right: 0;+ margin-bottom: -50px;+ background: #000;+ background: rgba(0, 0, 0, 0.8);+ color: #FFF;+ padding: 10px;+ text-align: center;+ -webkit-transition: all 0.3s ease-out;+ -moz-transition: all 0.3s ease-out;+ -o-transition: all 0.3s ease-out;+ transition: all 0.3s ease-out;+ }+ body .boxInner:hover .titleBox, body.touch .boxInner.touchFocus .titleBox {+ margin-bottom: 0;+ }+ @media only screen and (max-width : 480px) {+ /* Smartphone view: 1 tile */+ .image-box {+ width: 100%;+ padding-bottom: 100%;+ }+ }+ @media only screen and (max-width : 650px) and (min-width : 481px) {+ /* Tablet view: 2 tiles */+ .image-box {+ width: 50%;+ padding-bottom: 50%;+ }+ }+ @media only screen and (max-width : 1050px) and (min-width : 651px) {+ /* Small desktop / ipad view: 3 tiles */+ .image-box {+ width: 33.3%;+ padding-bottom: 33.3%;+ }+ }+ @media only screen and (max-width : 1290px) and (min-width : 1051px) {+ /* Medium desktop: 4 tiles */+ .image-box {+ /*+ width: 25%;+ padding-bottom: 25%;+ */+ width: 33.3%;+ padding-bottom: 33.3%;+ }+ }+ @media only screen and (min-width : 1290px) {+ /* Medium desktop: 4 tiles */+ .image-box {+ /*+ width: 25%;+ padding-bottom: 25%;+ */+ width: 33.3%;+ padding-bottom: 33.3%;+ }+ }+ </style>{% endblock %}<!--Title@@ -126,18 +212,38 @@ included in the database.{% endblocktrans %}{% endif %}--{% with images=exercise.exerciseimage_set.all %}{% if images %}-<h4>{% trans "Images" %}</h4>+ <h4>{% trans "Images" %}</h4>+ <div class="wrap">{% for image in images %}- <img src="{{ image.image.url }}"- width="{{ image.image.height_field }}"- height="{{ image.image.height_field }}"- class="flexible">+ <div class="image-box">+ <div class="boxInner">+ <img src="{{ image.image|thumbnail_url:'medium_cropped' }}"+ width="200"+ height="200"+ class="gallery-image">+ {% if perms.exercises.change_exerciseimage %}+ <div class="titleBox">+ <a href="{% url 'exerciseimage-delete' exercise.pk image.pk %}"+ class="modal-dialog no-hover">+ <img src="{% static 'images/icons/trash.svg' %}"+ width="24"+ height="24"+ alt="{% trans 'Delete' %}"></a>+ <a href="{% url 'exerciseimage-edit' image.pk %}"+ class="no-hover">+ <img src="{% static 'images/icons/edit.svg' %}"+ width="24"+ height="24"+ alt="{% trans 'Edit' %}"></a>+ </div>+ {% endif %}+ </div>+ </div>{% endfor %}+ </div>{% endif %}{% endwith %}@@ -291,7 +397,6 @@ weight_log_chart({{json|safe}}, "{{svg_uuid}}", "{{reps}}"){% trans "Delete" %}</a></p>-<hr>{% endif %}{% cc_by_sa_sidebar language %}diff --git a/wger/exercises/urls.py b/wger/exercises/urls.pyindex 4c326c14d..8aee1eb8f 100644--- a/wger/exercises/urls.py+++ b/wger/exercises/urls.py@@ -83,7 +83,7 @@ urlpatterns = patterns('wger.exercises.views',url(r'^image/(?P<pk>\d+)/edit$',images.ExerciseImageEditView.as_view(),name='exerciseimage-edit'),- url(r'^image/(?P<id>\d+)/delete$',+ url(r'^(?P<exercise_pk>\d+)/image/(?P<pk>\d+)/delete$',images.ExerciseImageDeleteView.as_view(),name='exerciseimage-delete'),diff --git a/wger/exercises/views/comments.py b/wger/exercises/views/comments.pyindex 5cb70ea62..625a3f792 100644--- a/wger/exercises/views/comments.py+++ b/wger/exercises/views/comments.py@@ -49,7 +49,7 @@ class ExerciseCommentEditView(WgerFormMixin, UpdateView, WgerPermissionMixin):permission_required = 'exercises.change_exercisecomment'def get_success_url(self):- return reverse('wger.exercises.views.exercises.view',+ return reverse('exercise-view',kwargs={'id': self.object.exercise.id})# Send some additional data to the templatediff --git a/wger/exercises/views/images.py b/wger/exercises/views/images.pyindex 29ec8eb43..1f57e7dcc 100644--- a/wger/exercises/views/images.py+++ b/wger/exercises/views/images.py@@ -95,15 +95,26 @@ class ExerciseImageDeleteView(WgerDeleteMixin, DeleteView, WgerPermissionMixin):'''model = ExerciseImage- success_url = reverse_lazy('wger.exercises.views.exercises.overview')messages = ugettext_lazy('Image successfully deleted')permission_required = 'exercises.delete_exerciseimage'- # Send some additional data to the template+ def get_success_url(self):+ '''+ Return to exercise image+ '''+ return reverse('exercise-view',+ kwargs={'id': self.kwargs['exercise_pk']})+def get_context_data(self, **kwargs):+ '''+ Send some additional data to the template+ '''+ pk = self.kwargs['pk']+ exercise_pk = self.kwargs['exercise_pk']context = super(ExerciseImageDeleteView, self).get_context_data(**kwargs)- context['title'] = _('Delete image?')- context['form_action'] = reverse('exerciseimage-delete', kwargs={'pk': self.kwargs['pk']})+ context['title'] = _('Delete exercise image?')+ context['form_action'] = reverse('exerciseimage-delete',+ kwargs={'pk': pk, 'exercise_pk': exercise_pk})return contextdiff --git a/wger/manager/templates/mobile/form.html b/wger/manager/templates/mobile/form.htmlindex 28c33b57a..03e80b01e 100644--- a/wger/manager/templates/mobile/form.html+++ b/wger/manager/templates/mobile/form.html@@ -26,6 +26,7 @@-->{% block content %}<form action="{{form_action}}"+ enctype="multipart/form-data"method="post"class="ym-form">{% csrf_token %}diff --git a/wger/settings_global.py b/wger/settings_global.pyindex bf17c0f24..a7811c404 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -217,6 +217,9 @@ INSTALLED_APPS = (# REST-API'tastypie',++ # thumbnails+ 'easy_thumbnails')@@ -297,3 +300,18 @@ CACHES = {'TIMEOUT': 30 * 24 * 60 * 60, # Cache for a month}}+++# Thumbnail sizes+THUMBNAIL_ALIASES = {+ '': {+ 'thumbnail': {'size': (80, 80)},+ 'thumbnail_cropped': {'size': (80, 80), 'crop': 'smart'},++ 'medium': {'size': (200, 200)},+ 'medium_cropped': {'size': (200, 200), 'crop': 'smart'},++ 'larger': {'size': (400, 400)},+ 'larger_cropped': {'size': (400, 400), 'crop': 'smart'},+ },+}