Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
public-libs
latex-renderer
Commits
81473a67
Commit
81473a67
authored
1 year ago
by
Filip Štamcar
Browse files
Options
Download
Email Patches
Plain Diff
Store generated documents in database
parent
c8e1e648
develop
hr-management-integration
No related merge requests found
Pipeline
#9504
failed with stages
in 1 minute and 7 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
project/settings/ci_testing.py
+1
-1
project/settings/ci_testing.py
render_latex/admin.py
+27
-1
render_latex/admin.py
render_latex/migrations/0001_initial.py
+21
-0
render_latex/migrations/0001_initial.py
render_latex/models.py
+9
-0
render_latex/models.py
render_latex/views.py
+20
-7
render_latex/views.py
with
78 additions
and
9 deletions
+78
-9
project/settings/ci_testing.py
+
1
-
1
View file @
81473a67
...
...
@@ -16,4 +16,4 @@ PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
SEND_EMAIL_CONFIRMATIONS
=
False
# FIX: this should be looked at at some point
INSTALLED_APPS
.
remove
(
"rest_registration"
)
#
INSTALLED_APPS.remove("rest_registration")
This diff is collapsed.
Click to expand it.
render_latex/admin.py
+
27
-
1
View file @
81473a67
from
django.contrib
import
admin
from
django.contrib
import
admin
,
messages
from
django.http
import
HttpResponse
from
.models
import
Document
# Register your models here.
class
DocumentAdmin
(
admin
.
ModelAdmin
):
list_display
=
[
"datetime"
,
"successful"
]
fields
=
[
"datetime"
,
"source"
]
actions
=
[
"download"
]
@
admin
.
action
(
description
=
"Download rendered document"
)
def
download
(
self
,
request
,
queryset
):
if
len
(
queryset
)
!=
1
:
messages
.
error
(
request
,
"Exactly one successful document must be selected"
)
return
if
not
queryset
[
0
].
successful
():
messages
.
error
(
request
,
"Only successfully rendered documents can be downloaded"
)
return
response
=
HttpResponse
(
queryset
[
0
].
output
,
content_type
=
"application/pdf"
)
response
[
"Content-Disposition"
]
=
'attachment; filename="report.pdf"'
return
response
admin
.
site
.
register
(
Document
,
DocumentAdmin
)
This diff is collapsed.
Click to expand it.
render_latex/migrations/0001_initial.py
0 → 100644
+
21
-
0
View file @
81473a67
# Generated by Django 4.2 on 2023-08-07 14:48
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
initial
=
True
dependencies
=
[]
operations
=
[
migrations
.
CreateModel
(
name
=
"Document"
,
fields
=
[
(
"id"
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
"ID"
)),
(
"datetime"
,
models
.
DateTimeField
()),
(
"source"
,
models
.
TextField
()),
(
"output"
,
models
.
BinaryField
(
null
=
True
)),
],
),
]
This diff is collapsed.
Click to expand it.
render_latex/models.py
+
9
-
0
View file @
81473a67
from
django.db
import
models
class
Document
(
models
.
Model
):
datetime
=
models
.
DateTimeField
()
source
=
models
.
TextField
()
output
=
models
.
BinaryField
(
null
=
True
)
def
successful
(
self
):
return
self
.
output
is
not
None
This diff is collapsed.
Click to expand it.
render_latex/views.py
+
20
-
7
View file @
81473a67
import
datetime
import
logging
import
os
import
subprocess
import
tempfile
from
django.core.exceptions
import
BadRequest
from
django.http
import
FileResponse
from
drf_spectacular.types
import
OpenApiTypes
from
drf_spectacular.utils
import
OpenApiResponse
,
extend_schema
from
django.http
import
HttpResponse
from
drf_spectacular.utils
import
extend_schema
from
rest_framework
import
parsers
,
viewsets
from
rest_framework.decorators
import
action
from
utils.decorators
import
file_response
from
utils.renderers
import
BinaryRenderer
from
.models
import
Document
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -32,10 +34,14 @@ class LatexViewSet(viewsets.GenericViewSet):
@
action
(
detail
=
False
,
methods
=
[
"post"
],
renderer_classes
=
[
BinaryRenderer
])
def
generate_pdf
(
self
,
request
,
pk
=
None
):
with
tempfile
.
TemporaryDirectory
(
prefix
=
"latex-renderer-"
)
as
directory
:
document
=
Document
(
datetime
=
datetime
.
datetime
.
now
(
tz
=
datetime
.
timezone
.
utc
))
try
:
# Store uploaded file to temporary directory
with
open
(
os
.
path
.
join
(
directory
,
"report.tex"
),
"wb"
)
as
source
:
source
.
write
(
request
.
FILES
.
get
(
"file"
).
read
())
with
open
(
os
.
path
.
join
(
directory
,
"report.tex"
),
"wb"
)
as
sourcefile
:
source
=
request
.
FILES
.
get
(
"file"
).
read
()
document
.
source
=
source
.
decode
()
sourcefile
.
write
(
source
)
except
AttributeError
as
error
:
logger
.
warning
(
error
,
exc_info
=
error
)
raise
BadRequest
(
"Failed to store uploaded file"
)
...
...
@@ -44,12 +50,19 @@ class LatexViewSet(viewsets.GenericViewSet):
# Convert TEX to PDF
generate_pdf_report
(
directory
)
except
subprocess
.
CalledProcessError
as
error
:
document
.
save
()
logger
.
warning
(
error
,
exc_info
=
error
)
raise
BadRequest
(
"Failed to render uploaded file"
)
# Return the rendered PDF file
filename
=
os
.
path
.
join
(
directory
,
"report.pdf"
)
response
=
FileResponse
(
open
(
filename
,
"rb"
),
content_type
=
"application/pdf"
)
# Store result in database
with
open
(
filename
,
"rb"
)
as
outputfile
:
document
.
output
=
outputfile
.
read
()
document
.
save
()
# Return the rendered PDF file
response
=
HttpResponse
(
document
.
output
,
content_type
=
"application/pdf"
)
response
[
"Content-Disposition"
]
=
'attachment; filename="report.pdf"'
return
response
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help