-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Public_file
query param and office365 renderer
Adding support for a `public_file` query param so the OSF can request a public renderer. Added office365 which is a public renderer. This uses office online to do .docx file conversions.
- Loading branch information
1 parent
8bb2dd4
commit 04740f1
Showing
12 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
# Office 365 Renderer | ||
|
||
|
||
This renderer uses Office Online to render .docx files for us. If the Office Online URL ever changes, it will also need to be changed here in settings. | ||
|
||
Currently there is no OSF side component for these changes. Once there is, this specific note can be removed. In the meantime in order to test this renderer, you need to go to your local OSF copy of this file: https://github.com/CenterForOpenScience/osf.io/blob/develop/addons/base/views.py#L728-L736 | ||
and add 'public_file' : 1, to the dict. This will send all files as public files. | ||
|
||
Testing this renderer locally is hard. Since Office Online needs access to the files it will not work with private files or ones hosted locally. To see what the docx files will render like, replace the render function with something that looks like this: | ||
|
||
``` | ||
def render(self): | ||
static_url = 'https://files.osf.io/v1/resources/<fake_project_id>/providers/osfstorage/<fake_file_id>' | ||
url = settings.OFFICE_BASE_URL + download_url.url | ||
return self.TEMPLATE.render(base=self.assets_url, url=url) | ||
``` | ||
|
||
The file at `static_url` must be publicly available. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .render import Office365Renderer # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import furl | ||
|
||
from mfr.core import extension | ||
from mako.lookup import TemplateLookup | ||
from mfr.extensions.office365 import settings | ||
|
||
|
||
class Office365Renderer(extension.BaseRenderer): | ||
"""A renderer for use with public .docx files. | ||
Office online can render .docx files to pdf for us. | ||
This renderer will only ever be made if a query param with `public_file=1` is sent. | ||
It then generates and embeds an office online url into an | ||
iframe and returns the template. The file it is trying to render MUST | ||
be available publically online. This renderer will not work if testing locally. | ||
""" | ||
|
||
TEMPLATE = TemplateLookup( | ||
directories=[ | ||
os.path.join(os.path.dirname(__file__), 'templates') | ||
]).get_template('viewer.mako') | ||
|
||
def render(self): | ||
download_url = furl.furl(self.metadata.download_url).set(query='') | ||
url = settings.OFFICE_BASE_URL + download_url.url | ||
return self.TEMPLATE.render(base=self.assets_url, url=url) | ||
|
||
@property | ||
def file_required(self): | ||
return False | ||
|
||
@property | ||
def cache_result(self): | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from mfr import settings | ||
|
||
|
||
config = settings.child('OFFICE365_EXTENSION_CONFIG') | ||
|
||
OFFICE_BASE_URL = 'https://view.officeapps.live.com/op/embed.aspx?src=' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<style> | ||
iframe { | ||
width: 100%; | ||
height: 800; | ||
} | ||
</style> | ||
|
||
<iframe src=${url} frameborder='0'></iframe> | ||
|
||
<script src="/static/js/mfr.js"></script> | ||
<script src="/static/js/mfr.child.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import furl | ||
import pytest | ||
|
||
from mfr.extensions.office365 import settings | ||
from mfr.core.provider import ProviderMetadata | ||
from mfr.extensions.office365 import Office365Renderer | ||
|
||
|
||
@pytest.fixture | ||
def metadata(): | ||
return ProviderMetadata('test', '.pdf', 'text/plain', '1234', | ||
'http://wb.osf.io/file/test.pdf?token=1234&public_file=1', | ||
is_public=True) | ||
|
||
|
||
@pytest.fixture | ||
def file_path(): | ||
return '/tmp/test.docx' | ||
|
||
|
||
@pytest.fixture | ||
def url(): | ||
return 'http://osf.io/file/test.pdf' | ||
|
||
|
||
@pytest.fixture | ||
def assets_url(): | ||
return 'http://mfr.osf.io/assets' | ||
|
||
|
||
@pytest.fixture | ||
def export_url(): | ||
return 'http://mfr.osf.io/export?url=' + url() | ||
|
||
|
||
@pytest.fixture | ||
def renderer(metadata, file_path, url, assets_url, export_url): | ||
return Office365Renderer(metadata, file_path, url, assets_url, export_url) | ||
|
||
|
||
class TestOffice365Renderer: | ||
|
||
def test_render_pdf(self, renderer, metadata, assets_url): | ||
download_url = furl.furl(metadata.download_url).set(query='') | ||
body_url = settings.OFFICE_BASE_URL + download_url.url | ||
body = renderer.render() | ||
assert '<iframe src={} frameborder=\'0\'></iframe>'.format(body_url) in body |