Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ def has_changes(self):

def recalculate_editors_storage(self):
from contentcuration.utils.user import calculate_user_storage
for editor in self.files.values_list('uploaded_by_id', flat=True):
for editor in self.files.values_list('uploaded_by_id', flat=True).distinct():
calculate_user_storage(editor)

def on_create(self):
Expand Down
4 changes: 1 addition & 3 deletions contentcuration/contentcuration/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from celery import states
from celery.utils.log import get_task_logger
from django.conf import settings
from django.core.cache import cache
from django.core.mail import EmailMessage
from django.db import IntegrityError
from django.db.utils import OperationalError
Expand All @@ -27,7 +26,6 @@
from contentcuration.utils.nodes import generate_diff
from contentcuration.utils.publish import publish_channel
from contentcuration.utils.sync import sync_channel
from contentcuration.utils.user import CACHE_USER_STORAGE_KEY
from contentcuration.viewsets.sync.constants import CHANNEL
from contentcuration.viewsets.sync.constants import CONTENTNODE
from contentcuration.viewsets.sync.constants import COPYING_FLAG
Expand Down Expand Up @@ -266,7 +264,6 @@ def calculate_user_storage_task(user_id):
try:
user = User.objects.get(pk=user_id)
user.set_space_used()
cache.delete(CACHE_USER_STORAGE_KEY.format(user_id))
except User.DoesNotExist:
logging.error("Tried to calculate user storage for user with id {} but they do not exist".format(user_id))

Expand Down Expand Up @@ -296,6 +293,7 @@ def sendcustomemails_task(subject, message, query):
"export-channel": export_channel_task,
"sync-channel": sync_channel_task,
"get-node-diff": generatenodediff_task,
"calculate-user-storage": calculate_user_storage_task,
"calculate-resource-size": calculate_resource_size_task,
}

Expand Down
19 changes: 11 additions & 8 deletions contentcuration/contentcuration/utils/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from django.core.cache import cache
import logging

CACHE_USER_STORAGE_KEY = "user_storage_{}"
from contentcuration.tasks import get_or_create_async_task


def calculate_user_storage(user_id):
from contentcuration.tasks import calculate_user_storage_task

key = CACHE_USER_STORAGE_KEY.format(user_id)
if key not in cache:
cache.set(key, True, timeout=600) # Invalidate after 10 minutes
calculate_user_storage_task.s(user_id).apply_async(countdown=5)
"""TODO: Perhaps move this to User model to avoid unnecessary User lookups"""
from contentcuration.models import User
try:
if user_id is None:
raise User.DoesNotExist
user = User.objects.get(pk=user_id)
get_or_create_async_task('calculate-user-storage', user, user_id=user_id)
except User.DoesNotExist:
logging.error("Tried to calculate user storage for user with id {} but they do not exist".format(user_id))