add POST on /resource/
This commit is contained in:
parent
0b3ca6485e
commit
9ef9efdad9
|
@ -14,6 +14,8 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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'<html>'
|
||||
document += u'<h1>%s</h1>' % schema['name']
|
||||
for field in schema['fields']:
|
||||
part = ''
|
||||
if field['type'] == 'page':
|
||||
part = u'<hr /><h2>%s</h2>' % field['label']
|
||||
elif field['type'] == 'title':
|
||||
part = u'<h3>%s</h3>' % field['label']
|
||||
elif 'varname' in field:
|
||||
part = u'<dl>'
|
||||
part += u'<dt>%s</dt>' % 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'<dd>%s</dd>' % value
|
||||
part += u'</dl>'
|
||||
document += part
|
||||
document += '</html>'
|
||||
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):
|
||||
|
|
Reference in New Issue