<gh_stars>0
""" Circles permission classes
"""

#Django REST framework
from rest_framework.permissions import BasePermission

#Models
from cride.circles.models import Membership

class IsActiveCircleMember(BasePermission):
	"""Allow access only to circle memebers
	Expect that views implementing this permission
	have a 'circle' attribute assigned
	"""

	def has_permission(self, request, view):
		#Verify user is an active member of the circle
		try:
			Membership.objects.get(
				user=request.user,
				circle=view.circle,
				is_active=True
			)
		except Membership.DoesNotExist:
			return False

		return True
	
class IsSelfMember(BasePermission):
	""" Allow access only to member owners"""

	def has_permission(self, request, view):
		#Let object permission grant access
		obj = view.get_object()
		return self.has_object_permission(request, view, obj)

	def has_object_permission(self, request, view, obj):
		#Allow access only if member is owned by the requesting user
		return request.user == obj.user
""" Circles permission classes
"""

#Django REST framework
from rest_framework.permissions import BasePermission

#Models
from cride.circles.models import Membership

class IsActiveCircleMember(BasePermission):
	"""Allow access only to circle memebers
	Expect that views implementing this permission
	have a 'circle' attribute assigned
	"""

	def has_permission(self, request, view):
		#Verify user is an active member of the circle
		try:
			Membership.objects.get(
				user=request.user,
				circle=view.circle,
				is_active=True
			)
		except Membership.DoesNotExist:
			return False

		return True
	
class IsSelfMember(BasePermission):
	""" Allow access only to member owners"""

	def has_permission(self, request, view):
		#Let object permission grant access
		obj = view.get_object()
		return self.has_object_permission(request, view, obj)

	def has_object_permission(self, request, view, obj):
		#Allow access only if member is owned by the requesting user
		return request.user == obj.user
Circles permission classes #Django REST framework #Models Allow access only to circle memebers 	Expect that views implementing this permission 	have a 'circle' attribute assigned #Verify user is an active member of the circle Allow access only to member owners #Let object permission grant access #Allow access only if member is owned by the requesting user