From b260ca5417115ea36f6329256b5cd94db1054cc3 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 11:33:18 +0100 Subject: [PATCH 1/7] DRF 3.6.2 see #1078 --- modoboa/admin/api.py | 30 ++----------------- modoboa/admin/serializers.py | 5 ++-- .../core/commands/templates/settings.py.tpl | 3 +- modoboa/core/templates/core/api_access.html | 6 ++++ modoboa/urls.py | 5 +++- requirements.txt | 3 +- 6 files changed, 18 insertions(+), 34 deletions(-) diff --git a/modoboa/admin/api.py b/modoboa/admin/api.py index f7002bedb..27432a663 100644 --- a/modoboa/admin/api.py +++ b/modoboa/admin/api.py @@ -120,35 +120,9 @@ class AccountViewSet(viewsets.ModelViewSet): class AliasViewSet(viewsets.ModelViewSet): - """ViewSet for Alias. - - --- + """ create: - serializer: modoboa.admin.serializers.AliasSerializer - parameters: - - name: recipients - description: recipient(s) of the alias - required: true - type: array[string] - paramType: form - - update: - serializer: modoboa.admin.serializers.AliasSerializer - parameters: - - name: recipients - description: recipient(s) of the alias - required: true - type: array[string] - paramType: form - - partial_update: - serializer: modoboa.admin.serializers.AliasSerializer - parameters: - - name: recipients - description: recipient(s) of the alias - required: true - type: array[string] - paramType: form + Create a new alias instance. """ permission_classes = [IsAuthenticated, DjangoModelPermissions, ] diff --git a/modoboa/admin/serializers.py b/modoboa/admin/serializers.py index 5c6aee1b9..5a0608874 100644 --- a/modoboa/admin/serializers.py +++ b/modoboa/admin/serializers.py @@ -2,7 +2,7 @@ from django.core.exceptions import ValidationError from django.shortcuts import get_object_or_404 -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext as _, ugettext_lazy from rest_framework import serializers from passwords import validators @@ -317,7 +317,8 @@ class AliasSerializer(serializers.ModelSerializer): address = lib_fields.DRFEmailFieldUTF8AndEmptyUser() recipients = serializers.ListField( child=lib_fields.DRFEmailFieldUTF8AndEmptyUser(), - allow_empty=False) + allow_empty=False, + help_text=ugettext_lazy("A list of recipient")) class Meta: model = admin_models.Alias diff --git a/modoboa/core/commands/templates/settings.py.tpl b/modoboa/core/commands/templates/settings.py.tpl index 4d281cfe0..672df2db0 100644 --- a/modoboa/core/commands/templates/settings.py.tpl +++ b/modoboa/core/commands/templates/settings.py.tpl @@ -50,8 +50,8 @@ INSTALLED_APPS = ( 'reversion', 'ckeditor', 'ckeditor_uploader', + 'rest_framework', 'rest_framework.authtoken', - 'rest_framework_swagger', {% if devmode %} 'djangobower',{% endif %} ) @@ -167,6 +167,7 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media') REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', + 'rest_framework.authentication.SessionAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', diff --git a/modoboa/core/templates/core/api_access.html b/modoboa/core/templates/core/api_access.html index d0669a7b6..b2bae6495 100644 --- a/modoboa/core/templates/core/api_access.html +++ b/modoboa/core/templates/core/api_access.html @@ -2,6 +2,12 @@

{% trans "API access" %} {% trans "Control your access to Modoboa API" %}


