Verified Commit deb28aa9 authored by erdnaxe's avatar erdnaxe 🦋
Browse files

Change media permissions

parent 8a1af4c2
......@@ -17,11 +17,15 @@ class MediaAdmin(VersionAdmin):
class EmpruntAdmin(VersionAdmin):
list_display = ('media', 'user', 'date_emprunt', 'date_rendu', 'permanencier_emprunt', 'permanencier_rendu')
list_display = (
'media', 'user', 'date_emprunt', 'date_rendu', 'permanencier_emprunt',
'permanencier_rendu')
class JeuAdmin(VersionAdmin):
list_display = ('nom', 'proprietaire', 'duree', 'nombre_joueurs_min', 'nombre_joueurs_max', 'comment')
list_display = (
'nom', 'proprietaire', 'duree', 'nombre_joueurs_min', 'nombre_joueurs_max',
'comment')
admin.site.register(Auteur, AuteurAdmin)
......
......@@ -15,7 +15,9 @@ class AuteurForm(ModelForm):
class MediaForm(ModelForm):
auteur = forms.ModelMultipleChoiceField(Auteur.objects.all(), widget=forms.CheckboxSelectMultiple, required=False)
auteur = forms.ModelMultipleChoiceField(Auteur.objects.all(),
widget=forms.CheckboxSelectMultiple,
required=False)
class Meta:
model = Media
......@@ -28,7 +30,8 @@ class JeuForm(ModelForm):
fields = '__all__'
def clean_nombre_joueurs_max(self):
if self.cleaned_data['nombre_joueurs_max'] < self.cleaned_data['nombre_joueurs_min']:
if self.cleaned_data['nombre_joueurs_max'] < self.cleaned_data[
'nombre_joueurs_min']:
raise forms.ValidationError("Max ne peut être inférieur à min")
return self.cleaned_data['nombre_joueurs_max']
......@@ -42,4 +45,5 @@ class EmpruntForm(ModelForm):
class EditEmpruntForm(ModelForm):
class Meta:
model = Emprunt
fields = ['media', 'permanencier_emprunt', 'permanencier_rendu', 'date_rendu']
fields = ['media', 'permanencier_emprunt', 'permanencier_rendu',
'date_rendu']
......@@ -35,11 +35,23 @@ class Emprunt(models.Model):
media = models.ForeignKey('Media', on_delete=models.PROTECT)
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
date_emprunt = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S')
date_rendu = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S', blank=True, null=True)
permanencier_emprunt = models.ForeignKey('users.User', on_delete=models.PROTECT,
related_name='user_permanencier_emprunt')
permanencier_rendu = models.ForeignKey('users.User', on_delete=models.PROTECT,
related_name='user_permanencier_rendu', blank=True, null=True)
date_rendu = models.DateTimeField(
help_text='%d/%m/%y %H:%M:%S',
blank=True,
null=True,
)
permanencier_emprunt = models.ForeignKey(
'users.User',
on_delete=models.PROTECT,
related_name='user_permanencier_emprunt',
)
permanencier_rendu = models.ForeignKey(
'users.User',
on_delete=models.PROTECT,
related_name='user_permanencier_rendu',
blank=True,
null=True,
)
def __str__(self):
return str(self.media) + str(self.user)
......@@ -63,7 +75,8 @@ class Jeu(models.Model):
duree = models.CharField(choices=DUREE, max_length=255)
nombre_joueurs_min = models.IntegerField(validators=[MinValueValidator(1)])
nombre_joueurs_max = models.IntegerField(validators=[MinValueValidator(1)])
comment = models.CharField(help_text="Commentaire", max_length=255, blank=True, null=True)
comment = models.CharField(help_text="Commentaire", max_length=255,
blank=True, null=True)
def __str__(self):
return str(self.nom)
......
......@@ -23,22 +23,31 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% endcomment %}
{% if auteurs_list.paginator %}
{% include "pagination.html" with list=auteurs_list %}
{% include "pagination.html" with list=auteurs_list %}
{% endif %}
<table class="table table-striped">
<thead>
<tr>
<th>Nom</th>
<th></th>
</tr>
</thead>
{% for auteur in auteurs_list %}
<table class="table table-striped">
<thead>
<tr>
<th>Nom</th>
<th></th>
</tr>
</thead>
{% for auteur in auteurs_list %}
<tr>
<td>{{ auteur.nom }}</td>
<td>{% if is_perm %}{% include 'buttons/edit.html' with href='media:edit-auteur' id=auteur.id %}
{% include 'buttons/suppr.html' with href='media:del-auteur' id=auteur.id %}{% endif %}
{% include 'buttons/history.html' with href='media:history' name='auteur' id=auteur.id %}</td>
<td>
{% if is_perm %}
<a class="btn btn-primary btn-sm" role="button" title="Éditer"
href="{% url 'media:edit-auteur' auteur.id %}">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'media:del-auteur' auteur.id %}"
title="Supprimer">
<i class="glyphicon glyphicon-trash"></i>
</a>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
{% endfor %}
</table>
......@@ -46,9 +46,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<td>{{ jeu.nombre_joueurs_min }}</td>
<td>{{ jeu.nombre_joueurs_max }}</td>
<td>{{ jeu.comment }}</td>
<td>{% if is_perm %}{% include 'buttons/edit.html' with href='media:edit-jeu' id=jeu.id %}
{% include 'buttons/suppr.html' with href='media:del-jeu' id=jeu.id %}{% endif %}
{% include 'buttons/history.html' with href='media:history' name='jeu' id=jeu.id %}</td>
<td>
{% if is_perm %}
<a class="btn btn-primary btn-sm" role="button" title="Éditer"
href="{% url 'media:edit-jeu' jeu.id %}">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'media:del-jeu' jeu.id %}"
title="Supprimer">
<i class="glyphicon glyphicon-trash"></i>
</a>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
......@@ -23,26 +23,35 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% endcomment %}
{% if medias_list.paginator %}
{% include "pagination.html" with list=medias_list %}
{% include "pagination.html" with list=medias_list %}
{% endif %}
<table class="table table-striped">
<thead>
<tr>
<th>Titre</th>
<th>Auteur</th>
<th>Cote</th>
<th></th>
</tr>
</thead>
{% for media in medias_list %}
<table class="table table-striped">
<thead>
<tr>
<th>Titre</th>
<th>Auteur</th>
<th>Cote</th>
<th></th>
</tr>
</thead>
{% for media in medias_list %}
<tr>
<td>{{ media.titre }}</td>
<td>{% for aut in media.auteur.all %}{{ aut }}, {% endfor %}</td>
<td>{{ media.cote }}</td>
<td>{% if is_perm %}{% include 'buttons/edit.html' with href='media:edit-media' id=media.id %}
{% include 'buttons/suppr.html' with href='media:del-media' id=media.id %}{% endif %}
{% include 'buttons/history.html' with href='media:history' name='media' id=media.id %}</td>
<td>
{% if is_perm %}
<a class="btn btn-primary btn-sm" role="button" title="Éditer"
href="{% url 'media:edit-media' media.id %}">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'media:del-media' media.id %}"
title="Supprimer">
<i class="glyphicon glyphicon-trash"></i>
</a>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
{% endfor %}
</table>
{% extends "base.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load bootstrap3 %}
{% block title %}Historique{% endblock %}
{% block content %}
<h2>Historique de {{ object }}</h2>
{% if reversions.paginator %}
<ul class="pagination nav navbar-nav">
{% if reversions.has_previous %}
<li><a href="?page={{ reversions.previous_page_number }}">Suivants</a></li>
{% endif %}
{% for page in reversions.paginator.page_range %}
<li class="{% if reversions.number == page %}active{% endif %}"><a
href="?page={{ page }}">{{ page }}</a></li>
{% endfor %}
{% if reversions.has_next %}
<li><a href="?page={{ reversions.next_page_number }}">Précédents</a></li>
{% endif %}
</ul>
{% endif %}
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Cableur</th>
<th>Commentaire</th>
</tr>
</thead>
{% for rev in reversions %}
<tr>
<td>{{ rev.revision.date_created }}</td>
<td>{{ rev.revision.user }}</td>
<td>{{ rev.revision.comment }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
......@@ -8,25 +8,28 @@ from . import views
urlpatterns = [
url(r'^add_auteur/$', views.add_auteur, name='add-auteur'),
url(r'^edit_auteur/(?P<auteurid>[0-9]+)$', views.edit_auteur, name='edit-auteur'),
url(r'^del_auteur/(?P<auteurid>[0-9]+)$', views.del_auteur, name='del-auteur'),
url(r'^edit_auteur/(?P<auteurid>[0-9]+)$', views.edit_auteur,
name='edit-auteur'),
url(r'^del_auteur/(?P<auteurid>[0-9]+)$', views.del_auteur,
name='del-auteur'),
url(r'^index_auteurs/$', views.index_auteurs, name='index-auteurs'),
url(r'^history/(?P<object>auteur)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^add_jeu/$', views.add_jeu, name='add-jeu'),
url(r'^edit_jeu/(?P<jeuid>[0-9]+)$', views.edit_jeu, name='edit-jeu'),
url(r'^del_jeu/(?P<jeuid>[0-9]+)$', views.del_jeu, name='del-jeu'),
url(r'^index_jeux/$', views.index_jeux, name='index-jeux'),
url(r'^history/(?P<object>jeu)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^add_media/$', views.add_media, name='add-media'),
url(r'^edit_media/(?P<mediaid>[0-9]+)$', views.edit_media, name='edit-media'),
url(r'^edit_media/(?P<mediaid>[0-9]+)$', views.edit_media,
name='edit-media'),
url(r'^del_media/(?P<mediaid>[0-9]+)$', views.del_media, name='del-media'),
url(r'^index_medias/$', views.index_medias, name='index-medias'),
url(r'^history/(?P<object>media)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^add_emprunt/(?P<userid>[0-9]+)$', views.add_emprunt, name='add-emprunt'),
url(r'^retour_emprunt/(?P<empruntid>[0-9]+)$', views.retour_emprunt, name='retour-emprunt'),
url(r'^edit_emprunt/(?P<empruntid>[0-9]+)$', views.edit_emprunt, name='edit-emprunt'),
url(r'^del_emprunt/(?P<empruntid>[0-9]+)$', views.del_emprunt, name='del-emprunt'),
url(r'^add_emprunt/(?P<userid>[0-9]+)$', views.add_emprunt,
name='add-emprunt'),
url(r'^retour_emprunt/(?P<empruntid>[0-9]+)$', views.retour_emprunt,
name='retour-emprunt'),
url(r'^edit_emprunt/(?P<empruntid>[0-9]+)$', views.edit_emprunt,
name='edit-emprunt'),
url(r'^del_emprunt/(?P<empruntid>[0-9]+)$', views.del_emprunt,
name='del-emprunt'),
url(r'^index_emprunts/$', views.index, name='index'),
url(r'^history/(?P<object>emprunt)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^$', views.index, name='index'),
]
......@@ -10,7 +10,6 @@ from django.shortcuts import render, redirect
from django.template.context_processors import csrf
from django.utils import timezone
from reversion import revisions as reversion
from reversion.models import Version
from med.settings import PAGINATION_NUMBER
from users.models import User
......@@ -25,7 +24,7 @@ def form(ctx, template, request):
@login_required
@permission_required('perm')
@permission_required('media.add_auteur')
def add_auteur(request):
auteur = AuteurForm(request.POST or None)
if auteur.is_valid():
......@@ -39,7 +38,7 @@ def add_auteur(request):
@login_required
@permission_required('perm')
@permission_required('media.edit_auteur')
def edit_auteur(request, auteurid):
try:
auteur_instance = Auteur.objects.get(pk=auteurid)
......@@ -51,14 +50,15 @@ def edit_auteur(request, auteurid):
with transaction.atomic(), reversion.create_revision():
auteur.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in auteur.changed_data))
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in auteur.changed_data))
messages.success(request, "Auteur modifié")
return redirect("/media/index_auteurs/")
return form({'mediaform': auteur}, 'media/media.html', request)
@login_required
@permission_required('perm')
@permission_required('media.delete_auteur')
def del_auteur(request, auteurid):
try:
auteur_instance = Auteur.objects.get(pk=auteurid)
......@@ -71,11 +71,12 @@ def del_auteur(request, auteurid):
reversion.set_user(request.user)
messages.success(request, "L'auteur a été détruit")
return redirect("/media/index_auteurs")
return form({'objet': auteur_instance, 'objet_name': 'auteur'}, 'media/delete.html', request)
return form({'objet': auteur_instance, 'objet_name': 'auteur'},
'media/delete.html', request)
@login_required
@permission_required('perm')
@permission_required('media.add_media')
def add_media(request):
media = MediaForm(request.POST or None)
if media.is_valid():
......@@ -89,7 +90,7 @@ def add_media(request):
@login_required
@permission_required('perm')
@permission_required('media.edit_media')
def edit_media(request, mediaid):
try:
media_instance = Media.objects.get(pk=mediaid)
......@@ -101,14 +102,15 @@ def edit_media(request, mediaid):
with transaction.atomic(), reversion.create_revision():
media.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in media.changed_data))
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in media.changed_data))
messages.success(request, "Media modifié")
return redirect("/media/index_medias/")
return form({'mediaform': media}, 'media/media.html', request)
@login_required
@permission_required('perm')
@permission_required('media.delete_media')
def del_media(request, mediaid):
try:
media_instance = Media.objects.get(pk=mediaid)
......@@ -121,11 +123,12 @@ def del_media(request, mediaid):
reversion.set_user(request.user)
messages.success(request, "Le media a été détruit")
return redirect("/media/index_medias")
return form({'objet': media_instance, 'objet_name': 'media'}, 'media/delete.html', request)
return form({'objet': media_instance, 'objet_name': 'media'},
'media/delete.html', request)
@login_required
@permission_required('perm')
@permission_required('media.add_jeu')
def add_jeu(request):
jeu = JeuForm(request.POST or None)
if jeu.is_valid():
......@@ -139,7 +142,7 @@ def add_jeu(request):
@login_required
@permission_required('perm')
@permission_required('media.edit_jeu')
def edit_jeu(request, jeuid):
try:
jeu_instance = Jeu.objects.get(pk=jeuid)
......@@ -151,14 +154,15 @@ def edit_jeu(request, jeuid):
with transaction.atomic(), reversion.create_revision():
jeu.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in jeu.changed_data))
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in jeu.changed_data))
messages.success(request, "Media modifié")
return redirect("/media/index_jeux/")
return form({'mediaform': jeu}, 'media/media.html', request)
@login_required
@permission_required('perm')
@permission_required('media.delete_jeu')
def del_jeu(request, jeuid):
try:
jeu_instance = Jeu.objects.get(pk=jeuid)
......@@ -171,20 +175,23 @@ def del_jeu(request, jeuid):
reversion.set_user(request.user)
messages.success(request, "Le jeu a été détruit")
return redirect("/media/index_jeux")
return form({'objet': jeu_instance, 'objet_name': 'jeu'}, 'media/delete.html', request)
return form({'objet': jeu_instance, 'objet_name': 'jeu'},
'media/delete.html', request)
@login_required
@permission_required('perm')
@permission_required('media.add_emprunt')
def add_emprunt(request, userid):
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, u"Entrée inexistante")
return redirect("/media/index_emprunts/")
emprunts_en_cours = Emprunt.objects.filter(date_rendu=None, user=user).count()
emprunts_en_cours = Emprunt.objects.filter(date_rendu=None,
user=user).count()
if emprunts_en_cours >= user.maxemprunt:
messages.error(request, "Maximum d'emprunts atteint de l'user %s" % user.maxemprunt)
messages.error(request,
"Maximum d'emprunts atteint de l'user %s" % user.maxemprunt)
return redirect("/media/index_emprunts/")
emprunt = EmpruntForm(request.POST or None)
if emprunt.is_valid():
......@@ -202,7 +209,7 @@ def add_emprunt(request, userid):
@login_required
@permission_required('perm')
@permission_required('media.edit_emprunt')
def edit_emprunt(request, empruntid):
try:
emprunt_instance = Emprunt.objects.get(pk=empruntid)
......@@ -214,14 +221,15 @@ def edit_emprunt(request, empruntid):
with transaction.atomic(), reversion.create_revision():
emprunt.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in emprunt.changed_data))
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in emprunt.changed_data))
messages.success(request, "Emprunt modifié")
return redirect("/media/index_emprunts/")
return form({'mediaform': emprunt}, 'media/media.html', request)
@login_required
@permission_required('perm')
@permission_required('media.edit_emprunt')
def retour_emprunt(request, empruntid):
try:
emprunt_instance = Emprunt.objects.get(pk=empruntid)
......@@ -238,7 +246,7 @@ def retour_emprunt(request, empruntid):
@login_required
@permission_required('perm')
@permission_required('media.delete_emprunt')
def del_emprunt(request, empruntid):
try:
emprunt_instance = Emprunt.objects.get(pk=empruntid)
......@@ -251,7 +259,8 @@ def del_emprunt(request, empruntid):
reversion.set_user(request.user)
messages.success(request, "L'emprunt a été détruit")
return redirect("/media/index_emprunts")
return form({'objet': emprunt_instance, 'objet_name': 'emprunt'}, 'media/delete.html', request)
return form({'objet': emprunt_instance, 'objet_name': 'emprunt'},
'media/delete.html', request)
@login_required
......@@ -283,7 +292,8 @@ def index_auteurs(request):
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
auteurs_list = paginator.page(paginator.num_pages)
return render(request, 'media/index_auteurs.html', {'auteurs_list': auteurs_list})
return render(request, 'media/index_auteurs.html',
{'auteurs_list': auteurs_list})
@login_required
......@@ -299,7 +309,8 @@ def index_medias(request):
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
medias_list = paginator.page(paginator.num_pages)
return render(request, 'media/index_medias.html', {'medias_list': medias_list})
return render(request, 'media/index_medias.html',
{'medias_list': medias_list})
@login_required
......@@ -308,7 +319,8 @@ def index(request):
emprunts_list = Emprunt.objects.all()
else:
emprunts_list = Emprunt.objects.filter(user=request.user)
paginator = Paginator(emprunts_list.order_by('date_emprunt').reverse(), PAGINATION_NUMBER)
paginator = Paginator(emprunts_list.order_by('date_emprunt').reverse(),
PAGINATION_NUMBER)
page = request.GET.get('page')
try:
emprunts_list = paginator.page(page)
......@@ -318,44 +330,5 @@ def index(request):
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
emprunts_list = paginator.page(paginator.num_pages)
return render(request, 'media/index_emprunts.html', {'emprunts_list': emprunts_list})
@login_required
def history(request, object, id):
if object == 'auteur':
try:
object_instance = Auteur.objects.get(pk=id)
except Auteur.DoesNotExist:
messages.error(request, "Auteur inexistant")
return redirect("/media/index_auteurs")
elif object == 'media':
try:
object_instance = Media.objects.get(pk=id)
except Media.DoesNotExist:
messages.error(request, "Media inexistant")
return redirect("/media/index_medias")
elif object == 'emprunt':
try:
object_instance = Emprunt.objects.get(pk=id)
except Emprunt.DoesNotExist:
messages.error(request, "Emprunt inexistant")
return redirect("/media/index_emprunts")
elif object == 'jeu':
try:
object_instance = Jeu.objects.get(pk=id)
except Jeu.DoesNotExist:
messages.error(request, "Jeu inexistant")
return redirect("/media/index_jeux")
reversions = Version.objects.get_for_object(object_instance)
paginator = Paginator(reversions, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
reversions = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
reversions = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
reversions = paginator.page(paginator.num_pages)
return render(request, 'media/history.html', {'reversions': reversions, 'object': object_instance})
return render(request, 'media/index_emprunts.html',
{'emprunts_list': emprunts_list})
{% comment %}
Re2o est un logiciel d'administration développé initiallement au rezometz. Il
se veut agnostique au réseau considéré, de manière à être installable en
quelques clics.
Copyright © 2017 Gabriel Détraz
Copyright © 2017 Goulven Kermarec
Copyright © 2017 Augustin Lemesle
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
{% endcomment %}
<a class="btn btn-primary btn-sm" role="button" href="{% url href id %}" title="{{ desc|default:"Ajouter" }}">
<i class="glyphicon glyphicon-plus"></i>
</a>
{% comment %}
Re2o est un logiciel d'administration développé initiallement au rezometz. Il
se veut agnostique au réseau considéré, de manière à être installable en
quelques clics.
Copyright © 2017 Gabriel Détraz
Copyright © 2017 Goulven Kermarec
Copyright © 2017 Augustin Lemesle
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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