# Path: lepo/handlers.py
# class BaseHandler:
#     def __init__(self, request, args):
#         self.request = request
#         self.args = args
# 
#     @classmethod
#     def get_view(cls, method_name):
#         """
#         Get a Django function view calling the given method (and pre/post-processors)
# 
#         :param method_name: The method on the class
#         :return: View function
#         """
#         method = getattr(cls, method_name)
# 
#         @wraps(method)
#         def view(request, **kwargs):
#             handler = cls(request, kwargs)
#             handler.call_processors('view')
#             response = None
#             try:
#                 response = method(handler)
#                 return response
#             finally:
#                 handler.call_processors('post_view', response=response)
# 
#         return view
# 
#     def get_processors(self, purpose):
#         return getattr(self, f'{purpose}_processors', ())
# 
#     def call_processors(self, purpose, **kwargs):
#         for proc in self.get_processors(purpose):
#             if isinstance(proc, str):
#                 proc = getattr(self, proc, None) or import_string(proc)
#             kwargs['purpose'] = purpose
#             proc(**kwargs)


import pytest
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from lepo.handlers import BaseHandler



class EvalHandler(BaseHandler):
    view_processors = ['ensure_superuser']  # all views will pass through this

    def handle_eval(self):
        return eval(self.args['expression'])

    def ensure_superuser(self, purpose, **kwargs):
        if not self.request.user.is_superuser:
            raise PermissionDenied('oh no')


def test_eval_handler(rf, admin_user):
    eval_fn = EvalHandler.get_view('handle_eval')
    request = rf.post('/')
    request.user = AnonymousUser()

# Path: lepo/handlers.py
# class BaseHandler:
#     def __init__(self, request, args):
#         self.request = request
#         self.args = args
# 
#     @classmethod
#     def get_view(cls, method_name):
#         """
#         Get a Django function view calling the given method (and pre/post-processors)
# 
#         :param method_name: The method on the class
#         :return: View function
#         """
#         method = getattr(cls, method_name)
# 
#         @wraps(method)
#         def view(request, **kwargs):
#             handler = cls(request, kwargs)
#             handler.call_processors('view')
#             response = None
#             try:
#                 response = method(handler)
#                 return response
#             finally:
#                 handler.call_processors('post_view', response=response)
# 
#         return view
# 
#     def get_processors(self, purpose):
#         return getattr(self, f'{purpose}_processors', ())
# 
#     def call_processors(self, purpose, **kwargs):
#         for proc in self.get_processors(purpose):
#             if isinstance(proc, str):
#                 proc = getattr(self, proc, None) or import_string(proc)
#             kwargs['purpose'] = purpose
#             proc(**kwargs)


# Path: lepo_tests/tests/test_processor.py
import pytest
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from lepo.handlers import BaseHandler




class EvalHandler(BaseHandler):
    view_processors = ['ensure_superuser']  # all views will pass through this

    def handle_eval(self):
        return eval(self.args['expression'])

    def ensure_superuser(self, purpose, **kwargs):
        if not self.request.user.is_superuser:
            raise PermissionDenied('oh no')


def test_eval_handler(rf, admin_user):
    eval_fn = EvalHandler.get_view('handle_eval')
    request = rf.post('/')
    request.user = AnonymousUser()

    with pytest.raises(PermissionDenied):