Gitlab CSE Unil

Commit 55497216 authored by Julien Furrer's avatar Julien Furrer
Browse files

Implemented api for getting shared annotations

The shared annotations list respect the publishing
level defined by the user (owner or public)
parent fe27cc43
......@@ -2,21 +2,34 @@
from __future__ import unicode_literals
from django.conf.urls import url, patterns, include
from rest_framework import routers
from rest_framework.routers import DefaultRouter
from rest_framework_nested import routers
from adim.serializers import SharedAnObjSerializer
from adim.views import AnObjViewSet, SharedAnObjViewSet, AnnotationViewSet, UserViewSet
from adim.views import AnObjViewSet, SharedAnObjViewSet, AnnotationViewSet, SharedAnnotationViewSet, UserViewSet
# Default router, as wee need the 'root-api' name
d_router = DefaultRouter()
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router = routers.SimpleRouter()
router.register(r'anobjs', AnObjViewSet)
router.register(r'shared/anobjs', SharedAnObjViewSet, base_name='shared-anobj')
router.register(r'annotations', AnnotationViewSet)
router.register(r'users', UserViewSet, base_name='users')
annot_router = routers.NestedSimpleRouter(router, r'anobjs', lookup='anobjs')
annot_router.register(r'shared/annotations', SharedAnnotationViewSet, base_name='shared-annotations')
sh_annot_router = routers.NestedSimpleRouter(router, r'shared/anobjs', lookup='anobjs')
sh_annot_router.register(r'shared/annotations', SharedAnnotationViewSet, base_name='shared-annotations-2')
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browseable API.
urlpatterns = patterns('',
url(r'^$', d_router.get_api_root_view(), name='api-root'),
url(r'^', include(router.urls)),
url(r'^', include(annot_router.urls)),
url(r'^', include(sh_annot_router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
\ No newline at end of file
......@@ -13,7 +13,8 @@ from adim.serializers import AnObjSerializer, SharedAnObjSerializer, AnObjListSe
from adim.permissions import has_anobj_access
from rest_framework import generics, viewsets
from rest_framework.response import Response
from rest_framework.decorators import api_view, action, detail_route
from rest_framework.decorators import detail_route
from rest_framework.views import APIView
from rest_framework.parsers import FileUploadParser
......@@ -169,8 +170,8 @@ class AnnotationViewSet(viewsets.ModelViewSet):
queryset = Annotation.objects.filter(owner=self.request.user)
return queryset
def list(self, request):
return Response([])
# def list(self, request):
# return Response([])
def pre_save(self, obj):
"""
......@@ -182,7 +183,11 @@ class AnnotationViewSet(viewsets.ModelViewSet):
# if not obj.annotable.locked:
# raise Exception("Annotable locked")
if obj.id is None:
if obj.id:
if obj.owner != self.request.user:
raise Exception("Annotable access forbidden")
else:
# user = self.request.user
# anobj_q = Q(pk=obj.annotable_id) & (Q(owner=user) | Q(members=user))
try:
......@@ -199,7 +204,7 @@ class AnnotationViewSet(viewsets.ModelViewSet):
super(AnnotationViewSet, self).pre_save(obj)
class SharedAnnotationViewSet(viewsets.ModelViewSet):
class SharedAnnotationViewSet(viewsets.ReadOnlyModelViewSet):
"""
"""
......@@ -208,8 +213,20 @@ class SharedAnnotationViewSet(viewsets.ModelViewSet):
def get_queryset(self):
"""
Return the shared annotations for this anobj
anots = anobj.annotations.filter(owner__anobjmembership__publish_mode__gte=<publish_level>, owner__anobjmembership__anobj=anobj).select_related('owner')
"""
anobj = get_object_or_404(AnObj, pk=self.kwargs.get('anobjs_pk', -1))
publish_level = 1 if self.request.user == anobj.owner else 2
q = (
Q(annotable=anobj) &
Q(owner__anobjmembership__publish_mode__gte=publish_level) &
Q(owner__anobjmembership__anobj=anobj)
)
return Annotation.objects.filter(q).exclude(owner=self.request.user)
......
Supports Markdown
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