Merge branch 'performances' into 'master'

Improve performances on forum and SAS

See merge request ae/Sith!235
This commit is contained in:
Antoine Bartuccio 2019-09-16 14:23:52 +02:00
commit 411c117f0f
7 changed files with 48 additions and 24 deletions

View File

@ -114,22 +114,24 @@
{% endmacro %} {% endmacro %}
{% macro paginate(page_obj, paginator) %} {% macro paginate(page_obj, paginator) %}
{% if page_obj.has_previous() or page_obj.has_next() %}
{% if page_obj.has_previous() %} {% if page_obj.has_previous() %}
<a href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a> <a href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a>
{% else %} {% else %}
<span class="disabled">{% trans %}Previous{% endtrans %}</span> <span class="disabled">{% trans %}Previous{% endtrans %}</span>
{% endif %} {% endif %}
{% for i in paginator.page_range %} {% for i in paginator.page_range %}
{% if page_obj.number == i %} {% if page_obj.number == i %}
<span class="active">{{ i }} <span class="sr-only">({% trans %}current{% endtrans %})</span></span> <span class="active">{{ i }} <span class="sr-only">({% trans %}current{% endtrans %})</span></span>
{% else %}
<a href="?page={{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next() %}
<a href="?page={{ page_obj.next_page_number() }}">{% trans %}Next{% endtrans %}</a>
{% else %} {% else %}
<a href="?page={{ i }}">{{ i }}</a> <span class="disabled">{% trans %}Next{% endtrans %}</span>
{% endif %} {% endif %}
{% endfor %}
{% if page_obj.has_next() %}
<a href="?page={{ page_obj.next_page_number() }}">{% trans %}Next{% endtrans %}</a>
{% else %}
<span class="disabled">{% trans %}Next{% endtrans %}</span>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}

View File

