diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index 36b9ed05..d9dd68c2 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -114,22 +114,24 @@ {% endmacro %} {% macro paginate(page_obj, paginator) %} + {% if page_obj.has_previous() or page_obj.has_next() %} {% if page_obj.has_previous() %} {% trans %}Previous{% endtrans %} {% else %} {% trans %}Previous{% endtrans %} {% endif %} - {% for i in paginator.page_range %} - {% if page_obj.number == i %} - {{ i }} ({% trans %}current{% endtrans %}) + {% for i in paginator.page_range %} + {% if page_obj.number == i %} + {{ i }} ({% trans %}current{% endtrans %}) + {% else %} + {{ i }} + {% endif %} + {% endfor %} + {% if page_obj.has_next() %} + {% trans %}Next{% endtrans %} {% else %} - {{ i }} + {% trans %}Next{% endtrans %} {% endif %} - {% endfor %} - {% if page_obj.has_next() %} - {% trans %}Next{% endtrans %} - {% else %} - {% trans %}Next{% endtrans %} {% endif %} {% endmacro %} diff --git a/core/views/files.py b/core/views/files.py index 60902cfe..683a6f1e 100644 --- a/core/views/files.py +++ b/core/views/files.py @@ -23,7 +23,7 @@ # # 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.edit import UpdateView, FormMixin, DeleteView 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 iterator for chunks of 8KB. """ - f = file_class.objects.filter(id=file_id).first() - if f is None or not f.file: - return not_found(request) + f = get_object_or_404(file_class, id=file_id) if not ( can_view(f, request.user) or ( diff --git a/forum/views.py b/forum/views.py index 5ffdfea8..49cee753 100644 --- a/forum/views.py +++ b/forum/views.py @@ -280,7 +280,7 @@ class ForumTopicSubscribeView(CanViewMixin, SingleObjectMixin, RedirectView): def get(self, request, *args, **kwargs): 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) else: self.object.subscribed_users.add(request.user) diff --git a/sas/models.py b/sas/models.py index a92d41c6..2a691f38 100644 --- a/sas/models.py +++ b/sas/models.py @@ -24,6 +24,7 @@ from django.db import models from django.core.urlresolvers import reverse +from django.core.cache import cache from django.conf import settings from django.utils.translation import ugettext_lazy as _ from django.utils import timezone @@ -71,16 +72,19 @@ class Picture(SithFile): return False 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 - ) # or user.can_edit(file) + return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID) def can_be_viewed_by(self, user): - # file = SithFile.objects.filter(id=self.id).first() - return self.can_be_edited_by(user) or ( - self.is_in_sas and self.is_moderated and user.was_subscribed - ) # or user.can_view(file) + # SAS pictures are visible to old subscribers + # Result is cached 4s for this user + if user.is_anonymous: + 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): return reverse("sas:download", kwargs={"picture_id": self.id}) diff --git a/sas/templates/sas/album.jinja b/sas/templates/sas/album.jinja index 38cd2431..6958adc6 100644 --- a/sas/templates/sas/album.jinja +++ b/sas/templates/sas/album.jinja @@ -1,4 +1,5 @@ {% extends "core/base.jinja" %} +{% from "core/macros.jinja" import paginate %} {% block title %} {% trans %}SAS{% endtrans %} @@ -62,7 +63,7 @@ {% endfor %}