From 9ef9efdad9331159207e89d919e41a9731a690a6 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Fri, 15 May 2015 15:25:35 +0200 Subject: [PATCH] add POST on /resource/ --- passerelle_maarch/views.py | 85 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/passerelle_maarch/views.py b/passerelle_maarch/views.py index 7ce2bee..436ab91 100644 --- a/passerelle_maarch/views.py +++ b/passerelle_maarch/views.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import json +import urlparse import requests from suds.client import Client @@ -21,6 +23,8 @@ from django.core.urlresolvers import reverse from django.views.generic import DetailView from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.core.cache import cache +from django.utils.decorators import method_decorator +from django.views.decorators.csrf import csrf_exempt from passerelle import utils @@ -78,6 +82,10 @@ class MaarchPingView(MaarchDetailView): class MaarchResourceView(MaarchDetailView): + @method_decorator(csrf_exempt) + def dispatch(self, *args, **kwargs): + return super(MaarchResourceView, self).dispatch(*args, **kwargs) + def get_data(self, request, resource_id=None, table_name=None, adr_table_name=None, *args, **kwargs): client = self.get_client() if resource_id: @@ -94,6 +102,83 @@ class MaarchResourceView(MaarchDetailView): results = client.service.Demo_searchResources(searchParams) return recursive_asdict(results) + @utils.protected_api('can_access') + def post(self, request, *args, **kwargs): + client = self.get_client() + formdata = json.loads(request.body) + url = formdata['url'] + + # get formdef schema + p = urlparse.urlsplit(url) + scheme, netloc, path, query, fragment = \ + p.scheme, p.netloc, p.path, p.query, p.fragment + schema_path = path.rsplit('/', 2)[0] + '/schema' + schema_url = urlparse.urlunsplit((scheme, netloc, schema_path, query, fragment)) + schema = requests.get(schema_url).json() + + # build document + attachments = [] + document = u'' + document += u'

%s

' % schema['name'] + for field in schema['fields']: + part = '' + if field['type'] == 'page': + part = u'

%s

' % field['label'] + elif field['type'] == 'title': + part = u'

%s

' % field['label'] + elif 'varname' in field: + part = u'
' + part += u'
%s
' % field['label'] + value = formdata['fields'].get(field['varname'], '') + if value and field['type'] == 'file': + attachments.append(value) + value = '%s' % value['filename'] + if value is None: + value = '---' + part += u'
%s
' % value + part += u'
' + document += part + document += '' + encodedFile = document.encode('utf-8').encode('base64') + fileFormat = 'html' + + # Maarch metadata + options = schema.get('options', {}) + status = options.get('status') or 'ATT' # ATT->QualificationBasket + subject = client.factory.create('arrayOfDataContent') + subject.column = 'subject' + subject.value = formdata['display_name'] + subject.type = 'string' + maarch_meta = [subject] + for meta in ('initiator', 'type_id', 'destination', 'priority', 'dest_user', 'typist'): + if meta in options: + soapmeta = client.factory.create('arrayOfDataContent') + soapmeta.column = meta + soapmeta.value = options[meta] + soapmeta.type = 'string' + maarch_meta.append(soapmeta) + metadata = client.factory.create('arrayOfData') + metadata.value = maarch_meta + + # Maarch target is letterbox + collId = 'letterbox_coll' + table = 'res_letterbox' + + # store resource + results = client.service.storeResource(encodedFile, metadata, collId, + table, fileFormat, status) + data = recursive_asdict(results) + resId = data['resId'] + + # if resId exists, store attachments + if resId: + for attachment in attachments: + fileFormat = attachment['content_type'].split('/')[1] # FIXME (how ?) + client.service.storeAttachmentResource(resId, collId, attachment['content'], + fileFormat, attachment['filename']) + + return utils.response_for_json(request, data) + class MaarchContactView(MaarchDetailView): def get_data(self, request, contact_id=None, *args, **kwargs):