From 50e4de966fed77d8fdde3096d0f773590b7badce Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 3 Sep 2014 15:48:33 +0200 Subject: [PATCH] Improved monkey patching setup. And remove unused monkey patching functions. --- taiga/base/__init__.py | 17 ++++++++++++++ taiga/base/{models.py => apps.py} | 15 +++++++++--- taiga/base/monkey.py | 39 +++---------------------------- 3 files changed, 32 insertions(+), 39 deletions(-) rename taiga/base/{models.py => apps.py} (76%) diff --git a/taiga/base/__init__.py b/taiga/base/__init__.py index e69de29b..d6006178 100644 --- a/taiga/base/__init__.py +++ b/taiga/base/__init__.py @@ -0,0 +1,17 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino +# Copyright (C) 2014 David Barragán +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +default_app_config = "taiga.base.apps.BaseAppConfig" diff --git a/taiga/base/models.py b/taiga/base/apps.py similarity index 76% rename from taiga/base/models.py rename to taiga/base/apps.py index 4bbadf1d..1b6c9027 100644 --- a/taiga/base/models.py +++ b/taiga/base/apps.py @@ -13,10 +13,19 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import sys # Patch api view for correctly return 401 responses on # request is authenticated instead of 403 +from django.apps import AppConfig from . import monkey -monkey.patch_api_view() -monkey.patch_serializer() -monkey.patch_import_module() + +class BaseAppConfig(AppConfig): + name = "taiga.base" + verbose_name = "Base App Config" + + def ready(self): + print("Monkey patching...", file=sys.stderr) + monkey.patch_restframework() + monkey.patch_serializer() + diff --git a/taiga/base/monkey.py b/taiga/base/monkey.py index fd2cbf2c..91d1be35 100644 --- a/taiga/base/monkey.py +++ b/taiga/base/monkey.py @@ -17,37 +17,6 @@ from __future__ import print_function import sys - -def patch_api_view(): - from rest_framework import views - from rest_framework import status, exceptions - from rest_framework.response import Response - - if hasattr(views, "_patched"): - return - - views._APIView = views.APIView - views._patched = True - - class APIView(views.APIView): - def handle_exception(self, exc): - if isinstance(exc, exceptions.NotAuthenticated): - return Response({'detail': 'Not authenticated'}, - status=status.HTTP_401_UNAUTHORIZED, - exception=True) - return super().handle_exception(exc) - - @classmethod - def as_view(cls, **initkwargs): - view = super(views._APIView, cls).as_view(**initkwargs) - view.cls_instance = cls(**initkwargs) - view.cls = cls - return view - - print("Patching APIView", file=sys.stderr) - views.APIView = APIView - - def patch_serializer(): from rest_framework import serializers if hasattr(serializers.BaseSerializer, "_patched"): @@ -76,8 +45,6 @@ def patch_serializer(): serializers.BaseSerializer.to_native = to_native -def patch_import_module(): - from django.utils import importlib as django_importlib - import importlib - - django_importlib.import_module = importlib.import_module +def patch_restframework(): + from rest_framework import fields + fields.strip_multiple_choice_msg = lambda x: x