8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 01:16:27 +00:00

Utilisation de QuerySet pour selectionner les versions voulues

Optimise plutôt que de devoir faire une fat boucle for sur l'ensemble
Fix l'affichage du paginator
This commit is contained in:
Maël Kervella 2017-09-18 22:46:57 +00:00 committed by Pierre Cadart
parent fadcbc9b31
commit 689d0c7912
3 changed files with 62 additions and 66 deletions

View file

@ -22,8 +22,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
{% endcomment %} {% endcomment %}
{% if revisions_list.paginator %} {% if versions_list.paginator %}
{% include "pagination.html" with list=revisions_list %} {% include "pagination.html" with list=versions_list %}
{% endif %} {% endif %}
{% load logs_extra %} {% load logs_extra %}
@ -36,104 +36,104 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
{% for rev in reversions_list %} {% for v in versions_list %}
{% if rev.rev.content_type.name == 'ban' %} {% if v.version.content_type.model == 'ban' %}
<tr class="danger"> <tr class="danger">
<td>{{ rev.datetime }}</td> <td>{{ v.datetime }}</td>
<td> <td>
{{ rev.username }} a banni {{ v.username }} a banni
<a href="{% url 'users:profil' rev.rev.object.user_id %}">{{ rev.rev.object.user.get_username }}</a> <a href="{% url 'users:profil' v.version.object.user_id %}">{{ v.version.object.user.get_username }}</a>
(<i> (<i>
{% if rev.rev.object.raison == '' %} {% if v.version.object.raison == '' %}
Aucune raison Aucune raison
{% else %} {% else %}
{{ rev.rev.object.raison }} {{ v.version.object.raison }}
{% endif %} {% endif %}
</i>) </i>)
</td> </td>
{% if is_bureau %} {% if is_bureau %}
<td> <td>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' rev.id %}"> <a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' v.rev_id %}">
<i class="glyphicon glyphicon-remove"></i> <i class="glyphicon glyphicon-remove"></i>
Annuler Annuler
</a> </a>
</td> </td>
{% endif %} {% endif %}
</tr> </tr>
{% elif rev.rev.content_type.name == 'whitelist' %} {% elif v.version.content_type.model == 'whitelist' %}
<tr class="success"> <tr class="success">
<td>{{ rev.datetime }}</td> <td>{{ v.datetime }}</td>
<td> <td>
{{ rev.username }} a autorisé gracieusement {{ v.username }} a autorisé gracieusement
<a href="{% url 'users:profil' rev.rev.object.user_id %}">{{ rev.rev.object.user.get_username }}</a> <a href="{% url 'users:profil' v.version.object.user_id %}">{{ v.version.object.user.get_username }}</a>
(<i> (<i>
{% if rev.rev.object.raison == '' %} {% if v.version.object.raison == '' %}
Aucune raison Aucune raison
{% else %} {% else %}
{{ rev.rev.object.raison }} {{ v.version.object.raison }}
{% endif %} {% endif %}
</i>) </i>)
</td> </td>
{% if is_bureau %} {% if is_bureau %}
<td> <td>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' rev.id %}"> <a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' v.rev_id %}">
<i class="glyphicon glyphicon-remove"></i> <i class="glyphicon glyphicon-remove"></i>
Annuler Annuler
</a> </a>
</td> </td>
{% endif %} {% endif %}
</tr> </tr>
{% elif rev.rev.content_type.name == 'user' %} {% elif v.version.content_type.model == 'user' %}
<tr> <tr>
<td>{{ rev.datetime }}</td> <td>{{ v.datetime }}</td>
<td> <td>
{{ rev.username }} a mis à jour {{ v.username }} a mis à jour
<a href="{% url 'users:profil' rev.rev.object.id %}">{{ rev.rev.object.get_username }}</a> <a href="{% url 'users:profil' v.version.object.id %}">{{ v.version.object.get_username }}</a>
{% if rev.comment != '' %} {% if v.comment != '' %}
(<i>{{ rev.comment }}</i>) (<i>{{ v.comment }}</i>)
{% endif %} {% endif %}
</td> </td>
{% if is_bureau %} {% if is_bureau %}
<td> <td>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' rev.id %}"> <a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' v.rev_id %}">
<i class="glyphicon glyphicon-remove"></i> <i class="glyphicon glyphicon-remove"></i>
Annuler Annuler
</a> </a>
</td> </td>
{% endif %} {% endif %}
</tr> </tr>
{% elif rev.rev.content_type.name == 'vente' %} {% elif v.version.content_type.model == 'vente' %}
<tr> <tr>
<td>{{ rev.datetime }}</td> <td>{{ v.datetime }}</td>
<td> <td>
{{ rev.username }} a vendu {{ rev.rev.object.number }}x {{ rev.rev.object.name }} à {{ v.username }} a vendu {{ v.version.object.number }}x {{ v.version.object.name }} à
<a href="{% url 'users:profil' rev.rev.object.facture.user_id %}">{{ rev.rev.object.facture.user.get_username }}</a> <a href="{% url 'users:profil' v.version.object.facture.user_id %}">{{ v.version.object.facture.user.get_username }}</a>
{% if rev.rev.object.iscotisation %} {% if v.version.object.iscotisation %}
(<i>+{{ rev.rev.object.duration }} mois</i>) (<i>+{{ v.version.object.duration }} mois</i>)
{% endif %} {% endif %}
</td> </td>
{% if is_bureau %} {% if is_bureau %}
<td> <td>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' rev.id %}"> <a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' v.rev_id %}">
<i class="glyphicon glyphicon-remove"></i> <i class="glyphicon glyphicon-remove"></i>
Annuler Annuler
</a> </a>
</td> </td>
{% endif %} {% endif %}
</tr> </tr>
{% elif rev.rev.content_type.name == 'interface' %} {% elif v.version.content_type.model == 'interface' %}
<tr> <tr>
<td>{{ rev.datetime }}</td> <td>{{ v.datetime }}</td>
<td> <td>
{{ rev.username }} a modifié une interface de {{ v.username }} a modifié une interface de
<a href="{% url 'users:profil' rev.rev.object.machine.user_id %}">{{ rev.rev.object.machine.user.get_username }}</a> <a href="{% url 'users:profil' v.version.object.machine.user_id %}">{{ v.version.object.machine.user.get_username }}</a>
{% if rev.comment != '' %} {% if v.comment != '' %}
(<i>{{ rev.comment }}</i>) (<i>{{ v.comment }}</i>)
{% endif %} {% endif %}
</td> </td>
{% if is_bureau %} {% if is_bureau %}
<td> <td>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' rev.id %}"> <a class="btn btn-danger btn-sm" role="button" href="{% url 'logs:revert-action' v.rev_id %}">
<i class="glyphicon glyphicon-remove"></i> <i class="glyphicon glyphicon-remove"></i>
Annuler Annuler
</a> </a>

