Skip to content

Commit

Permalink
Adding a viewer that delegates output to FLAT (untested) #48
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Jan 4, 2017
1 parent 44c0abe commit 5b4ee6f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion clam/common/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import csv
import sys
import os.path
import random
import requests
from lxml import etree
if sys.version < '3':
Expand Down Expand Up @@ -68,7 +69,6 @@ def view(self, file, **kwargs):
return flask.redirect(url)



class SimpleTableViewer(AbstractViewer):
id = 'tableviewer'
name = "Table viewer"
Expand Down Expand Up @@ -186,6 +186,25 @@ def view(self, file, **kwargs):
return str(transform(xml_doc))


class FLATViewer(AbstractViewer):
id = 'flatviewer'
name = "Open in FLAT"

def __init__(self, **kwargs):
if 'url' in kwargs:
self.url = kwargs['url']
del kwargs['url']
else:
self.url = ''

super(FLATViewer,self).__init__(**kwargs)

def view(self, file, **kwargs):
#filename will contain a random component to prevent clashes
filename = os.path.basename(file.filename).replace('.folia.xml','').replace('.xml','') + str("%034x" % random.getrandbits(128)) + '.folia.xml'
r = requests.post(self.url + '/pub/upload/', allow_redirects=False, files=[('file',(filename,file,'application/xml'))])
#FLAT redirects after upload, we catch the redirect rather than following it automatically following it, and return it ourselves as our redirect
if 'location' in r.headers:
return flask.redirect(r.headers['location'])
else:
return str(r)

0 comments on commit 5b4ee6f

Please sign in to comment.