from . import serializers
from content import models as content_models
from rest_framework import viewsets, mixins
from django.db.models import Q


class ImageSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, viewsets.ReadOnlyModelViewSet):
    serializer_class = serializers.Image
    filter_fields = ('content', 'creator', )

    def get_queryset(self):
        user = self.request.user
        content = content_models.Content.objects.permitted(user)
        return content_models.Image.objects.filter(Q(content__in=content) | Q(creator=user.gestalt)).order_by('-weight')

    def has_permission(self):
        if self.action == 'create':
            content_pk = self.request.data.get('content')
            if content_pk:
                content = content_models.Content.objects.get(pk=content_pk)
                return self.request.user.has_perm('content.create_image', content)
            return True
        elif self.action == 'list':
            return True
        elif self.action == 'retrieve':
            image = self.get_object()
            return self.request.user.has_perm('content.view_image', image)
        elif self.action == 'update':
            image = self.get_object()
            return self.request.user.has_perm('content.update_image', image)
        return False

@@ -451,16 +451,122 @@
 d(user)%0A
+        try:%0A            gestalt = user.gestalt%0A        except AttributeError:%0A            gestalt = None%0A
         
@@ -643,21 +643,16 @@
 creator=
-user.
 gestalt)

fix (another) error when users don’t have an associated gestalt