"""This module holds the apiv2 permissions for ``photostories`` app."""

from rest_framework import permissions


class IsOwnerToEditStrip(permissions.BasePermission):
    """Allows access only to the owner for edition method."""

    RESTRICTED_METHODS = ['POST', 'DELETE', 'PATCH', 'PUT']
    message = "You must be the owner of this strip to perform this action."

    def has_object_permission(self, request, view, obj):  # pylint: disable=unused-argument
        """Only the owner of this strip can edit or delete it

        For the parameters, see ``rest_framework.permissions.BasePermission``"""

        if request.method in self.RESTRICTED_METHODS:
            return obj.owner == request.user
        return True


class IsStripOwnerToEditPhoto(permissions.BasePermission):
    """Allows access only to the owner for edition method."""

    RESTRICTED_METHODS = ['POST', 'DELETE', 'PATCH', 'PUT']
    message = "You must be the owner of the strip to perform this action."

    def has_permission(self, request, view):  # pylint: disable=unused-argument
        """Only the owner of this strip can edit photos

        For the parameters, see ``rest_framework.permissions.BasePermission``"""

        if request.method in self.RESTRICTED_METHODS:
            return view.strip.owner == request.user
        return True
