def add_or_remove_folder_task(folder_id, task_id, add): """Add or remove given task from the given folder.""" try: foldertask = FolderTask.objects.get( folder_id=folder_id, task_id=task_id) except: foldertask = None if foldertask and not add: foldertask.delete() changed = True elif not foldertask and add: info = FolderTask.objects.filter(folder_id=folder_id) \ .aggregate(Max('position'), Count('id')) position = max(info['position__max'], info['id__count']) + 1 FolderTask.objects.create( folder_id=folder_id, task_id=task_id, position=position) changed = True else: changed = False if changed: ncache.invalidate_namespace( FOLDER_NAMESPACE_FORMAT_ID.format(folder_id))
from django.conf import settings from django.db.models.signals import post_save from django.dispatch import receiver from folder.models import FOLDER_NAMESPACE_FORMAT_ID, Folder, FolderTask from task.models import Task from skoljka.libs.decorators import cache_function, \ get_cache_function_full_key from skoljka.libs import ncache FEATURED_FOLDER_ID = getattr(settings, 'FEATURED_LECTURES_FOLDER_ID', None) FEATURED_LECTURES_FOLDER_NAMESPACE = \ FOLDER_NAMESPACE_FORMAT_ID.format(FEATURED_FOLDER_ID) def can_edit_featured_lectures(user): """Check if the given user can edit featured lectures.""" return user.is_staff @cache_function(namespace=FEATURED_LECTURES_FOLDER_NAMESPACE) def _get_featured_lectures_cached(): try: folder = Folder.objects.get(id=FEATURED_FOLDER_ID) except Folder.DoesNotExist: return [] # This is the homepage, don't just throw exceptions. # Remove all hidden task. Featured lectured should work as fast as # possible, and we don't want to cache this for each user separately. tasks = list(folder.get_queryset(None, no_perm_check=True)) if any(task.hidden for task in tasks): # TODO: log
def invalidate_cache_for_folder_ids(folder_ids): """ Something to avoid, or be extremely careful with. """ namespaces = [FOLDER_NAMESPACE_FORMAT_ID.format(x) for x in folder_ids] ncache.invalidate_namespaces(namespaces)