Start working on gallery app

This will allow users to upload pictures of themselves
This commit is contained in:
Roland Geider
2021-05-05 15:03:40 +02:00
parent 246c9bb3ea
commit b646eff459
14 changed files with 352 additions and 0 deletions

24
wger/gallery/__init__.py Normal file
View File

@@ -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'

5
wger/gallery/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class GalleryConfig(AppConfig):
name = 'wger.gallery'
verbose_name = "Gallery"

21
wger/gallery/forms.py Normal file
View File

@@ -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(),
}

View File

@@ -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'],
},
),
]

View File

View File

@@ -0,0 +1 @@
from .image import Image

View File

@@ -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 self

View File

@@ -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 %}

View File

48
wger/gallery/urls.py Normal file
View File

@@ -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")),
]

View File

View File

@@ -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 context

View File

@@ -60,6 +60,7 @@ INSTALLED_APPS = (
'wger.software',
'wger.utils',
'wger.weight',
'wger.gallery',
# reCaptcha support, see https://github.com/praekelt/django-recaptcha
'captcha',

View File

@@ -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',