Created
May 28, 2010 14:53
-
-
Save ojii/417243 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BaseView(object): | |
| """ | |
| A base class to create class based views. | |
| It will automatically check allowed methods if a list of allowed methods are | |
| given. It also automatically tries to route to 'handle_`method`' methods if | |
| they're available. So if for example you define a 'handle_post' method and | |
| the request method is 'POST', this one will be called instead of 'handle'. | |
| """ | |
| def __init__(self, *args, **kwargs): | |
| # Preserve args and kwargs | |
| self._initial_args = args | |
| self._initial_kwargs = kwargs | |
| @property | |
| def __name__(self): | |
| """ | |
| INTERNAL: required by django | |
| """ | |
| return self.get_view_name() | |
| def __call__(self, request, *args, **kwargs): | |
| """ | |
| INTERNAL: Called by django when a request should be handled by this view. | |
| Creates a new instance of this class to sandbox | |
| """ | |
| handle_func_name = 'handle' | |
| sandbox = self.__class__(*self._initial_args, **self._initial_kwargs) | |
| return getattr(sandbox, handle_func_name)(request, *args, **kwargs) | |
| def get_view_name(self): | |
| """ | |
| Returns the name of this view | |
| """ | |
| return self.__class__.__name__ | |
| def handle(self): | |
| """ | |
| Write your view logic here | |
| """ | |
| raise NotImplementedError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment