@ -35,7 +35,15 @@ import nacl.secret
import nacl . hash
import nacl . hash
from . import app_settings , permissions
from . import app_settings , permissions
from . models import Collection , CollectionItem , CollectionItemRevision , CollectionMember , CollectionInvitation , UserInfo
from . models import (
Collection ,
CollectionItem ,
CollectionItemRevision ,
CollectionMember ,
CollectionInvitation ,
Stoken ,
UserInfo ,
)
from . serializers import (
from . serializers import (
b64encode ,
b64encode ,
AuthenticationSignupSerializer ,
AuthenticationSignupSerializer ,
@ -94,18 +102,18 @@ class BaseViewSet(viewsets.ModelViewSet):
user = self . request . user
user = self . request . user
return queryset . filter ( members__user = user )
return queryset . filter ( members__user = user )
def get_cstoken_ rev ( self , request ) :
def get_cstoken_ obj ( self , request ) :
cstoken = request . GET . get ( ' cstoken ' , None )
cstoken = request . GET . get ( ' cstoken ' , None )
if cstoken is not None :
if cstoken is not None :
return get_object_or_404 ( CollectionItemRevisio n. objects . all ( ) , uid = cstoken )
return get_object_or_404 ( Stoke n. objects . all ( ) , uid = cstoken )
return None
return None
def filter_by_cstoken ( self , request , queryset ) :
def filter_by_cstoken ( self , request , queryset ) :
cstoken_id_field = self . cstoken_id_field + ' __id '
cstoken_id_field = self . cstoken_id_field + ' __id '
cstoken_rev = self . get_cstoken_ rev ( request )
cstoken_rev = self . get_cstoken_ obj ( request )
if cstoken_rev is not None :
if cstoken_rev is not None :
filter_by = { cstoken_id_field + ' __gt ' : cstoken_rev . id }
filter_by = { cstoken_id_field + ' __gt ' : cstoken_rev . id }
queryset = queryset . filter ( * * filter_by )
queryset = queryset . filter ( * * filter_by )
@ -116,7 +124,7 @@ class BaseViewSet(viewsets.ModelViewSet):
cstoken_id_field = self . cstoken_id_field + ' __id '
cstoken_id_field = self . cstoken_id_field + ' __id '
new_cstoken_id = queryset . aggregate ( cstoken_id = Max ( cstoken_id_field ) ) [ ' cstoken_id ' ]
new_cstoken_id = queryset . aggregate ( cstoken_id = Max ( cstoken_id_field ) ) [ ' cstoken_id ' ]
new_cstoken = new_cstoken_id and CollectionItemRevisio n. objects . get ( id = new_cstoken_id ) . uid
new_cstoken = new_cstoken_id and Stoke n. objects . get ( id = new_cstoken_id ) . uid
return queryset , new_cstoken
return queryset , new_cstoken
@ -139,7 +147,7 @@ class CollectionViewSet(BaseViewSet):
queryset = Collection . objects . all ( )
queryset = Collection . objects . all ( )
serializer_class = CollectionSerializer
serializer_class = CollectionSerializer
lookup_field = ' uid '
lookup_field = ' uid '
cstoken_id_field = ' items__revisions '
cstoken_id_field = ' items__revisions __stoken '
def get_queryset ( self , queryset = None ) :
def get_queryset ( self , queryset = None ) :
if queryset is None :
if queryset is None :
@ -199,7 +207,7 @@ class CollectionItemViewSet(BaseViewSet):
queryset = CollectionItem . objects . all ( )
queryset = CollectionItem . objects . all ( )
serializer_class = CollectionItemSerializer
serializer_class = CollectionItemSerializer
lookup_field = ' uid '
lookup_field = ' uid '
cstoken_id_field = ' revisions '
cstoken_id_field = ' revisions __stoken '
def get_queryset ( self ) :
def get_queryset ( self ) :
collection_uid = self . kwargs [ ' collection_uid ' ]
collection_uid = self . kwargs [ ' collection_uid ' ]
@ -290,8 +298,8 @@ class CollectionItemViewSet(BaseViewSet):
queryset , cstoken_rev = self . filter_by_cstoken ( request , queryset )
queryset , cstoken_rev = self . filter_by_cstoken ( request , queryset )
uids , stokens = zip ( * [ ( item [ ' uid ' ] , item . get ( ' stoken ' ) ) for item in serializer . validated_data ] )
uids , stokens = zip ( * [ ( item [ ' uid ' ] , item . get ( ' stoken ' ) ) for item in serializer . validated_data ] )
rev _id s = CollectionItemRevision . objects . filter ( uid__in= stokens , current = True ) . values_list ( ' id ' , flat = True )
rev s = CollectionItemRevision . objects . filter ( stoken__ uid__in= stokens , current = True )
queryset = queryset . filter ( uid__in = uids ) . exclude ( revisions__i d__i n= rev _id s)
queryset = queryset . filter ( uid__in = uids ) . exclude ( revisions__i n= rev s)
queryset , new_cstoken = self . get_queryset_cstoken ( queryset )
queryset , new_cstoken = self . get_queryset_cstoken ( queryset )
cstoken = cstoken_rev and cstoken_rev . uid
cstoken = cstoken_rev and cstoken_rev . uid