Commit ae6c675f authored by Mirko Vucicevich's avatar Mirko Vucicevich

remove the used in slideshow list to speed things up

parent 45b1d43f
......@@ -15,3 +15,4 @@ pyvenv.cfg
from layers.models import GroupMembership, ContentGroup
from django.shortcuts import get_object_or_404
from django.db.models import When, Case, BooleanField
def item_permission_queryset(user, queryset):
return queryset.annotate(
When(contentgroup__groupmembership__can_edit_items=True, contentgroup__groupmembership__user=user, then=True),
When(contentgroup__groupmembership__can_use_items=True, contentgroup__groupmembership__user=user, then=True),
def get_item_permissions(user, item):
perms = {'own': False, 'share': False, 'edit': False, 'delete': False, 'use': False}
......@@ -5,6 +5,17 @@ from jinja2 import evalcontextfilter, Markup, escape
register = template.Library()
perms_list = ['own', 'edit', 'delete', 'share']
def determine_perms(item, user):
if item.owner_id ==
return {x: True for x in perms_list}
if hasattr(item, 'perms__use'):
return {x: getattr(item, 'perm__{}'.format(x), False) for x in perms_list}
return get_item_permissions(user, item)
@register.inclusion_tag('layers/layer_list_fav.html', takes_context=True)
def layer_item_fav(context, layer, mode='manage'):
......@@ -35,7 +46,7 @@ def slide_item(context, slide):
'user': context['user'],
'slide': slide,
'mode': context['mode'],
'perms': get_item_permissions(context['user'], slide)
'perms': determine_perms(slide, context['user'])
......@@ -6,12 +6,12 @@ from django.views.decorators.clickjacking import xframe_options_exempt
from django.views.decorators.vary import vary_on_cookie
from .models import Slide, Slideshow, InputType, Version, Layer, Control,\
SlideLayer, FavoriteLayer, UploadedImage, Screen, GroupMembership,\
CachedData, UploadedVideo, LayerRating, VersionFeedback
CachedData, UploadedVideo, LayerRating, VersionFeedback, ContentGroup
from django.contrib.auth.models import User
from django.urls import reverse
from django.db.models import Count, Q, Max, F
from django.db.models import Count, Q, Max, F, Prefetch, Value, CharField
from .forms import *
from .functions import get_item_permissions, group_perms
from .functions import get_item_permissions, group_perms, item_permission_queryset
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from django.forms.models import inlineformset_factory
......@@ -708,9 +708,10 @@ def manageSlides(request):
def listSlides(request, mode):
slides = Slide.objects.filter(owner=request.user).order_by('-edit_date')
slides = Slide.objects.filter(owner=request.user).order_by('-edit_date').select_related('owner')
slides = item_permission_queryset(request.user, slides)
slideshow_ids = []
# get list of corresponding slideshows per slide
for slide in slides:
slideshows = SlideshowSlide.objects.filter(
......@@ -725,8 +726,25 @@ def listSlides(request, mode):
slide.slideshow_part1 = slideshow_list[:min(3, len(slideshow_list))]
if len(slideshow_list) >= 3: slide.slideshow_part2 = slideshow_list[3:]
groups = request.user.contentgroup_set.all() | request.user.owned_groups.all()
groups = groups.distinct().annotate(c=Count('slides')).filter(c__gt=0)
failed attempt at optimization
groupslides = Slide.objects\
groupslides = item_permission_queryset(request.user, groupslides)
groups = ContentGroup.objects.filter(Q(owner=request.user)|Q(members=request.user)).distinct()\
return render(request, 'slides/slide_list.html', {'slides': slides, 'groups': groups, 'mode': mode})
......@@ -26,6 +26,7 @@
Collapse All <i class="fa fa-chevron-up"></i>
{% for group in groups %}
<div class="panel panel-default">
<div class="panel-heading" data-toggle="collapse" data-target="#{{}}">
......@@ -20,11 +20,14 @@
(by: <span class="owner">{% if slide.owner == user %}Me{% else %} {{slide.owner.username}}{% endif %}</span>)
{% load slide_tags %}
Used in Slideshows:
{% comment %}
This currently causes too many DB calls.
We can probably improve this with a nice prefetch or something
<a href="#!" data-toggle="popover" data-trigger="focus" title="Slideshows" data-html = "true" style="display: inline; cursor: pointer;"
data-content="{% slide_item_slideshow slide %}</div>">
{% endcomment %}
Last Edited: {{slide.edit_date|date:"Y/m/d"}} by {% if slideshow.last_edit %}{{slideshow.last_edit}}{% else %}{{slide.owner.username}}{% endif %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment