w
clone your own copy | download snapshot

Snapshots | iceberg

No images in this repository’s iceberg at this time

Inside this repository

views.py
text/x-python

Download raw (6.3 KB)

from django.views.generic.base import TemplateView
from rest_framework import viewsets, permissions, authentication
from rest_framework.pagination import PageNumberPagination
from rest_framework.parsers import FormParser, MultiPartParser, JSONParser
from rest_framework.filters import OrderingFilter, SearchFilter, DjangoObjectPermissionsFilter
from rest_framework.decorators import action
from django_filters import BooleanFilter
from django_filters import rest_framework as filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from .models import Attachment, Score, FeaturedScore
from .serializers import AttachmentSerializer, ScoreSerializer, ScoreLightSerializer, UserSerializer
from guardian.shortcuts import get_anonymous_user
from django.contrib.auth.models import User
from guardian.shortcuts import get_objects_for_user
from rest_framework.response import Response
from collections import OrderedDict
from django.db import models
from playground.models import Score
from taggit.models import Tag




class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (SearchFilter,)
    search_fields = ('username',)


class AttachmentViewSet(viewsets.ModelViewSet):
    queryset = Attachment.objects.all()
    serializer_class = AttachmentSerializer
    parser_classes = (MultiPartParser, FormParser, JSONParser)

    def perform_create(self, serializer):
        serializer.save(attachment=self.request.data.get('attachment'))


class ScoreViewSetPagination(PageNumberPagination):
    page_size_query_param = 'page_size'


class TagsFilter(filters.CharFilter):
    def filter(self, qs, value):
        if value:
            tags = [tag.strip() for tag in value.split(',')]
            qs = qs.filter(tags__name__in=tags).distinct()

        return qs


class ScoreFilter(FilterSet):
    is_featured = BooleanFilter()
    shared_with = BooleanFilter()
    tags = TagsFilter()

    class Meta:
        model = Score
        fields = ('title', 'score_type', 'is_featured', 'shared_with', 'language', 'tags')


class ScoreAuthentication(authentication.SessionAuthentication):
    def authenticate(self, request):
        """
        Returns a `User` if the request session currently has a logged in user.
        Otherwise returns AnonymousUser if no user is supplied, or `None` if
        user is inactive.
        """

        # Get the session-based user from the underlying HttpRequest object
        user = getattr(request._request, 'user', None)

        # Unauthenticated, CSRF validation not required
        if not user:
            return (get_anonymous_user(), None)
        elif not user.is_active:
            return None

        self.enforce_csrf(request)

        # CSRF passed with authenticated user
        return (user, None)


class ScorePermissions(permissions.DjangoObjectPermissions):
    """
    Similar to `DjangoObjectPermissions`, but adding 'view' permissions.
    """
    # authenticated_users_only = False

    perms_map = {
        'GET': ['%(app_label)s.view_%(model_name)s'],
        'OPTIONS': ['%(app_label)s.view_%(model_name)s'],
        'HEAD': ['%(app_label)s.view_%(model_name)s'],
        'POST': ['%(app_label)s.add_%(model_name)s'],
        'PUT': ['%(app_label)s.change_%(model_name)s'],
        'PATCH': ['%(app_label)s.change_%(model_name)s'],
        'DELETE': ['%(app_label)s.delete_%(model_name)s'],
    }


class ScoreViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    _ignore_model_permissions = True # Seems essential to django guardian
    queryset = Score.objects.all()
    pagination_class = ScoreViewSetPagination
    filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter, DjangoObjectPermissionsFilter)
    authentication_classes = (ScoreAuthentication,)
    permission_classes = (ScorePermissions,)
    search_fields = ('title',)
    # filter_fields = ('title', 'score_type')
    filter_class = ScoreFilter

    @action(detail=False)
    def score_type(self, request):
        Score.SCORE_TYPE_CHOICES 
        data = self.filter_queryset(self.queryset).order_by("score_type").values("score_type").annotate(n=models.Count("pk")).order_by('-n')
        data = [{'value': i['score_type'], 'label': dict(Score.SCORE_TYPE_CHOICES).get(i['score_type']), 'n': i['n']} for i in data]
        return Response(data)

    @action(detail=False)
    def genres(self, request):
        data = self.filter_queryset(self.queryset).order_by("genre").values("genre").annotate(n=models.Count("pk")).order_by('-n')
        data = [{'value': i['genre'], 'label': i['genre'], 'n': i['n']} for i in data]
        return Response(data)

    @action(detail=False)
    def tags(self, request):
        ids = self.filter_queryset(self.queryset).order_by("tags").values_list("id", flat=True)
        data = Tag.objects.filter(score__id__in=ids).values("name").annotate(n=models.Count("score__pk")).order_by('-n')
        data = [{'value': i['name'], 'label': i['name'], 'n': i['n']} for i in data]
        return Response(data)


    @action(detail=False)
    def language(self, request):
        data = self.filter_queryset(self.queryset).order_by("language").values("language").annotate(n=models.Count("pk")).order_by('-n')
        data = [{'value': i['language'], 'label': i['language'], 'n': i['n']} for i in data if i['language']]
        return Response(data)

    def get_queryset(self):
        """
        Filter featured scores
        """
        queryset = super(ScoreViewSet, self).get_queryset()

        shared_with = self.request.query_params.get('shared_with', None)
        if shared_with is not None:
            queryset = get_objects_for_user(self.request.user, 'playground.view_score')

        is_featured = self.request.query_params.get('is_featured', None)
        if is_featured is not None:
            ids = FeaturedScore.objects.all().values_list("score__id", flat=True)
            queryset = queryset.filter(id__in=ids)

        return queryset

    def get_serializer_class(self, *args, **kwargs):
        """
        Expose a lighter set of fields when a list is required
        """
        if self.action in ['list']:
            return ScoreLightSerializer
        else:
            return ScoreSerializer
    

class ScoreView(TemplateView):
    """
    """
    template_name = "playground/score.html"