from rest_framework.permissions import BasePermission


class IsOwner(BasePermission):
    def has_object_permission(self, request, view, obj):
        if hasattr(obj, "user"):
            return obj.user == request.user


class IsPostOwner(BasePermission):
    def has_object_permission(self, request, view, obj):
        is_category_user = False
        is_rule_user = False
        rule = obj.rule

        if rule and rule.user:
            is_rule_user = bool(rule.user == request.user)

            if rule.category and rule.category.user:
                is_category_user = bool(rule.category.user == request.user)
                return bool(is_category_user and is_rule_user)

        return is_rule_user
