Add media folder size to admin template
This commit is contained in:
parent
21e5b8a676
commit
376482e965
4 changed files with 37 additions and 4 deletions
|
@ -7,3 +7,20 @@ def cached_or_set(key, timeout, func):
|
|||
value = func()
|
||||
cache.set(key, value, timeout)
|
||||
return value
|
||||
|
||||
|
||||
def cached_or_set_locked(key, timeout, func):
|
||||
value = cache.get(key)
|
||||
if value is None:
|
||||
if cache.add(f"{key}:lock", True, 30): # 30s lukko
|
||||
value = func()
|
||||
cache.set(key, value, timeout)
|
||||
cache.delete(f"{key}:lock")
|
||||
else:
|
||||
import time
|
||||
for _ in range(10): # max 1s odotus
|
||||
value = cache.get(key)
|
||||
if value is not None:
|
||||
break
|
||||
time.sleep(0.1)
|
||||
return value
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
<strong>{{ counts.2 }}</strong>
|
||||
<a href="{% url 'gallery:photos_url' %}">
|
||||
{% blocktrans count photo_count=counts.2 %}photo{% plural %}photos{% endblocktrans %}
|
||||
</a>
|
||||
</a> ( {{ counts.3 }} GB )
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -2,8 +2,9 @@ from django import template
|
|||
from django.core.cache import cache
|
||||
|
||||
from config.cache_durations import *
|
||||
from gallery.cache import cached_or_set
|
||||
from gallery.cache import cached_or_set_locked
|
||||
from gallery.models import Album, Photo
|
||||
from gallery.utils import get_media_size_gb
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
@ -14,12 +15,13 @@ def gallery_stats():
|
|||
Returns a tuple with (total albums, public albums, total photos).
|
||||
Result is cached for GALLERY_STATS_DURATION.
|
||||
"""
|
||||
return cached_or_set(
|
||||
return cached_or_set_locked(
|
||||
"gallery_stats",
|
||||
GALLERY_STATS_DURATION,
|
||||
lambda: (
|
||||
Album.objects.count(),
|
||||
Album.objects.filter(is_public=True).count(),
|
||||
Photo.objects.count()
|
||||
Photo.objects.count(),
|
||||
get_media_size_gb()
|
||||
)
|
||||
)
|
||||
|
|
|
@ -1,9 +1,23 @@
|
|||
import os
|
||||
from urllib.parse import urlencode, urlparse, urlunparse
|
||||
|
||||
from django.conf import settings
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
def get_media_size_gb():
|
||||
total_size = 0
|
||||
media_root = settings.MEDIA_ROOT
|
||||
for dirpath, dirnames, filenames in os.walk(media_root):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dirpath, f)
|
||||
try:
|
||||
total_size += os.path.getsize(fp)
|
||||
except OSError:
|
||||
pass
|
||||
return round(total_size / (1024 ** 3), 2) # GB, kahden desimaalin tarkkuudella
|
||||
|
||||
|
||||
def build_canonical_url(request, base_url=None, params=None):
|
||||
|
||||
if base_url is None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue