-
Notifications
You must be signed in to change notification settings - Fork 23
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
Default toc length config #2042
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0b56eea
Merge branch 'master' of https://github.com/openoereb/pyramid_oereb
voisardf be33be2
Merge branch 'master' of https://github.com/openoereb/pyramid_oereb
voisardf b84a487
Merge remote-tracking branch 'upstream/master'
voisardf df0c13f
Merge remote-tracking branch 'upstream/master'
voisardf d667506
Merge branch 'master' of https://github.com/openoereb/pyramid_oereb
voisardf 46444b5
Merge remote-tracking branch 'upstream/master'
voisardf f50347b
Merge branch 'master' of https://github.com/openoereb/pyramid_oereb
voisardf 69e23c4
first draft of new toc parameters implementation
voisardf 3b55d99
Refactured the process and added more logging
voisardf d8c72db
added linting comments
voisardf 7296571
added two config tests
voisardf 087b45d
fixed some test and linting
voisardf c4b9261
more linting, sorry
voisardf e9023cb
improved and fixed parameter documentation
voisardf a5157d0
improved code as discussed and suggested
voisardf 77f6db1
typo with lint
voisardf a5f42fa
renamed parameter as requested
voisardf ea54626
adapted documentation of new parameter
voisardf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,17 +73,20 @@ | |
extract_as_dict = self._render(extract_record, value[1]) | ||
feature_geometry = mapping(extract_record.real_estate.limit) | ||
|
||
if Config.get('print', {}).get('compute_toc_pages', False): | ||
print_config = Config.get('print', {}) | ||
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. If the variable 'print_config' is defined, it should thoroughly be used in subsequent parts of the code. |
||
|
||
if print_config.get('compute_toc_pages', False): | ||
extract_as_dict['nbTocPages'] = TocPages(extract_as_dict).getNbPages() | ||
else: | ||
extract_as_dict['nbTocPages'] = 1 | ||
if print_config.get('general_toc_length') and int(print_config.get('general_toc_length')) > 0: | ||
extract_as_dict['nbTocPages'] = print_config.get('general_toc_length', 2) | ||
else: | ||
extract_as_dict['nbTocPages'] = 1 | ||
|
||
# set the global_datetime variable so that it can be used later for the archive | ||
self.set_global_datetime(extract_as_dict['CreationDate']) | ||
self.convert_to_printable_extract(extract_as_dict, feature_geometry) | ||
|
||
print_config = Config.get('print', {}) | ||
|
||
extract_as_dict['Display_RealEstate_SubunitOfLandRegister'] = print_config.get( | ||
'display_real_estate_subunit_of_land_register', True | ||
) | ||
|
@@ -97,7 +100,7 @@ | |
) | ||
|
||
spec = { | ||
'layout': Config.get('print', {})['template_name'], | ||
'layout': print_config['template_name'], | ||
'outputFormat': 'pdf', | ||
'lang': self._language, | ||
'attributes': extract_as_dict, | ||
|
@@ -108,37 +111,41 @@ | |
if self._request.GET.get('getspec', 'no') != 'no': | ||
response.headers['Content-Type'] = 'application/json; charset=UTF-8' | ||
return json.dumps(spec, sort_keys=True, indent=4) | ||
pdf_url = urlparse.urljoin(Config.get('print', {})['base_url'] + '/', 'buildreport.pdf') | ||
pdf_headers = Config.get('print', {})['headers'] | ||
pdf_url = urlparse.urljoin(print_config['base_url'] + '/', 'buildreport.pdf') | ||
pdf_headers = print_config['headers'] | ||
print_result = requests.post( | ||
pdf_url, | ||
headers=pdf_headers, | ||
data=json.dumps(spec) | ||
) | ||
try: | ||
if Config.get('print', {}).get('compute_toc_pages', False): | ||
with io.BytesIO() as pdf: | ||
pdf.write(print_result.content) | ||
pdf_reader = PdfReader(pdf) | ||
x = [] | ||
for i in range(len(pdf_reader.outline)): | ||
if isinstance(pdf_reader.outline[i], list): | ||
x.append(pdf_reader.outline[i][0]['/Page']['/StructParents']) | ||
else: | ||
x.append(pdf_reader.outline[i]['/Page']['/StructParents']) | ||
try: | ||
true_nb_of_toc = min(x)-1 | ||
except ValueError: | ||
true_nb_of_toc = 1 | ||
|
||
if true_nb_of_toc != extract_as_dict['nbTocPages']: | ||
log.warning('nbTocPages in result pdf: {} are not equal to the one predicted : {}, request new pdf'.format(true_nb_of_toc,extract_as_dict['nbTocPages'])) # noqa | ||
extract_as_dict['nbTocPages'] = true_nb_of_toc | ||
print_result = requests.post( | ||
pdf_url, | ||
headers=pdf_headers, | ||
data=json.dumps(spec) | ||
) | ||
log.debug('Validation of the TOC length with compute_toc_pages set to {} and general_toc_length set to {}'.format(print_config.get('compute_toc_pages'), print_config.get('general_toc_length'))) # noqa | ||
with io.BytesIO() as pdf: | ||
pdf.write(print_result.content) | ||
pdf_reader = PdfReader(pdf) | ||
x = [] | ||
for i in range(len(pdf_reader.outline)): | ||
if isinstance(pdf_reader.outline[i], list): | ||
x.append(pdf_reader.outline[i][0]['/Page']['/StructParents']) | ||
else: | ||
x.append(pdf_reader.outline[i]['/Page']['/StructParents']) | ||
try: | ||
true_nb_of_toc = min(x)-1 | ||
except ValueError: | ||
true_nb_of_toc = 1 | ||
|
||
log.debug('True number of TOC pages is {}, expected number was {}'.format(true_nb_of_toc, extract_as_dict['nbTocPages'])) # noqa | ||
if true_nb_of_toc != extract_as_dict['nbTocPages']: | ||
log.warning('nbTocPages in result pdf: {} are not equal to the one predicted : {}, request new pdf'.format(true_nb_of_toc,extract_as_dict['nbTocPages'])) # noqa | ||
log.debug('Secondary PDF extract call STARTED') | ||
extract_as_dict['nbTocPages'] = true_nb_of_toc | ||
print_result = requests.post( | ||
pdf_url, | ||
headers=pdf_headers, | ||
data=json.dumps(spec) | ||
) | ||
log.debug('Secondary PDF extract call to fix TOC pages number DONE') | ||
|
||
except PdfReadError as e: | ||
err_msg = 'a problem occurred while generating the pdf file' | ||
log.error(err_msg + ': ' + str(e)) | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
That is omitted now.
A change in this pull request is that the pdf is built a second time whenever the numbers of toc pages disagree between extract_as_dict['nbTocPages'] (computed/expected/1, depending on the setting) and the one in the returned pdf.
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.
In the current version it is only rebuild if 'compute_toc_pages' is true.