Skip to content

Commit

Permalink
add: password-protected PDFs for PDF import
Browse files Browse the repository at this point in the history
  • Loading branch information
saemideluxe committed Nov 15, 2023
1 parent 24a8225 commit a41ef48
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion basxbread/contrib/customforms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ class Meta:
verbose_name_plural = _("Custom form fields")


def pdf_fields(pdffile):
def pdf_fields(pdffile, password=None):
fields = {}
pdf = fitz.Document(stream=pdffile)
if password is not None:
pdf.authenticate(password)
for page in pdf:
widget = page.first_widget
while widget:
Expand Down
8 changes: 5 additions & 3 deletions basxbread/contrib/customforms/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import base64

import htmlgenerator as hg
from basxbread import formatters, layout, utils, views
from django import forms
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext_lazy as _
from guardian.shortcuts import get_objects_for_user

from basxbread import formatters, layout, utils, views

from . import models


Expand Down Expand Up @@ -92,6 +91,7 @@ def formview(request, pk):
def pdfimportview(request, pk):
class UploadForm(forms.Form):
importfile = forms.FileField(required=False)
password = forms.CharField(required=False)

form = UploadForm()
pdfimporter = get_object_or_404(models.PDFImport, pk=pk)
Expand All @@ -100,7 +100,9 @@ class UploadForm(forms.Form):
if uploadform.is_valid():
if uploadform.cleaned_data.get("importfile"):
pdfcontent = uploadform.cleaned_data["importfile"].read()
pdffields = models.pdf_fields(pdfcontent)
pdffields = models.pdf_fields(
pdfcontent, uploadform.cleaned_data["password"] or None
)
initial = {}
for pdf_formfield in pdfimporter.fields.exclude(customform_field=None):
value = pdffields.get(pdf_formfield.pdf_field_name, "")
Expand Down

0 comments on commit a41ef48

Please sign in to comment.