From 179f7062cf1005c2ace6ea6ecd13b9887f43d081 Mon Sep 17 00:00:00 2001 From: Aaron Riedener Date: Sat, 4 Aug 2018 13:05:33 +0200 Subject: [PATCH] [Issue #n/a] tried to fix the travis conditional command again [Issue #190] Resolved issue by adding a setting and make the login_required decorator in the view conditional with this setting --- .travis.yml | 2 +- koalixcrm/crm/views/restinterface.py | 22 +++++++++---------- koalixcrm/globalSupportFunctions.py | 12 ++++++++++ projectsettings/settings/base_settings.py | 2 +- .../settings/development_settings.py | 4 +++- .../settings/development_sqlite_settings.py | 4 +++- projectsettings/settings/heroku_settings.py | 2 ++ .../settings/production_settings.py | 3 ++- 8 files changed, 35 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5e7e7709..1d26074e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ before_install: install: - python setup.py install - - pip install -r requirements/heroku_requirements.txte + - pip install -r requirements/heroku_requirements.txt script: - pytest --cov=koalixcrm --cov-report xml --cov-report term -m "not version_increase" diff --git a/koalixcrm/crm/views/restinterface.py b/koalixcrm/crm/views/restinterface.py index 0c898202..d1ace3c1 100644 --- a/koalixcrm/crm/views/restinterface.py +++ b/koalixcrm/crm/views/restinterface.py @@ -9,13 +9,15 @@ from koalixcrm.crm.documents.contract import Contract, ContractJSONSerializer from koalixcrm.crm.reporting.task import Task, TaskSerializer -from koalixcrm.crm.reporting.taskstatus import TaskStatus, TaskStatusJSONSerializer +from koalixcrm.crm.reporting.taskstatus import TaskStatus from koalixcrm.crm.reporting.project import Project, ProjectJSONSerializer from koalixcrm.crm.product.product import Product, ProductJSONSerializer from koalixcrm.crm.product.unit import Unit, UnitJSONSerializer from koalixcrm.crm.product.tax import Tax, TaxJSONSerializer from koalixcrm.crm.product.currency import CurrencyJSONSerializer, Currency from koalixcrm.crm.views.renderer import XSLFORenderer +from koalixcrm.globalSupportFunctions import ConditionalMethodDecorator +from django.conf import settings class TaskAsJSON(viewsets.ModelViewSet): @@ -27,7 +29,7 @@ class TaskAsJSON(viewsets.ModelViewSet): renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) filter_fields = ('project',) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(TaskAsJSON, self).dispatch(*args, **kwargs) @@ -40,7 +42,7 @@ class ContractAsJSON(viewsets.ModelViewSet): serializer_class = ContractJSONSerializer renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(ContractAsJSON, self).dispatch(*args, **kwargs) @@ -53,7 +55,7 @@ class TaskStatusAsJSON(viewsets.ModelViewSet): serializer_class = TaskSerializer renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(TaskStatusAsJSON, self).dispatch(*args, **kwargs) @@ -66,7 +68,7 @@ class CurrencyAsJSON(viewsets.ModelViewSet): serializer_class = CurrencyJSONSerializer renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(CurrencyAsJSON, self).dispatch(*args, **kwargs) @@ -79,7 +81,7 @@ class TaxAsJSON(viewsets.ModelViewSet): serializer_class = TaxJSONSerializer renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(TaxAsJSON, self).dispatch(*args, **kwargs) @@ -92,7 +94,7 @@ class UnitAsJSON(viewsets.ModelViewSet): serializer_class = UnitJSONSerializer renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(UnitAsJSON, self).dispatch(*args, **kwargs) @@ -105,7 +107,7 @@ class ProductAsJSON(viewsets.ModelViewSet): serializer_class = ProductJSONSerializer renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer) - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(ProductAsJSON, self).dispatch(*args, **kwargs) @@ -119,9 +121,7 @@ class ProjectAsJSON(viewsets.ModelViewSet): renderer_classes = (BrowsableAPIRenderer, JSONRenderer, XMLRenderer, XSLFORenderer) file_name = "this_is_the_ProjectList.xml" - - - @method_decorator(login_required) + @ConditionalMethodDecorator(method_decorator(login_required), settings.KOALIXCRM_REST_API_AUTH) def dispatch(self, *args, **kwargs): return super(ProjectAsJSON, self).dispatch(*args, **kwargs) diff --git a/koalixcrm/globalSupportFunctions.py b/koalixcrm/globalSupportFunctions.py index 9de83a7d..9168e155 100644 --- a/koalixcrm/globalSupportFunctions.py +++ b/koalixcrm/globalSupportFunctions.py @@ -21,3 +21,15 @@ def xstr(s): return "" else: return str(s) + + +class ConditionalMethodDecorator(object): + def __init__(self, dec, condition): + self.decorator = dec + self.condition = condition + + def __call__(self, func): + if not self.condition: + # Return the function unchanged, not decorated. + return func + return self.decorator(func) \ No newline at end of file diff --git a/projectsettings/settings/base_settings.py b/projectsettings/settings/base_settings.py index df348159..33b1a366 100644 --- a/projectsettings/settings/base_settings.py +++ b/projectsettings/settings/base_settings.py @@ -144,4 +144,4 @@ REST_FRAMEWORK = { 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',) -} \ No newline at end of file +} diff --git a/projectsettings/settings/development_settings.py b/projectsettings/settings/development_settings.py index d3b9ab90..c06fed70 100644 --- a/projectsettings/settings/development_settings.py +++ b/projectsettings/settings/development_settings.py @@ -15,4 +15,6 @@ } FOP_EXECUTABLE = "/usr/bin/fop" -GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' \ No newline at end of file +GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' + +KOALIXCRM_REST_API_AUTH = False \ No newline at end of file diff --git a/projectsettings/settings/development_sqlite_settings.py b/projectsettings/settings/development_sqlite_settings.py index 8fe65e5a..5335559a 100644 --- a/projectsettings/settings/development_sqlite_settings.py +++ b/projectsettings/settings/development_sqlite_settings.py @@ -14,4 +14,6 @@ } FOP_EXECUTABLE = "/usr/bin/fop-2.2/fop/fop" -GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' \ No newline at end of file +GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' + +KOALIXCRM_REST_API_AUTH = True \ No newline at end of file diff --git a/projectsettings/settings/heroku_settings.py b/projectsettings/settings/heroku_settings.py index 967324c1..57cc3463 100644 --- a/projectsettings/settings/heroku_settings.py +++ b/projectsettings/settings/heroku_settings.py @@ -17,3 +17,5 @@ FOP_EXECUTABLE = "/usr/bin/fop" GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' ALLOWED_HOSTS = ['koalix-crm.herokuapp.com'] + +KOALIXCRM_REST_API_AUTH = True diff --git a/projectsettings/settings/production_settings.py b/projectsettings/settings/production_settings.py index d3b9ab90..7af145d6 100644 --- a/projectsettings/settings/production_settings.py +++ b/projectsettings/settings/production_settings.py @@ -15,4 +15,5 @@ } FOP_EXECUTABLE = "/usr/bin/fop" -GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' \ No newline at end of file +GRAPPELLI_INDEX_DASHBOARD = 'projectsettings.dashboard.CustomIndexDashboard' +KOALIXCRM_REST_API_AUTH = True \ No newline at end of file