Self hosted FLOSS fitness/workout, nutrition and weight tracker
Start working on gallery app
Roland Geiderb646eff4591b
diff --git a/wger/gallery/__init__.py b/wger/gallery/__init__.pynew file mode 100644index 000000000..ca2d162cb--- /dev/null+++ b/wger/gallery/__init__.py@@ -0,0 +1,24 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+++# wger+from wger import get_version+++VERSION = get_version()+default_app_config = 'wger.gallery.apps.GalleryConfig'diff --git a/wger/gallery/apps.py b/wger/gallery/apps.pynew file mode 100644index 000000000..a91290a7e--- /dev/null+++ b/wger/gallery/apps.py@@ -0,0 +1,5 @@+from django.apps import AppConfig++class GalleryConfig(AppConfig):+ name = 'wger.gallery'+ verbose_name = "Gallery"diff --git a/wger/gallery/forms.py b/wger/gallery/forms.pynew file mode 100644index 000000000..7d72b07e1--- /dev/null+++ b/wger/gallery/forms.py@@ -0,0 +1,21 @@+from django.forms import ModelForm, DateField++from wger.gallery.models import Image+from wger.utils.constants import DATE_FORMATS+from wger.utils.widgets import Html5DateInput+from django.forms import (+ DateField,+ ModelForm,+ widgets+)+++class ImageForm(ModelForm):+ date = DateField(input_formats=DATE_FORMATS, widget=Html5DateInput())++ class Meta:+ model = Image+ exclude = []+ widgets = {+ 'user': widgets.HiddenInput(),+ }diff --git a/wger/gallery/migrations/0001_initial.py b/wger/gallery/migrations/0001_initial.pynew file mode 100644index 000000000..dc0b3d631--- /dev/null+++ b/wger/gallery/migrations/0001_initial.py@@ -0,0 +1,32 @@+# Generated by Django 3.1.8 on 2021-05-01 02:11++import datetime+from django.conf import settings+from django.db import migrations, models+import django.db.models.deletion+import wger.gallery.models.image+++class Migration(migrations.Migration):++ initial = True++ dependencies = [+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name='Image',+ fields=[+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),+ ('date', models.DateField(default=datetime.datetime.now, verbose_name='Date')),+ ('image', models.ImageField(help_text='Only PNG and JPEG formats are supported', upload_to=wger.gallery.models.image.gallery_image_upload_dir, verbose_name='Image')),+ ('description', models.TextField(blank=True, max_length=1000, verbose_name='Description')),+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='User')),+ ],+ options={+ 'ordering': ['-date'],+ },+ ),+ ]diff --git a/wger/gallery/migrations/__init__.py b/wger/gallery/migrations/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/gallery/models/__init__.py b/wger/gallery/models/__init__.pynew file mode 100644index 000000000..a0c1ffb9c--- /dev/null+++ b/wger/gallery/models/__init__.py@@ -0,0 +1 @@+from .image import Imagediff --git a/wger/gallery/models/image.py b/wger/gallery/models/image.pynew file mode 100644index 000000000..65c371a59--- /dev/null+++ b/wger/gallery/models/image.py@@ -0,0 +1,59 @@+# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library++# Standard Library+import datetime++# Django+import uuid++from django.contrib.auth.models import User+from django.db import models+from django.utils.translation import gettext_lazy as _+++def gallery_image_upload_dir(instance, filename):+ """+ Returns the upload target for exercise images+ """+ return "gallery/{0}/{1}".format(instance.user.id, uuid.uuid4())+++class Image(models.Model):++ class Meta:+ ordering = ["-date", ]++ date = models.DateField(_('Date'),+ default=datetime.datetime.now)++ user = models.ForeignKey(User,+ verbose_name=_('User'),+ on_delete=models.CASCADE)++ image = models.ImageField(verbose_name=_('Image'),+ help_text=_('Only PNG and JPEG formats are supported'),+ upload_to=gallery_image_upload_dir)++ description = models.TextField(verbose_name=_('Description'),+ max_length=1000,+ blank=True)++ def get_owner_object(self):+ """+ Returns the object that has owner information+ """+ return selfdiff --git a/wger/gallery/templates/images/overview.html b/wger/gallery/templates/images/overview.htmlnew file mode 100644index 000000000..8e82dd1cc--- /dev/null+++ b/wger/gallery/templates/images/overview.html@@ -0,0 +1,28 @@+{% extends "base.html" %}+{% load i18n %}+{% load static %}++{% block title %}{% translate "Gallery" %}{% endblock %}+++{% block content %}+ <div class="container">+ <div class="card-columns">+ {% for image in images %}+ <div class="card">+ <a href="{% url 'gallery:images:delete' image.pk %}">+ <img class="card-img-top" src="{{ image.image.url }}" class="img-thumbnail" alt="{{ image.description }}">+ </a>++ </div>+ {% endfor %}+ </div>+ </div>+{% endblock %}+++{% block options %}+<a href="{% url 'gallery:images:add' %}" class="btn btn-success btn-sm">+ {% translate "Add image" %}+</a>+{% endblock %}diff --git a/wger/gallery/tests/__init__.py b/wger/gallery/tests/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/gallery/urls.py b/wger/gallery/urls.pynew file mode 100644index 000000000..1dac3cc88--- /dev/null+++ b/wger/gallery/urls.py@@ -0,0 +1,48 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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+++# Django+from django.conf.urls import include+from django.urls import path++# wger+from wger.gallery.views import images+++# 'sub patterns' for gyms+patterns_images = [+ path('overview',+ images.overview,+ name='overview'),+ path('<int:pk>/edit',+ images.ImageUpdateView.as_view(),+ name='edit'),+ path('add',+ images.ImageAddView.as_view(),+ name='add'),+ path('<int:pk>/delete',+ images.ImageDeleteView.as_view(),+ name='delete'),+]+++#+# All patterns for this app+#+urlpatterns = [+ path('', include((patterns_images, 'images'), namespace="images")),+]diff --git a/wger/gallery/views/__init__.py b/wger/gallery/views/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/gallery/views/images.py b/wger/gallery/views/images.pynew file mode 100644index 000000000..13762c8fc--- /dev/null+++ b/wger/gallery/views/images.py@@ -0,0 +1,132 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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+#+# Django+from datetime import datetime++from django.contrib.auth.decorators import login_required+from django.contrib.auth.mixins import (+ LoginRequiredMixin,+ PermissionRequiredMixin+)+from django.http import HttpResponseForbidden+from django.shortcuts import render+from django.urls import reverse_lazy, reverse+from django.utils.translation import gettext_lazy as _, gettext_lazy+from django.views.generic import (+ DeleteView,+ UpdateView, CreateView+)++# wger+from wger.gallery.forms import ImageForm+from wger.gallery.models.image import Image+from wger.utils.generic_views import (+ WgerDeleteMixin,+ WgerFormMixin+)+++@login_required+def overview(request):+ """+ An overview of all the user's images+ """++ images = Image.objects.filter(user=request.user)+ context = {'images': images}++ return render(request, 'images/overview.html', context)++class ImageAddView(WgerFormMixin, CreateView):+ """+ Generic view to add a new weight entry+ """+ model = Image+ form_class = ImageForm+ title = gettext_lazy('Add')++ def get_initial(self):+ """+ Set the initial data for the form.++ Read the comment on weight/models.py WeightEntry about why we need+ to pass the user here.+ """+ return {'user': self.request.user,+ 'date': datetime.today()}++ def form_valid(self, form):+ """+ Set the owner of the entry here+ """+ form.instance.user = self.request.user+ return super(ImageAddView, self).form_valid(form)++ def get_success_url(self):+ """+ Return to overview with username+ """+ return reverse('gallery:images:overview')+++class ImageUpdateView(WgerFormMixin, LoginRequiredMixin, PermissionRequiredMixin, UpdateView):+ """+ View to update an existing gym+ """++ model = Image+ fields = ['image', 'date', 'description']+ title = gettext_lazy('Edit')+ permission_required = 'gym.change_gym'++ def dispatch(self, request, *args, **kwargs):+ """+ Only managers for this gym and general managers can edit the gym+ """+ if not request.user.is_authenticated:+ return HttpResponseForbidden()++ if request.user.has_perm('gym.manage_gym') \+ and not request.user.has_perm('gym.manage_gyms'):+ if request.user.userprofile.gym_id != int(self.kwargs['pk']):+ return HttpResponseForbidden()+ return super(ImageUpdateView, self).dispatch(request, *args, **kwargs)++ def get_context_data(self, **kwargs):+ """+ Send some additional data to the template+ """+ context = super(ImageUpdateView, self).get_context_data(**kwargs)+ context['title'] = _('Edit {0}').format(self.object)+ return context+++class ImageDeleteView(WgerDeleteMixin, LoginRequiredMixin, DeleteView):+ """+ View to delete an existing image+ """++ model = Image+ fields = ['image', 'date', 'description']+ success_url = reverse_lazy('gallery:images:overview')++ def get_context_data(self, **kwargs):+ """+ Send some additional data to the template+ """+ context = super(ImageDeleteView, self).get_context_data(**kwargs)+ context['title'] = _('Delete {0}?').format(self.object)+ return contextdiff --git a/wger/settings_global.py b/wger/settings_global.pyindex bcf5941cc..9b1c952d2 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -60,6 +60,7 @@ INSTALLED_APPS = ('wger.software','wger.utils','wger.weight',+ 'wger.gallery',# reCaptcha support, see https://github.com/praekelt/django-recaptcha'captcha',diff --git a/wger/urls.py b/wger/urls.pyindex d20086334..5c9568bfc 100644--- a/wger/urls.py+++ b/wger/urls.py@@ -113,6 +113,7 @@ urlpatterns = i18n_patterns(path('software/', include(('wger.software.urls', 'software'), namespace='software')),path('config/', include(('wger.config.urls', 'config'), namespace='config')),path('gym/', include(('wger.gym.urls', 'gym'), namespace='gym')),+ path('gallery/', include(('wger.gallery.urls', 'gallery'), namespace='gallery')),path('email', include(('wger.mailer.urls', 'email'), namespace='email')),path('sitemap.xml', index, {'sitemaps': sitemaps}, name='sitemap'),path('sitemap-<section>.xml',