Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Fix print endpoint to not return HTML in response
Browse files Browse the repository at this point in the history
  • Loading branch information
ethantkoenig committed Feb 19, 2018
1 parent e471461 commit ff9a6a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
6 changes: 4 additions & 2 deletions libraries/aws_tools/s3_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def replace(self, key, catch_exception=True):
return self.resource.Object(bucket_name=self.bucket_name, key=key).copy_from(
CopySource='{0}/{1}'.format(self.bucket_name, key), MetadataDirective='REPLACE')

def upload_file(self, path, key, cache_time=600):
def upload_file(self, path, key, cache_time=600, content_type=None):
"""
Upload file to S3 storage. Similar to the s3.upload_file, however, that
does not work nicely with moto, whereas this function does.
Expand All @@ -146,10 +146,12 @@ def upload_file(self, path, key, cache_time=600):
"""
with open(path, 'rb') as f:
binary = f.read()
if content_type is None:
content_type = get_mime_type(path)
self.bucket.put_object(
Key=key,
Body=binary,
ContentType=get_mime_type(path),
ContentType=content_type,
CacheControl='max-age={0}'.format(cache_time)
)

Expand Down
73 changes: 35 additions & 38 deletions libraries/door43_tools/project_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,44 @@ def print_project(self, project_id):
source_path = 'u/{0}'.format(project_id)
print_all_key = '{0}/print_all.html'.format(source_path)
print_all_file = tempfile.mktemp(prefix='print_all_')
if not App.cdn_s3_handler().key_exists(print_all_key):
files_dir = tempfile.mkdtemp(prefix='files_')
App.cdn_s3_handler().download_dir(source_path, files_dir)
project_dir = os.path.join(files_dir, source_path.replace('/', os.path.sep))
if not os.path.isdir(project_dir):
raise Exception('Project not found.')
rc = RC(project_dir, repo_name)
with codecs.open(print_all_file, 'w', 'utf-8-sig') as print_all:
print_all.write("""
<html lang="{0}" dir="{1}">
<head>
<meta charset="UTF-8"/>
<title>{2}: {3}</title>
<style type="text/css">
body > div {{
page-break-after: always;
}}
</style>
</head>
<body onLoad="window.print()">
<h1>{2}: {3}</h1>
if App.cdn_s3_handler().key_exists(print_all_key):
return App.cdn_s3_handler().bucket_name + '/' + print_all_key
files_dir = tempfile.mkdtemp(prefix='files_')
App.cdn_s3_handler().download_dir(source_path, files_dir)
project_dir = os.path.join(files_dir, source_path.replace('/', os.path.sep))
if not os.path.isdir(project_dir):
raise Exception('Project not found.')
rc = RC(project_dir, repo_name)
with codecs.open(print_all_file, 'w', 'utf-8-sig') as print_all:
print_all.write("""<html lang="{0}" dir="{1}">
<head>
<meta charset="UTF-8"/>
<title>{2}: {3}</title>
<style type="text/css">
body > div {{
page-break-after: always;
}}
</style>
</head>
<body onLoad="window.print()">
<h1>{2}: {3}</h1>
""".format(rc.resource.language.identifier, rc.resource.language.direction, rc.resource.language.title,
rc.resource.title))
for fname in sorted(glob(os.path.join(project_dir, '*.html')), key=self.front_to_back):
with codecs.open(fname, 'r', 'utf-8-sig') as f:
soup = BeautifulSoup(f, 'html.parser')
# get the body of the raw html file
content = soup.div
if not content:
content = BeautifulSoup('<div>No content</div>', 'html.parser').find('div').extract()
content['id'] = os.path.basename(fname)
print_all.write(unicode(content))
print_all.write("""
</body>
rc.resource.title))
for fname in sorted(glob(os.path.join(project_dir, '*.html')), key=self.front_to_back):
with codecs.open(fname, 'r') as f:
soup = BeautifulSoup(f, 'html.parser')
# get the body of the raw html file
content = soup.div
if not content:
content = BeautifulSoup('<div>No content</div>', 'html.parser').find('div').extract()
content['id'] = os.path.basename(fname)
print_all.write(unicode(content))
print_all.write("""
</body>
</html>
""")
App.cdn_s3_handler().upload_file(print_all_file, print_all_key, cache_time=0)
html = read_file(print_all_file)
else:
html = App.cdn_s3_handler().get_file_contents(print_all_key)
return html
App.cdn_s3_handler().upload_file(print_all_file, print_all_key, cache_time=0, content_type='text/html')
return App.cdn_s3_handler().bucket_name + '/' + print_all_key

@staticmethod
def front_to_back(file_path):
Expand Down

0 comments on commit ff9a6a4

Please sign in to comment.