Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
6340e789
Verified
Commit
6340e789
authored
2 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Divide PDF generation functionality in more functions to use separate parts
parent
19434b3e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!991
Add base template for plain prints and optimize PDF generation
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/core/util/pdf.py
+22
-7
22 additions, 7 deletions
aleksis/core/util/pdf.py
with
22 additions
and
7 deletions
aleksis/core/util/pdf.py
+
22
−
7
View file @
6340e789
...
...
@@ -2,7 +2,7 @@ import os
import
subprocess
# noqa
from
datetime
import
timedelta
from
tempfile
import
TemporaryDirectory
from
typing
import
Optional
,
Tuple
,
Union
from
typing
import
Optional
,
Tuple
,
Union
,
Callable
from
urllib.parse
import
urljoin
from
django.conf
import
settings
...
...
@@ -69,10 +69,8 @@ def generate_pdf(
recorder
.
set_progress
(
1
,
1
)
def
generate_pdf_from_template
(
template_name
:
str
,
context
:
Optional
[
dict
]
=
None
,
request
:
Optional
[
HttpRequest
]
=
None
)
->
Tuple
[
PDFFile
,
AsyncResult
]:
"""
Start a PDF generation task and return the matching file object and Celery result.
"""
def
process_context_for_pdf
(
context
:
Optional
[
dict
]
=
None
,
request
:
Optional
[
HttpRequest
]
=
None
):
context
=
context
or
{}
if
not
request
:
processed_context
=
process_custom_context_processors
(
settings
.
NON_REQUEST_CONTEXT_PROCESSORS
...
...
@@ -80,10 +78,14 @@ def generate_pdf_from_template(
processed_context
.
update
(
context
)
else
:
processed_context
=
context
html_template
=
render_to_string
(
template_name
,
processed_context
,
request
)
return
processed_context
def
generate_pdf_from_html
(
html
:
str
,
request
:
Optional
[
HttpRequest
]
=
None
)
->
Tuple
[
PDFFile
,
AsyncResult
]:
"""
Start a PDF generation task and return the matching file object and Celery result.
"""
file_object
=
PDFFile
.
objects
.
create
(
html_file
=
ContentFile
(
html
_template
.
encode
(),
name
=
"
source.html
"
)
html_file
=
ContentFile
(
html
.
encode
(),
name
=
"
source.html
"
)
)
# As this method may be run in background and there is no request available,
...
...
@@ -97,6 +99,19 @@ def generate_pdf_from_template(
return
file_object
,
result
def
generate_pdf_from_template
(
template_name
:
str
,
context
:
Optional
[
dict
]
=
None
,
request
:
Optional
[
HttpRequest
]
=
None
,
render_method
:
Optional
[
Callable
]
=
None
)
->
Tuple
[
PDFFile
,
AsyncResult
]:
"""
Start a PDF generation task and return the matching file object and Celery result.
"""
processed_context
=
process_context_for_pdf
(
context
,
request
)
if
render_method
:
html_template
=
render_method
(
processed_context
,
request
)
else
:
html_template
=
render_to_string
(
template_name
,
processed_context
,
request
)
return
generate_pdf_from_html
(
html_template
,
request
)
def
render_pdf
(
request
:
Union
[
HttpRequest
,
None
],
template_name
:
str
,
context
:
dict
=
None
...
...
This diff is collapsed.
Click to expand it.
Jonathan Weth
@hansegucker
mentioned in commit
cc744fab
·
2 years ago
mentioned in commit
cc744fab
mentioned in commit cc744fabbc402321be33e748845d51ff9db306a3
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment