
from rest_framework.permissions import BasePermission, SAFE_METHODS


class AllowAnyCreateOrIsAuthenticated(BasePermission):
    """
    The request is authenticated as a user, or is a read-only request.
    """

    def has_permission(self, request, view):
        if request.method == 'POST' or request.user.is_authenticated:
            return True
        return False


class IsSelfUserOrReadOnly(BasePermission):

    def has_object_permission(self, request, view, obj):
        # Read permissions are allowed to any request,
        # so we'll always allow GET, HEAD or OPTIONS requests.
        if request.method in SAFE_METHODS:
            return True

        return obj == request.user