+
+ {% url "api-docs:docs-index" as api_doc_url %} + {% blocktrans with url=api_doc_url trimmed %} + A documentation of the API is available here. + {% endblocktrans %} +
{% if user.auth_token %}
diff --git a/modoboa/urls.py b/modoboa/urls.py index db9662484..2d5eea481 100644 --- a/modoboa/urls.py +++ b/modoboa/urls.py @@ -5,11 +5,14 @@ from django.views.i18n import JavaScriptCatalog from django.contrib.auth.decorators import login_required from ckeditor_uploader import views as cku_views +from rest_framework.documentation import include_docs_urls from modoboa.admin.views.user import forward from modoboa.core.extensions import exts_pool from modoboa.core import signals as core_signals +API_TITLE = 'Modoboa API' + urlpatterns = [ url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'), url(r'^ckeditor/upload/', login_required(cku_views.upload), @@ -34,7 +37,7 @@ if extra_routes: # API urls urlpatterns += [ url("^api/v1/", include("modoboa.urls_api", namespace="external_api")), - url("^docs/api/", include('rest_framework_swagger.urls')), + url(r"^docs/api/", include_docs_urls(title=API_TITLE)) ] if settings.DEBUG: diff --git a/requirements.txt b/requirements.txt index 378632a09..ff96c3746 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,8 +8,7 @@ django-xforwardedfor-middleware dj-database-url -djangorestframework==3.5.3 -django-rest-swagger==0.3.10 +djangorestframework==3.6.2 argparse # bcrypt requires libffi-dev From 78da3240fb7dde1ccddc083f5e194cd12ce34c3a Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 12:08:10 +0100 Subject: [PATCH 2/7] Fixed test settings. see #1078 --- test_project/test_project/settings.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test_project/test_project/settings.py b/test_project/test_project/settings.py index 54f7f1d49..d19515f9a 100644 --- a/test_project/test_project/settings.py +++ b/test_project/test_project/settings.py @@ -45,6 +45,7 @@ INSTALLED_APPS = ( 'django.contrib.sites', 'django.contrib.staticfiles', 'reversion', + 'rest_framework', 'rest_framework.authtoken' ) @@ -141,6 +142,7 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media') REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', + 'rest_framework.authentication.SessionAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', From b9f789d8135e17575d70a5baa04afdfd79c71c76 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 12:21:16 +0100 Subject: [PATCH 3/7] Try to disable urls... --- modoboa/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modoboa/urls.py b/modoboa/urls.py index 2d5eea481..c70937674 100644 --- a/modoboa/urls.py +++ b/modoboa/urls.py @@ -37,7 +37,7 @@ if extra_routes: # API urls urlpatterns += [ url("^api/v1/", include("modoboa.urls_api", namespace="external_api")), - url(r"^docs/api/", include_docs_urls(title=API_TITLE)) + # url(r"^docs/api/", include_docs_urls(title=API_TITLE)) ] if settings.DEBUG: From d2f4dab7397a1e9e958f6777bf9a7c4d0f3565ab Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 13:58:33 +0100 Subject: [PATCH 4/7] Change include order. --- modoboa/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modoboa/urls.py b/modoboa/urls.py index c70937674..4e5686dba 100644 --- a/modoboa/urls.py +++ b/modoboa/urls.py @@ -36,8 +36,8 @@ if extra_routes: # API urls urlpatterns += [ + url("^docs/api/", include_docs_urls(title=API_TITLE)), url("^api/v1/", include("modoboa.urls_api", namespace="external_api")), - # url(r"^docs/api/", include_docs_urls(title=API_TITLE)) ] if settings.DEBUG: From f9ef3cb64936763ff745ba7f9289ca8d4bf544a9 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 16:11:58 +0100 Subject: [PATCH 5/7] Updated travis config. --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9b1fb2277..0a580df42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,9 +24,10 @@ before_install: - if [[ $DB = 'MYSQL' ]]; then pip install -q mysqlclient; fi install: - - python setup.py -q develop + - pip install -q -r requirements.txt - pip install -q -r ldap-requirements.txt - pip install -q -r test-requirements.txt + - pip install -e . before_script: - if [[ $DB = 'POSTGRESQL' ]]; then psql -c 'create database modoboa_test;' -U postgres; fi From 9374f9b30b15339062437fa1f09d208d731a2729 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 16:19:32 +0100 Subject: [PATCH 6/7] Updated requirements. --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index ff96c3746..5799aefdb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,8 @@ django-xforwardedfor-middleware dj-database-url +coreapi==2.3.0 +coreapi-cli==1.0.6 djangorestframework==3.6.2 argparse From becb32b795b189c90be0cd53171850dce0e18c01 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Thu, 16 Mar 2017 17:15:06 +0100 Subject: [PATCH 7/7] Updated documentation. --- doc/upgrade.rst | 23 +++++++++++++++---- .../core/commands/templates/settings.py.tpl | 18 --------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/doc/upgrade.rst b/doc/upgrade.rst index b8e272ecb..48c5b3170 100644 --- a/doc/upgrade.rst +++ b/doc/upgrade.rst @@ -87,12 +87,14 @@ Specific instructions 1.7.2 ===== -CKeditor is now embedded by default (thanks to the ``django-ckeditor`` -package) and some configuration changes are required to make it work. +API documentation has evolved (because of the upgrade to Django Rest +Framework 3.6) and CKeditor is now embedded by default (thanks to the +``django-ckeditor`` package). Some configuration changes are +required. Edit your :file:`settings.py` file and apply the following modifications: -* Add ``ckeditor`` and ``ckeditor_uploader`` to ``INSTALLED_APPS`` +* Update the ``INSTALLED_APPS`` variable as follows: .. sourcecode:: python @@ -106,10 +108,23 @@ Edit your :file:`settings.py` file and apply the following modifications: 'reversion', 'ckeditor', 'ckeditor_uploader', + 'rest_framework', 'rest_framework.authtoken', - 'rest_framework_swagger', ) +* Update the ``REST_FRAMEWORK`` variable as follows: + +.. sourcecode:: python + + REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework.authentication.TokenAuthentication', + 'rest_framework.authentication.SessionAuthentication', + ), + } + +* Remove the ``SWAGGER_SETTINGS`` variable + * Add the following content .. sourcecode:: python diff --git a/modoboa/core/commands/templates/settings.py.tpl b/modoboa/core/commands/templates/settings.py.tpl index 672df2db0..4f3b09009 100644 --- a/modoboa/core/commands/templates/settings.py.tpl +++ b/modoboa/core/commands/templates/settings.py.tpl @@ -169,24 +169,6 @@ REST_FRAMEWORK = { 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ), - 'DEFAULT_PERMISSION_CLASSES': ( - 'rest_framework.permissions.IsAuthenticated', - ) -} - -SWAGGER_SETTINGS = { - "is_authenticated": False, - "api_version": "1.0", - "exclude_namespaces": [], - "info": { - "contact": "contact@modoboa.com", - "description": ( - "Modoboa API, requires a valid token. To obtain a token, login " - "as super administrator, go to 'User settings > API' and enable " - "API access. Click on 'Update' and wait for page refresh." - ), - "title": "Modoboa API documentation", - } } # Modoboa settings