Caches é SQL optimisations
This commit is contained in:
parent
213644d0af
commit
f1673da45f
5 changed files with 69 additions and 14 deletions
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
import unicodedata
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
|
|
@ -16,6 +15,7 @@ from django.conf import settings
|
|||
from django.core.exceptions import ValidationError
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.validators import RegexValidator
|
||||
from django.core.cache import caches
|
||||
from django.db import models
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.urls import reverse
|
||||
|
|
@ -25,6 +25,8 @@ from django.utils.timezone import now
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
from PIL import Image, ImageFile, ImageFilter
|
||||
|
||||
|
||||
|
||||
logger = logging.getLogger("photologue.models")
|
||||
|
||||
# Default limit for gallery.latest
|
||||
|
|
@ -184,13 +186,15 @@ class Gallery(models.Model):
|
|||
def sample(self, public=True):
|
||||
"""Return a sample of photos, ordered at random."""
|
||||
count = 1
|
||||
if count > self.photo_count():
|
||||
count = self.photo_count()
|
||||
nb = self.photo_count(public) #Optimisation don't do twice the SQL requests
|
||||
if nb < count:
|
||||
count = nb
|
||||
|
||||
if public:
|
||||
photo_set = self.photos.filter(is_public=True)
|
||||
else:
|
||||
photo_set = self.photos
|
||||
return random.sample(list(photo_set), count)
|
||||
return photo_set.order_by("?")[:count] # Use native SQL random
|
||||
|
||||
def photo_count(self, public=True):
|
||||
"""Return a count of all the photos in this gallery."""
|
||||
|
|
@ -721,10 +725,16 @@ class PhotoSizeCache:
|
|||
|
||||
def __init__(self):
|
||||
self.__dict__ = self.__state
|
||||
if not len(self.sizes):
|
||||
sizes = PhotoSize.objects.all()
|
||||
for size in sizes:
|
||||
self.sizes[size.name] = size
|
||||
|
||||
cached = caches.get("PhotoSizeCache",None)
|
||||
if cached is None :
|
||||
if not len(self.sizes):
|
||||
sizes = PhotoSize.objects.all()
|
||||
for size in sizes:
|
||||
self.sizes[size.name] = size
|
||||
caches.set("PhotoSizeCache",self)
|
||||
else :
|
||||
self = cached
|
||||
|
||||
def reset(self):
|
||||
global size_method_map
|
||||
|
|
|
|||
|
|
@ -20,10 +20,16 @@ from django.views.generic.dates import ArchiveIndexView, YearArchiveView
|
|||
from django.views.generic.detail import DetailView
|
||||
from django.views.generic.edit import DeleteView, FormView
|
||||
from PIL import Image
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
|
||||
|
||||
from .forms import UploadForm
|
||||
from .models import Gallery, Photo, Tag
|
||||
|
||||
# Cette ligne renvoie le modèle d'utilisateur actif (le natif ou le vôtre)
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class GalleryDateView(LoginRequiredMixin):
|
||||
model = Gallery
|
||||
|
|
@ -135,11 +141,11 @@ class GalleryDetailView(LoginRequiredMixin, DetailView):
|
|||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
# Non-staff members only see public photos
|
||||
# Non-staff members only see public photos + prefetch all owners informations (Optimisation)
|
||||
if self.request.user.is_staff:
|
||||
context["photos"] = self.object.photos.all()
|
||||
context["photos"] = self.object.photos.all().select_related('owner')
|
||||
else:
|
||||
context["photos"] = self.object.photos.filter(is_public=True)
|
||||
context["photos"] = self.object.photos.filter(is_public=True).select_related('onwer')
|
||||
|
||||
# List owners
|
||||
context["owners"] = []
|
||||
|
|
@ -147,6 +153,8 @@ class GalleryDetailView(LoginRequiredMixin, DetailView):
|
|||
if photo.owner not in context["owners"]:
|
||||
context["owners"].append(photo.owner)
|
||||
|
||||
|
||||
|
||||
# Filter on owner
|
||||
if "owner" in self.kwargs:
|
||||
context["photos"] = context["photos"].filter(owner__id=self.kwargs["owner"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue