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
459123e3
Commit
459123e3
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Plain Diff
Merge branch '546-pdf-generation-doesn-t-use-context-processors' into 'master'
Resolve "PDF generation doesn't use context processors" Closes
#546
See merge request
!811
parents
65e747cd
7317f377
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!811
Resolve "PDF generation doesn't use context processors"
Pipeline
#44702
canceled
3 years ago
Stage: test
Stage: build
Stage: publish
Stage: docker
Stage: deploy
Changes
4
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.rst
+1
-0
1 addition, 0 deletions
CHANGELOG.rst
aleksis/core/settings.py
+8
-0
8 additions, 0 deletions
aleksis/core/settings.py
aleksis/core/util/core_helpers.py
+12
-2
12 additions, 2 deletions
aleksis/core/util/core_helpers.py
aleksis/core/util/pdf.py
+11
-3
11 additions, 3 deletions
aleksis/core/util/pdf.py
with
32 additions
and
5 deletions
CHANGELOG.rst
+
1
−
0
View file @
459123e3
...
...
@@ -18,6 +18,7 @@ Fixed
~~~~~
* View for assigning permissions didn't work with some global permissions.
* PDFs generated in background didn't contain logo or site title.
`2.2.1_ – 2021-12-02
--------------------
...
...
This diff is collapsed.
Click to expand it.
aleksis/core/settings.py
+
8
−
0
View file @
459123e3
...
...
@@ -196,6 +196,14 @@ TEMPLATES = [
},
]
# Attention: The following context processors must accept None
# as first argument (in addition to a HttpRequest object)
PDF_CONTEXT_PROCESSORS
=
[
"
django.template.context_processors.i18n
"
,
"
django.template.context_processors.tz
"
,
"
aleksis.core.util.core_helpers.custom_information_processor
"
,
]
WSGI_APPLICATION
=
"
aleksis.core.wsgi.application
"
# Database
...
...
This diff is collapsed.
Click to expand it.
aleksis/core/util/core_helpers.py
+
12
−
2
View file @
459123e3
...
...
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
from
importlib
import
import_module
,
metadata
from
itertools
import
groupby
from
operator
import
itemgetter
from
typing
import
Any
,
Callable
,
Optional
,
Sequence
,
Union
from
typing
import
Any
,
Callable
,
Dict
,
Optional
,
Sequence
,
Union
from
warnings
import
warn
from
django.conf
import
settings
...
...
@@ -14,6 +14,7 @@ from django.http import HttpRequest
from
django.shortcuts
import
get_object_or_404
from
django.utils
import
timezone
from
django.utils.functional
import
lazy
from
django.utils.module_loading
import
import_string
from
cache_memoize
import
cache_memoize
...
...
@@ -198,7 +199,7 @@ def has_person(obj: Union[HttpRequest, Model]) -> bool:
return
True
def
custom_information_processor
(
request
:
HttpRequest
)
->
dict
:
def
custom_information_processor
(
request
:
Union
[
HttpRequest
,
None
]
)
->
dict
:
"""
Provide custom information in all templates.
"""
from
..models
import
CustomMenu
...
...
@@ -316,3 +317,12 @@ def get_allowed_object_ids(request: HttpRequest, models: list) -> list:
]
return
allowed_object_ids
def
process_custom_context_processors
(
context_processors
:
list
)
->
Dict
[
str
,
Any
]:
"""
Process custom context processors.
"""
context
=
{}
processors
=
tuple
(
import_string
(
path
)
for
path
in
context_processors
)
for
processor
in
processors
:
context
.
update
(
processor
(
None
))
return
context
This diff is collapsed.
Click to expand it.
aleksis/core/util/pdf.py
+
11
−
3
View file @
459123e3
import
os
import
subprocess
# noqa
from
tempfile
import
TemporaryDirectory
from
typing
import
Optional
,
Tuple
from
typing
import
Optional
,
Tuple
,
Union
from
urllib.parse
import
urljoin
from
django.conf
import
settings
...
...
@@ -22,6 +22,7 @@ from celery_progress.backend import ProgressRecorder
from
aleksis.core.celery
import
app
from
aleksis.core.models
import
PDFFile
from
aleksis.core.util.celery_progress
import
recorded_task
,
render_progress_page
from
aleksis.core.util.core_helpers
import
process_custom_context_processors
@recorded_task
...
...
@@ -71,7 +72,12 @@ 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.
"""
html_template
=
render_to_string
(
template_name
,
context
,
request
)
if
not
request
:
processed_context
=
process_custom_context_processors
(
settings
.
PDF_CONTEXT_PROCESSORS
)
processed_context
.
update
(
context
)
else
:
processed_context
=
context
html_template
=
render_to_string
(
template_name
,
processed_context
,
request
)
file_object
=
PDFFile
.
objects
.
create
(
html_file
=
ContentFile
(
html_template
,
name
=
"
source.html
"
))
...
...
@@ -87,7 +93,9 @@ def generate_pdf_from_template(
return
file_object
,
result
def
render_pdf
(
request
:
HttpRequest
,
template_name
:
str
,
context
:
dict
=
None
)
->
HttpResponse
:
def
render_pdf
(
request
:
Union
[
HttpRequest
,
None
],
template_name
:
str
,
context
:
dict
=
None
)
->
HttpResponse
:
"""
Start PDF generation and show progress page.
The progress page will redirect to the PDF after completion.
...
...
This diff is collapsed.
Click to expand it.
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