-
Notifications
You must be signed in to change notification settings - Fork 76
[SVCS-675] Add file size limit to tabular renderer. #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import os | ||
| import json | ||
| import logging | ||
| import os | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix import order which should be length based.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought for MFR we were doing alphabetical order, or am I misremembering that? |
||
|
|
||
| from humanfriendly import format_size | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 A nice package that I was trying to find a while ago. |
||
| from mako.lookup import TemplateLookup | ||
| from mfr.core import extension | ||
|
|
||
| from mfr.core import extension | ||
| from mfr.extensions.tabular import settings | ||
| from mfr.extensions.tabular import exceptions | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
|
|
||
| class TabularRenderer(extension.BaseRenderer): | ||
|
|
||
|
|
@@ -16,6 +20,16 @@ class TabularRenderer(extension.BaseRenderer): | |
| ]).get_template('viewer.mako') | ||
|
|
||
| def render(self): | ||
| file_size = os.path.getsize(self.file_path) | ||
| if file_size > settings.MAX_FILE_SIZE: | ||
| raise exceptions.FileTooLargeError( | ||
| 'Tabular files larger than {} are not rendered. Please download ' | ||
| 'the file to view.'.format(format_size(settings.MAX_FILE_SIZE, binary=True)), | ||
| file_size=file_size, | ||
| max_size=settings.MAX_FILE_SIZE, | ||
| extension=self.metadata.ext, | ||
| ) | ||
|
|
||
| with open(self.file_path, errors='replace') as fp: | ||
| sheets, size = self._render_grid(fp, self.metadata.ext) | ||
| return self.TEMPLATE.render( | ||
|
|
@@ -34,7 +48,7 @@ def file_required(self): | |
| def cache_result(self): | ||
| return True | ||
|
|
||
| def _render_grid(self, fp, ext, *args, **kwargs): # assets_path, ext): | ||
| def _render_grid(self, fp, ext, *args, **kwargs): | ||
| """Render a tabular file to html | ||
| :param fp: file pointer object | ||
| :return: RenderResult object containing html and assets | ||
|
|
@@ -46,22 +60,31 @@ def _render_grid(self, fp, ext, *args, **kwargs): # assets_path, ext): | |
| size = settings.SMALL_TABLE | ||
| self._renderer_tabular_metrics['size'] = 'small' | ||
| self._renderer_tabular_metrics['nbr_sheets'] = len(sheets) | ||
| for sheet in sheets: | ||
| sheet = sheets[sheet] # Sheets are stored in key-value pairs of the form {sheet: (col, row)} | ||
| if len(sheet[0]) > 9: # Check the number of columns | ||
| for sheet_title in sheets: | ||
| sheet = sheets[sheet_title] | ||
|
|
||
| # sheet is a two-element list. sheet[0] is a list of dicts containing metadata about | ||
| # the column headers. Each dict contains four keys: `field`, `name`, `sortable`, `id`. | ||
| # sheet[1] is a list of dicts where each dict contains the row data. The keys are the | ||
| # fields the data belongs to and the values are the data values. | ||
|
|
||
| nbr_cols = len(sheet[0]) | ||
| if nbr_cols > 9: | ||
| size = settings.BIG_TABLE | ||
| self._renderer_tabular_metrics['size'] = 'big' | ||
|
|
||
| if len(sheet[0]) > settings.MAX_SIZE or len(sheet[1]) > settings.MAX_SIZE: | ||
| raise exceptions.TableTooBigError('Table is too large to render.', extension=ext) | ||
| nbr_rows = len(sheet[1]) | ||
| if nbr_cols > settings.MAX_SIZE or nbr_rows > settings.MAX_SIZE: | ||
| raise exceptions.TableTooBigError('Table is too large to render.', extension=ext, | ||
| nbr_cols=nbr_cols, nbr_rows=nbr_rows) | ||
|
|
||
| return sheets, size | ||
|
|
||
| def _populate_data(self, fp, ext): | ||
| """Determine the appropriate library and use it to populate rows and columns | ||
| :param fp: file pointer | ||
| :param ext: file extension | ||
| :return: tuple of column headers and row data | ||
| :return: a dict mapping sheet titles to tuples of column headers and row data | ||
| """ | ||
| function_preference = settings.LIBS.get(ext.lower()) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember you mentioned in WB but I am not sure about MFR: when an exception is added, should we update the tests somewhere to make sure that serialization works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed for MFR. That test is to make sure the error survives the pickling/unpickling done by WB's celery worker.