2013-03-20 06:26:35 +04:00
from django . http import HttpResponse
from django . views . decorators . csrf import csrf_exempt
from lib . main . models import *
from lib . main . serializers import *
2013-03-21 08:34:59 +04:00
from lib . main . rbac import *
2013-03-21 06:47:51 +04:00
from django . contrib . auth . models import AnonymousUser
2013-03-20 06:26:35 +04:00
from rest_framework import mixins
from rest_framework import generics
from rest_framework import permissions
2013-03-21 18:25:49 +04:00
import exceptions
2013-03-20 06:26:35 +04:00
class OrganizationsList ( generics . ListCreateAPIView ) :
model = Organization
serializer_class = OrganizationSerializer
permission_classes = ( CustomRbac , )
#def pre_save(self, obj):
# obj.owner = self.request.user
2013-03-21 07:14:09 +04:00
def get_queryset ( self ) :
2013-03-21 08:34:59 +04:00
2013-03-21 07:14:09 +04:00
if self . request . user . is_superuser :
2013-03-21 08:31:07 +04:00
return Organization . objects . filter ( active = True )
return Organization . objects . filter ( active = True , admins__in = [ self . request . user . application_user ] ) . distinct ( ) | \
Organization . objects . filter ( active = True , users__in = [ self . request . user . application_user ] ) . distinct ( )
2013-03-21 07:14:09 +04:00
2013-03-21 19:06:47 +04:00
def list_permissions_check ( self , request , obj = None ) :
if request . method == ' GET ' :
# everybody can call get, but it's filtered
return True
if request . method == ' POST ' :
# superusers have already been cleared, so deny regular users
return False
2013-03-21 18:25:49 +04:00
raise exceptions . NotImplementedError
2013-03-21 07:14:09 +04:00
2013-03-20 06:26:35 +04:00
class OrganizationsDetail ( generics . RetrieveUpdateDestroyAPIView ) :
model = Organization
serializer_class = OrganizationSerializer
permission_classes = ( CustomRbac , )
#def pre_save(self, obj):
# obj.owner = self.request.user
2013-03-21 19:06:47 +04:00
def item_permissions_check ( self , request , obj ) :
admin = request . user . application_user in obj . admins . all ( )
user = request . user . application_user in obj . users . all ( )
if request . method == ' GET ' :
return admin or user
if request . method == ' PUT ' :
return admin
2013-03-21 18:44:01 +04:00