From 96e4423a5e8f2f0b2ecc0c72874365e9a730c355 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Jaillet Date: Fri, 19 Aug 2016 10:36:28 +0200 Subject: [PATCH] cmis: add cmis connector to upload file (#12876) --- debian/control | 1 + passerelle/apps/cmis/__init__.py | 0 passerelle/apps/cmis/migrations/0001_initial.py | 31 ++++++++ passerelle/apps/cmis/migrations/__init__.py | 0 passerelle/apps/cmis/models.py | 82 ++++++++++++++++++++++ .../cmis/templates/cmis/cmisconnector_detail.html | 18 +++++ passerelle/apps/cmis/urls.py | 22 ++++++ passerelle/apps/cmis/views.py | 23 ++++++ passerelle/settings.py | 2 +- tests/test_cmis.py | 80 +++++++++++++++++++++ 10 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 passerelle/apps/cmis/__init__.py create mode 100644 passerelle/apps/cmis/migrations/0001_initial.py create mode 100644 passerelle/apps/cmis/migrations/__init__.py create mode 100644 passerelle/apps/cmis/models.py create mode 100644 passerelle/apps/cmis/templates/cmis/cmisconnector_detail.html create mode 100644 passerelle/apps/cmis/urls.py create mode 100644 passerelle/apps/cmis/views.py create mode 100644 tests/test_cmis.py diff --git a/debian/control b/debian/control index 8b918c5..1095544 100644 --- a/debian/control +++ b/debian/control @@ -22,6 +22,7 @@ Depends: ${python:Depends}, python-django-jsonfield, python-magic, python-suds, + python-cmislib, Recommends: python-soappy, python-phpserialize Suggests: python-sqlalchemy, python-mako Description: Uniform access to multiple data sources and services (Python module) diff --git a/passerelle/apps/cmis/__init__.py b/passerelle/apps/cmis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/passerelle/apps/cmis/migrations/0001_initial.py b/passerelle/apps/cmis/migrations/0001_initial.py new file mode 100644 index 0000000..9cf308e --- /dev/null +++ b/passerelle/apps/cmis/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0002_auto_20151009_0326'), + ] + + operations = [ + migrations.CreateModel( + name='CmisConnector', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('title', models.CharField(max_length=50)), + ('slug', models.SlugField()), + ('description', models.TextField()), + ('log_level', models.CharField(default=b'NOTSET', max_length=10, verbose_name='Log Level', choices=[(b'NOTSET', b'NOTSET'), (b'DEBUG', b'DEBUG'), (b'INFO', b'INFO'), (b'WARNING', b'WARNING'), (b'ERROR', b'ERROR'), (b'CRITICAL', b'CRITICAL'), (b'FATAL', b'FATAL')])), + ('cmis_endpoint', models.CharField(help_text='URL of the CMIS endpoint', max_length=250, verbose_name='CMIS endpoint')), + ('username', models.CharField(help_text='Username on DMS platform', max_length=128, verbose_name='Service username')), + ('password', models.CharField(help_text='Password on DMS platform', max_length=128, verbose_name='Password')), + ('users', models.ManyToManyField(to='base.ApiUser', blank=True)), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/passerelle/apps/cmis/migrations/__init__.py b/passerelle/apps/cmis/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/passerelle/apps/cmis/models.py b/passerelle/apps/cmis/models.py new file mode 100644 index 0000000..0fdd953 --- /dev/null +++ b/passerelle/apps/cmis/models.py @@ -0,0 +1,82 @@ +# passerelle.apps.cmis +# Copyright (C) 2016 Entr'ouvert +# +# 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 . + +from cmislib import CmisClient +from cmislib.model import PermissionDeniedException, ObjectNotFoundException, UpdateConflictException, CmisException as CmisLibException +import json +import base64 +import logging + +from django.db import models +from passerelle.base.models import BaseResource +from passerelle.utils.api import endpoint +from django.utils.translation import ugettext_lazy as _ +from django.http import HttpResponse + + +class CmisException(Exception): + http_status = 200 + log_error = False + + +class CmisConnector(BaseResource): + cmis_endpoint = models.CharField(max_length=250, verbose_name=_('CMIS endpoint'), + help_text=_('URL of the CMIS endpoint')) + username = models.CharField(max_length=128, verbose_name=_('Service username'), help_text=_('Username on DMS platform')) + password = models.CharField(max_length=128, + verbose_name=_('Password'), help_text=_('Password on DMS platform')) + + category = _('Business Process Connectors') + + @classmethod + def get_icon_class(cls): + return 'ressources' + + @endpoint(serializer_type='json-api', methods=['post'], perm='can_upload_file') + def upload_file(self, request, **kwargs): + data = json.loads(request.body) + + title = data['filename'] + path = data['path'].encode('utf-8') + content = base64.b64decode(data['content']) + content_type = data['contentType'] + + repo = self.cmis_connection() + try: + folder = repo.getObjectByPath(path) + except ObjectNotFoundException: + raise CmisException('Path not found on platform.') + except Exception as e: + raise CmisException(str(e)) + + try: + doc = folder.createDocumentFromString(title, contentString=content, contentType=content_type) + except UpdateConflictException: + raise CmisException('The document already exists on platform.') + except Exception as e: + raise CmisException(str(e)) + + return doc.properties + + def cmis_connection(self): + try: + cmis_client = CmisClient(self.cmis_endpoint, self.username, self.password) + except PermissionDeniedException: + raise CmisException('Wrong username or password to connect to platform.') + except ObjectNotFoundException: + raise CmisException('Platform endpoint not found.') + + return cmis_client.defaultRepository diff --git a/passerelle/apps/cmis/templates/cmis/cmisconnector_detail.html b/passerelle/apps/cmis/templates/cmis/cmisconnector_detail.html new file mode 100644 index 0000000..3d2acbd --- /dev/null +++ b/passerelle/apps/cmis/templates/cmis/cmisconnector_detail.html @@ -0,0 +1,18 @@ +{% extends "passerelle/manage/service_view.html" %} +{% load i18n passerelle %} + +{% block endpoints %} +

+{% blocktrans %} +The API currently doesn't support all parameters and is limited to the JSON +format. +{% endblocktrans %} +

+{% endblock %} + +{% block security %} +

+{% trans 'Upload is limited to the following API users:' %} +

+{% access_rights_table resource=object permission='can_upload_file' %} +{% endblock %} \ No newline at end of file diff --git a/passerelle/apps/cmis/urls.py b/passerelle/apps/cmis/urls.py new file mode 100644 index 0000000..e304413 --- /dev/null +++ b/passerelle/apps/cmis/urls.py @@ -0,0 +1,22 @@ +# passerelle.apps.cmis +# Copyright (C) 2016 Entr'ouvert +# +# 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 . +from django.conf.urls import patterns, url + +from .views import * + +urlpatterns = patterns('', + url(r'^(?P[\w-]+)/$', CmisConnectorDetailView.as_view(), + name='cmisconnector-view')) \ No newline at end of file diff --git a/passerelle/apps/cmis/views.py b/passerelle/apps/cmis/views.py new file mode 100644 index 0000000..cae952d --- /dev/null +++ b/passerelle/apps/cmis/views.py @@ -0,0 +1,23 @@ +# passerelle.apps.cmis +# Copyright (C) 2016 Entr'ouvert +# +# 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 . +from django.views.generic import DetailView + +from .models import CmisConnector + + +class CmisConnectorDetailView(DetailView): + model = CmisConnector + template_name = 'cmis/cmisconnector_detail.html' \ No newline at end of file diff --git a/passerelle/settings.py b/passerelle/settings.py index 2830f08..33be2bc 100644 --- a/passerelle/settings.py +++ b/passerelle/settings.py @@ -113,7 +113,7 @@ INSTALLED_APPS = ( 'csvdatasource', 'orange', 'family', - # backoffice templates and static + 'cmis', 'gadjo', ) diff --git a/tests/test_cmis.py b/tests/test_cmis.py new file mode 100644 index 0000000..69015fa --- /dev/null +++ b/tests/test_cmis.py @@ -0,0 +1,80 @@ +import pytest +import mock +import base64 +import ast + +from cmis.models import CmisConnector +from django.contrib.contenttypes.models import ContentType +from passerelle.base.models import ApiUser, AccessRight +from django.core.urlresolvers import reverse + + +@pytest.fixture() +def setup(db): + api = ApiUser.objects.create(username='all', keytype='', key='') + + conn = CmisConnector.objects.create(cmis_endpoint='http://cmis_endpoint.com/cmis', username='admin', password='admin', slug='cmis') + obj_type = ContentType.objects.get_for_model(conn) + + AccessRight.objects.create(codename='can_access', apiuser=api, + resource_type=obj_type, resource_pk=conn.pk) + + return conn + +@pytest.fixture() +def url(): + return reverse('generic-endpoint', kwargs={ + 'connector': 'cmis', 'slug': 'cmis', 'endpoint': 'upload_file'}) + + +fake_file = { + 'filename': 'test.txt', + 'path': '/a/path', + 'content': base64.b64encode('fake file content.'), + 'contentType': 'text/plain' +} + + +class MockedCmisDocument(mock.Mock): + + @property + def properties(self): + return { + 'cmis:baseTypeId': 'cmis:document', + 'cmis:contentStreamFileName': self.title, + 'cmis:contentStreamMimeType': self.contentType, + } + + +class MockedCmisFolder(mock.Mock): + + def createDocumentFromString(self, title, contentString, contentType): + return MockedCmisDocument(title=title, contentString=contentString, contentType=contentType) + + +class MockedCmisRepository(mock.Mock): + + def getObjectByPath(self, path): + return MockedCmisFolder() + + +class MockedCmisClient(mock.Mock): + + @property + def defaultRepository(self): + return MockedCmisRepository() + +@mock.patch('cmis.models.CmisClient') +def test_cmis_upload(mock_connection, app, setup, url): + mock_connection.return_value = MockedCmisClient() + response = app.post_json(url, fake_file, status=200) + body = ast.literal_eval(response.body) + assert 'data' in body + assert body['err'] == 0 + data = body['data'] + assert 'cmis:baseTypeId' in data + assert 'cmis:contentStreamFileName' in data + assert 'cmis:contentStreamMimeType' in data + assert data['cmis:baseTypeId'] == 'cmis:document' + assert data['cmis:contentStreamFileName'] == fake_file['filename'] + assert data['cmis:contentStreamMimeType'] == fake_file['contentType'] \ No newline at end of file -- 2.8.1