View file

@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block content %} {% block content %}
<h2>Actions effectuées</h2> <h2>Actions effectuées</h2>
{% include "logs/aff_summary.html" with reversions_list=reversions_list %} {% include "logs/aff_summary.html" with versions_list=versions_list %}
<br /> <br />
<br /> <br />
<br /> <br />

View file

@ -37,7 +37,7 @@ from django.db import transaction
from django.db.models import Count from django.db.models import Count
from reversion.models import Revision from reversion.models import Revision
from reversion.models import Version from reversion.models import Version, ContentType
from users.models import User, ServiceUser, Right, School, ListRight, ListShell, Ban, Whitelist from users.models import User, ServiceUser, Right, School, ListRight, ListShell, Ban, Whitelist
from users.models import all_has_access, all_whitelisted, all_baned, all_adherent from users.models import all_has_access, all_whitelisted, all_baned, all_adherent
@ -70,40 +70,36 @@ def index(request):
options, created = GeneralOption.objects.get_or_create() options, created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number pagination_number = options.pagination_number
revisions = Revision.objects.all().order_by('date_created').reverse().select_related('user').prefetch_related('version_set__object') # The types of content kept for display
reversions = [] content_type_filter = ['ban', 'whitelist', 'vente', 'interface', 'user']
for revision in revisions :
for reversion in revision.version_set.all() :
content = '' # Select only wanted versions
try : versions = Version.objects.filter(content_type__in=ContentType.objects.filter(model__in=content_type_filter)).order_by('revision__date_created').reverse().select_related('revision')
content = reversion.content_type.name
except :
# If reversion has no content_type (when object has been deleted)
pass
if content in ['ban', 'whitelist', 'vente', 'interface', 'user'] : # Setup nice struct for template
reversions.append( versions_list = []
{'id' : revision.id, for v in versions :
'comment': revision.comment, if v.object :
'datetime': revision.date_created.strftime('%d/%m/%y %H:%M:%S'), versions_list.append(
'username': revision.user.get_username() if revision.user else '?', {'rev_id' : v.revision.id,
'user_id': revision.user_id, 'comment': v.revision.comment,
'rev': reversion } 'datetime': v.revision.date_created.strftime('%d/%m/%y %H:%M:%S'),
'username': v.revision.user.get_username() if v.revision.user else '?',
'user_id': v.revision.user_id,
'version': v }
) )
break
paginator = Paginator(reversions, pagination_number) paginator = Paginator(versions_list, pagination_number)
page = request.GET.get('page') page = request.GET.get('page')
try: try:
reversions = paginator.page(page) versions_list = paginator.page(page)
except PageNotAnInteger: except PageNotAnInteger:
# If page is not an integer, deliver first page. # If page is not an integer, deliver first page.
reversions = paginator.page(1) versions_list = paginator.page(1)
except EmptyPage: except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results. # If page is out of range (e.g. 9999), deliver last page of results.
reversions = paginator.page(paginator.num_pages) versions_list = paginator.page(paginator.num_pages)
return render(request, 'logs/index.html', {'reversions_list': reversions}) return render(request, 'logs/index.html', {'versions_list': versions_list})
@login_required @login_required
@permission_required('cableur') @permission_required('cableur')