Improved monkey patching setup.

And remove unused monkey patching functions.
remotes/origin/enhancement/email-actions
Andrey Antukh 2014-09-03 15:48:33 +02:00
parent ad7b231945
commit 50e4de966f
3 changed files with 32 additions and 39 deletions

View File

@ -0,0 +1,17 @@
# Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
# 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 <http://www.gnu.org/licenses/>.
default_app_config = "taiga.base.apps.BaseAppConfig"

View File

@ -13,10 +13,19 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
# Patch api view for correctly return 401 responses on # Patch api view for correctly return 401 responses on
# request is authenticated instead of 403 # request is authenticated instead of 403
from django.apps import AppConfig
from . import monkey from . import monkey
monkey.patch_api_view()
monkey.patch_serializer() class BaseAppConfig(AppConfig):
monkey.patch_import_module() name = "taiga.base"
verbose_name = "Base App Config"
def ready(self):
print("Monkey patching...", file=sys.stderr)
monkey.patch_restframework()
monkey.patch_serializer()

View File

@ -17,37 +17,6 @@
from __future__ import print_function from __future__ import print_function
import sys 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(): def patch_serializer():
from rest_framework import serializers from rest_framework import serializers
if hasattr(serializers.BaseSerializer, "_patched"): if hasattr(serializers.BaseSerializer, "_patched"):
@ -76,8 +45,6 @@ def patch_serializer():
serializers.BaseSerializer.to_native = to_native serializers.BaseSerializer.to_native = to_native
def patch_import_module(): def patch_restframework():
from django.utils import importlib as django_importlib from rest_framework import fields
import importlib fields.strip_multiple_choice_msg = lambda x: x
django_importlib.import_module = importlib.import_module