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
956797bf
Verified
Commit
956797bf
authored
3 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Support PDF generation without available request object
parent
d4cc128a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!689
Support PDF generation without available request object
Pipeline
#23764
canceled
3 years ago
Stage: test
Stage: build
Stage: publish
Stage: docker
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
aleksis/core/preferences.py
+15
-0
15 additions, 0 deletions
aleksis/core/preferences.py
aleksis/core/util/pdf.py
+26
-9
26 additions, 9 deletions
aleksis/core/util/pdf.py
with
41 additions
and
9 deletions
aleksis/core/preferences.py
+
15
−
0
View file @
956797bf
from
django.conf
import
settings
from
django.core.validators
import
URLValidator
from
django.forms
import
EmailField
,
ImageField
,
URLField
from
django.forms.widgets
import
SelectMultiple
from
django.utils.translation
import
gettext_lazy
as
_
...
...
@@ -52,6 +53,20 @@ class SiteDescription(StringPreference):
verbose_name
=
_
(
"
Site description
"
)
@site_preferences_registry.register
class
PDFURL
(
StringPreference
):
"""
URL which should be used as base for PDF printing.
"""
section
=
general
name
=
"
pdf_url
"
default
=
""
required
=
False
verbose_name
=
_
(
"
PDF URL
"
)
help_text
=
_
(
"
This URL is used for PDF printing.
"
)
field_kwargs
=
{
"
validators
"
:
[
URLValidator
()]}
@site_preferences_registry.register
class
ColourPrimary
(
StringPreference
):
"""
Primary colour in AlekSIS frontend.
"""
...
...
This diff is collapsed.
Click to expand it.
aleksis/core/util/pdf.py
+
26
−
9
View file @
956797bf
import
os
import
subprocess
# noqa
from
tempfile
import
TemporaryDirectory
from
typing
import
Optional
from
typing
import
Optional
,
Tuple
from
urllib.parse
import
urljoin
from
django.core.files
import
File
from
django.core.files.base
import
ContentFile
...
...
@@ -14,11 +15,13 @@ from django.utils import timezone
from
django.utils.translation
import
get_language
from
django.utils.translation
import
gettext
as
_
from
celery.result
import
AsyncResult
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
get_site_preferences
@recorded_task
...
...
@@ -64,6 +67,27 @@ 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.
"""
html_template
=
render_to_string
(
template_name
,
context
,
request
)
file_object
=
PDFFile
.
objects
.
create
(
html_file
=
ContentFile
(
html_template
,
name
=
"
source.html
"
))
# As this method may be run in background and there is no request available,
# we have to use a predefined URL from preferences then
if
request
:
html_url
=
request
.
build_absolute_uri
(
file_object
.
html_file
.
url
)
else
:
pdf_base_url
=
get_site_preferences
()[
"
general__pdf_url
"
]
html_url
=
urljoin
(
pdf_base_url
,
file_object
.
html_file
.
url
)
result
=
generate_pdf
.
delay
(
file_object
.
pk
,
html_url
,
lang
=
get_language
())
return
file_object
,
result
def
render_pdf
(
request
:
HttpRequest
,
template_name
:
str
,
context
:
dict
=
None
)
->
HttpResponse
:
"""
Start PDF generation and show progress page.
...
...
@@ -72,14 +96,7 @@ def render_pdf(request: HttpRequest, template_name: str, context: dict = None) -
if
not
context
:
context
=
{}
html_template
=
render_to_string
(
template_name
,
context
,
request
)
file_object
=
PDFFile
.
objects
.
create
(
person
=
request
.
user
.
person
,
html_file
=
ContentFile
(
html_template
,
name
=
"
source.html
"
)
)
html_url
=
request
.
build_absolute_uri
(
file_object
.
html_file
.
url
)
result
=
generate_pdf
.
delay
(
file_object
.
pk
,
html_url
,
lang
=
get_language
())
file_object
,
result
=
generate_pdf_from_template
(
template_name
,
context
,
request
)
redirect_url
=
reverse
(
"
redirect_to_pdf_file
"
,
args
=
[
file_object
.
pk
])
...
...
This diff is collapsed.
Click to expand it.
Jonathan Weth
@hansegucker
mentioned in commit
c3f48133
·
3 years ago
mentioned in commit
c3f48133
mentioned in commit c3f48133f32a0377b584d061f7e4c234121da1e8
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