0001-cmis-add-cmis-connector-12876.patch
passerelle/apps/cmis/migrations/0001_initial.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('base', '0002_auto_20151009_0326'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.CreateModel( |
|
15 |
name='CmisConnector', |
|
16 |
fields=[ |
|
17 |
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), |
|
18 |
('title', models.CharField(max_length=50)), |
|
19 |
('slug', models.SlugField()), |
|
20 |
('description', models.TextField()), |
|
21 |
('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')])), |
|
22 |
('cmis_endpoint', models.CharField(help_text='URL of the CMIS endpoint', max_length=250, verbose_name='CMIS endpoint')), |
|
23 |
('login', models.CharField(help_text='Login for user on platform', max_length=128, verbose_name='Service login')), |
|
24 |
('password', models.CharField(help_text='Password for user on plateform', max_length=128, verbose_name='Password')), |
|
25 |
('users', models.ManyToManyField(to='base.ApiUser', blank=True)), |
|
26 |
], |
|
27 |
options={ |
|
28 |
'abstract': False, |
|
29 |
}, |
|
30 |
), |
|
31 |
] |
passerelle/apps/cmis/migrations/0002_auto_20160824_0314.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 |
import datetime |
|
6 |
from django.utils.timezone import utc |
|
7 | ||
8 | ||
9 |
class Migration(migrations.Migration): |
|
10 | ||
11 |
dependencies = [ |
|
12 |
('cmis', '0001_initial'), |
|
13 |
] |
|
14 | ||
15 |
operations = [ |
|
16 |
migrations.RemoveField( |
|
17 |
model_name='cmisconnector', |
|
18 |
name='login', |
|
19 |
), |
|
20 |
migrations.AddField( |
|
21 |
model_name='cmisconnector', |
|
22 |
name='username', |
|
23 |
field=models.CharField(default=datetime.datetime(2016, 8, 24, 8, 14, 37, 83586, tzinfo=utc), help_text='Username on DMS platform', max_length=128, verbose_name='Service username'), |
|
24 |
preserve_default=False, |
|
25 |
), |
|
26 |
migrations.AlterField( |
|
27 |
model_name='cmisconnector', |
|
28 |
name='password', |
|
29 |
field=models.CharField(help_text='Password on DMS platform', max_length=128, verbose_name='Password'), |
|
30 |
), |
|
31 |
] |
passerelle/apps/cmis/models.py | ||
---|---|---|
1 |
from cmislib import CmisClient |
|
2 |
from cmislib.model import PermissionDeniedException, ObjectNotFoundException, CmisException as CmisLibException |
|
3 |
import json |
|
4 |
import base64 |
|
5 |
import logging |
|
6 | ||
7 |
from django.db import models |
|
8 |
from passerelle.base.models import BaseResource |
|
9 |
from passerelle.utils.api import endpoint |
|
10 |
from django.utils.translation import ugettext_lazy as _ |
|
11 |
from django.http import HttpResponse |
|
12 | ||
13 |
logger = logging.getLogger(__file__) |
|
14 | ||
15 | ||
16 |
class CmisException(Exception): |
|
17 |
http_status = 200 |
|
18 |
log_error = False |
|
19 | ||
20 | ||
21 |
class CmisConnector(BaseResource): |
|
22 |
cmis_endpoint = models.CharField(max_length=250, verbose_name=_('CMIS endpoint'), |
|
23 |
help_text=_('URL of the CMIS endpoint')) |
|
24 |
username = models.CharField(max_length=128, verbose_name=_('Service username'), help_text=_('Username on DMS platform')) |
|
25 |
password = models.CharField(max_length=128, |
|
26 |
verbose_name=_('Password'), help_text=_('Password on DMS platform')) |
|
27 | ||
28 |
category = _('Business Process Connectors') |
|
29 | ||
30 |
@classmethod |
|
31 |
def get_icon_class(cls): |
|
32 |
return 'ressources' |
|
33 | ||
34 |
@endpoint(serializer_type='json-api') |
|
35 |
def upload_file(self, request, **kwargs): |
|
36 |
data = json.loads(request.body) |
|
37 | ||
38 |
title = data['filename'] |
|
39 |
path = data['path'] |
|
40 |
content = base64.decode(data['content']) |
|
41 |
content_type = data['contentType'] |
|
42 | ||
43 |
repo = self.cmis_connection() |
|
44 |
try: |
|
45 |
folder = repo.getObjectByPath(path) |
|
46 |
folder.createDocumentFromString(title, contentString=content, contentType=content_type) |
|
47 |
except ObjectNotFoundException: |
|
48 |
raise CmisException('Path not found on platform.') |
|
49 |
except Exception as e: |
|
50 |
raise CmisException(e.strerror) |
|
51 | ||
52 |
@endpoint(serializer_type='json-api') |
|
53 |
def download_file(self, request, path, **kwargs): |
|
54 |
repo = self.cmis_connection() |
|
55 |
try: |
|
56 |
doc = repo.getObjectByPath(path.encode('utf-8')) |
|
57 |
content_stream = doc.getContentStream() |
|
58 |
except ObjectNotFoundException: |
|
59 |
raise CmisException('Path not found on platform.') |
|
60 |
except CmisLibException: |
|
61 |
raise CmisException('Can\'t get the document stream content.') |
|
62 | ||
63 |
content = content_stream.read() |
|
64 |
contenttype = doc.properties['cmis:contentStreamMimeType'] |
|
65 |
filename = doc.name |
|
66 | ||
67 |
response = HttpResponse(content, content_type=contenttype) |
|
68 |
response['Content-Disposition'] = 'attachment; filename="%s"' % filename |
|
69 | ||
70 |
return response |
|
71 | ||
72 |
def cmis_connection(self): |
|
73 |
try: |
|
74 |
cmis_client = CmisClient(self.cmis_endpoint, self.username, self.password) |
|
75 |
repo = cmis_client.defaultRepository |
|
76 |
except PermissionDeniedException: |
|
77 |
raise CmisException('Wrong username or password to connect to platform.') |
|
78 |
except ObjectNotFoundException: |
|
79 |
raise CmisException('Platform endpoint not found.') |
|
80 | ||
81 |
return repo |
passerelle/settings.py | ||
---|---|---|
112 | 112 |
'base_adresse', |
113 | 113 |
'csvdatasource', |
114 | 114 |
'orange', |
115 |
'cmis', |
|
115 | 116 |
# backoffice templates and static |
116 | 117 |
'gadjo', |
117 | 118 |
) |
118 |
- |