from django.shortcuts import render
from rest_framework import viewsets, routers, filters
from rest_framework.permissions import (
    SAFE_METHODS, BasePermission, IsAuthenticated
)

from vimma.models import Schedule
from vimma.perms import Perms
from vimma.util import has_perm, login_required_or_forbidden


class SchedulePermission(BasePermission):
    """
    Everyone can read Schedules, only users with permissions may write them.
    """
    def has_permission(self, request, view):
        if request.method in SAFE_METHODS:
            return True
        return has_perm(request.user, Perms.EDIT_SCHEDULE)

class ScheduleViewSet(viewsets.ModelViewSet):
    model = Schedule
    permission_classes = (IsAuthenticated, SchedulePermission,)


@login_required_or_forbidden
def index(request):
    """
    Homepage.
    """
    return render(request, 'vimma/index.html')


@login_required_or_forbidden
def test(request):
    """
    JavaScript Unit Tests.
    """
    return render(request, 'vimma/test.html')

@@ -742,16 +742,89 @@
 ssion,)%0A
+    filter_backends = (filters.OrderingFilter,)%0A    ordering = ('name',)%0A
 %0A%0A@login

order schedules by name in the API