@ -23,7 +23,7 @@
# #
# This file contains all the views that concern the page model # This file contains all the views that concern the page model
from django.shortcuts import redirect from django.shortcuts import redirect, get_object_or_404
from django.views.generic import ListView, DetailView, TemplateView from django.views.generic import ListView, DetailView, TemplateView
from django.views.generic.edit import UpdateView, FormMixin, DeleteView from django.views.generic.edit import UpdateView, FormMixin, DeleteView
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
@ -51,9 +51,7 @@ def send_file(request, file_id, file_class=SithFile, file_attr="file"):
memory at once. The FileWrapper will turn the file object into an memory at once. The FileWrapper will turn the file object into an
iterator for chunks of 8KB. iterator for chunks of 8KB.
""" """
f = file_class.objects.filter(id=file_id).first() f = get_object_or_404(file_class, id=file_id)
if f is None or not f.file:
return not_found(request)
if not ( if not (
can_view(f, request.user) can_view(f, request.user)
or ( or (

View File

@ -280,7 +280,7 @@ class ForumTopicSubscribeView(CanViewMixin, SingleObjectMixin, RedirectView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
if request.user in self.object.subscribed_users.all(): if self.object.subscribed_users.filter(id=request.user.id).exists():
self.object.subscribed_users.remove(request.user) self.object.subscribed_users.remove(request.user)
else: else:
self.object.subscribed_users.add(request.user) self.object.subscribed_users.add(request.user)

View File

@ -24,6 +24,7 @@
from django.db import models from django.db import models
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
@ -71,16 +72,19 @@ class Picture(SithFile):
return False return False
def can_be_edited_by(self, user): def can_be_edited_by(self, user):
# file = SithFile.objects.filter(id=self.id).first() return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)
return user.is_in_group(
settings.SITH_GROUP_SAS_ADMIN_ID
) # or user.can_edit(file)
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
# file = SithFile.objects.filter(id=self.id).first() # SAS pictures are visible to old subscribers
return self.can_be_edited_by(user) or ( # Result is cached 4s for this user
self.is_in_sas and self.is_moderated and user.was_subscribed if user.is_anonymous:
) # or user.can_view(file) return False
perm = cache.get("%d_can_view_pictures" % (user.id), False)
if perm:
return perm
perm = self.is_in_sas and self.is_moderated and user.was_subscribed
cache.set("%d_can_view_pictures" % (user.id), perm, timeout=4)
return perm
def get_download_url(self): def get_download_url(self):
return reverse("sas:download", kwargs={"picture_id": self.id}) return reverse("sas:download", kwargs={"picture_id": self.id})

View File

@ -1,4 +1,5 @@
{% extends "core/base.jinja" %} {% extends "core/base.jinja" %}
{% from "core/macros.jinja" import paginate %}
{% block title %} {% block title %}
{% trans %}SAS{% endtrans %} {% trans %}SAS{% endtrans %}
@ -62,7 +63,7 @@
{% endfor %} {% endfor %}
</div> </div>
<div> <div>
{% for p in album.children_pictures.order_by('id') %} {% for p in pictures %}
<div style="display: inline-block;"> <div style="display: inline-block;">
{% if edit_mode %} {% if edit_mode %}
<input type="checkbox" name="file_list" value="{{ p.id }}"> <input type="checkbox" name="file_list" value="{{ p.id }}">
@ -77,6 +78,8 @@
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<br>
{{ paginate(pictures, paginator) }}
{% if edit_mode %} {% if edit_mode %}
</form> </form>
{% endif %} {% endif %}

View File

@ -23,7 +23,7 @@
# #
from django.shortcuts import redirect from django.shortcuts import redirect
from django.http import HttpResponse from django.http import HttpResponse, Http404
from django.core.urlresolvers import reverse_lazy, reverse from django.core.urlresolvers import reverse_lazy, reverse
from core.views.forms import SelectDate from core.views.forms import SelectDate
from django.views.generic import DetailView, TemplateView from django.views.generic import DetailView, TemplateView
@ -32,6 +32,7 @@ from django.utils.translation import ugettext_lazy as _
from django.conf import settings from django.conf import settings
from django import forms from django import forms
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator, InvalidPage
from ajax_select import make_ajax_field from ajax_select import make_ajax_field
from ajax_select.fields import AutoCompleteSelectMultipleField from ajax_select.fields import AutoCompleteSelectMultipleField
@ -252,6 +253,14 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
form_class = SASForm form_class = SASForm
pk_url_kwarg = "album_id" pk_url_kwarg = "album_id"
template_name = "sas/album.jinja" template_name = "sas/album.jinja"
paginate_by = settings.SITH_SAS_IMAGES_PER_PAGE
def dispatch(self, request, *args, **kwargs):
try:
self.asked_page = int(request.GET.get("page", 1))
except ValueError:
raise Http404
return super(AlbumView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.form = self.get_form() self.form = self.get_form()
@ -291,6 +300,13 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs = super(AlbumView, self).get_context_data(**kwargs) kwargs = super(AlbumView, self).get_context_data(**kwargs)
kwargs["paginator"] = Paginator(
self.object.children_pictures.order_by("id"), self.paginate_by
)
try:
kwargs["pictures"] = kwargs["paginator"].page(self.asked_page)
except InvalidPage:
raise Http404
kwargs["form"] = self.form kwargs["form"] = self.form
kwargs["clipboard"] = SithFile.objects.filter( kwargs["clipboard"] = SithFile.objects.filter(
id__in=self.request.session["clipboard"] id__in=self.request.session["clipboard"]

View File

@ -338,6 +338,7 @@ SITH_FORUM_PAGE_LENGTH = 30
# SAS variables # SAS variables
SITH_SAS_ROOT_DIR_ID = 4 SITH_SAS_ROOT_DIR_ID = 4
SITH_SAS_IMAGES_PER_PAGE = 30
SITH_BOARD_SUFFIX = "-bureau" SITH_BOARD_SUFFIX = "-bureau"
SITH_MEMBER_SUFFIX = "-membres" SITH_MEMBER_SUFFIX = "-membres"