Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions django/geno/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@
)


class MultipleFileInput(forms.ClearableFileInput):
class MultipleFileInput(UnfoldAdminFileFieldWidget):
allow_multiple_selected = True


Expand Down Expand Up @@ -1310,10 +1310,21 @@
stamps = kwargs.pop("stamps_available")
super().__init__(*args, **kwargs)
choices = []
if stamps:
for key in sorted(stamps):
choices.append((key, stamps[key]))
self.fields["stamp_type"] = forms.ChoiceField(choices=choices, label="Frankierung")
self.fields["stamp_type"] = forms.ChoiceField(
choices=choices, label="Frankierung", widget=UnfoldAdminSelectWidget()
)

# Add Crispy Forms helper for Unfold styling
self.helper = FormHelper()
self.helper.form_tag = False # Form tag handled in template
self.helper.form_class = ""
self.helper.layout = Layout(

Check warning on line 1324 in django/geno/forms.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 1313-1324
Div("files", css_class="mb-4"),
Div("stamp_type", css_class="mb-4"),
)


class InvoiceFilterForm(forms.Form):
Expand Down
14 changes: 9 additions & 5 deletions django/geno/templates/geno/default.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "geno/base.html" %}
{% load static i18n unfold %}
{% load static i18n unfold crispy_forms_tags %}

{% block custom_content %}

Expand All @@ -10,14 +10,14 @@
{% if form_action %}action="{{ form_action }}"{% endif %}
method="post">
{% csrf_token %}
<div class="aligned border border-base-200 mb-8 rounded-default pt-3 px-3 pb-3 shadow-sm dark:border-base-800 legacyform">
<div class="aligned border border-base-200 mb-8 rounded-default pt-3 px-3 pb-3 shadow-sm dark:border-base-800">
{% if help_text %}
<div class="flex items-center pb-2">
<span class="material-symbols-outlined md-18 mr-2 w-[18px]">info</span><span>{{ help_text }}</span>
</div>
{% endif %}
{{ form.as_p }}
<input type="submit"
{% crispy form "unfold_crispy" %}
<input type="submit" class="font-medium flex group items-center gap-2 px-3 py-2 relative rounded-default justify-center whitespace-nowrap cursor-pointer border border-base-200 bg-primary-600 border-transparent text-white"
{% if submit_title %}value="{{ submit_title }}"{% endif %} />
</div>
</form>
Expand Down Expand Up @@ -142,7 +142,11 @@ <h2 class="text-lg font-semibold mb-4 text-font-important-light dark:text-font-i
{% endblock %}

{% if download_file_url %}
<iframe width="1" height="1" frameborder="0" src="{{ download_file_url }}"></iframe>
<script>
window.addEventListener('load', function () {
window.location.href = `{{ download_file_url }}`;
});
</script>
{% endif %}

{% endblock %}
51 changes: 31 additions & 20 deletions django/geno/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4490,7 +4490,7 @@
class WebstampView(CohivaAdminViewMixin, FormView):
title = "PDFs frankieren"
form_class = WebstampForm
permission_required = ("geno.tools_webstmap",)
permission_required = ("geno.tools_webstamp",)
tmpdir = "/tmp/webstamp"

def __init__(self, *args, **kwargs):
Expand All @@ -4515,35 +4515,54 @@
"PDF Dateien hochladen. Die erste Seite wird frankiert (Fenster-Couvert links)"
),
"download_file_url": self.download_file_url,
"submit_title": "Datei(en) frankieren",
}
)
return context

Check warning on line 4521 in django/geno/views.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on line 4521

def get(self, request, *args, **kwargs):
if request.GET.get("download", None):
return self.send_file(request.GET.get("download"))
return super().get(request, *args, **kwargs)

Check warning on line 4526 in django/geno/views.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on line 4526

def form_valid(self, form):
self.result = self.add_webstamps(
form.cleaned_data["files"], form.cleaned_data["stamp_type"]
)
if isinstance(self.result, str):
self.download_file_url = (
f"{reverse('geno:webstamp')}?download={os.path.basename(self.result)}"
)
ret = self.add_webstamps(form.cleaned_data["files"], form.cleaned_data["stamp_type"])
if isinstance(ret, str):
self.download_file_url = f"{reverse('geno:webstamp')}?download={os.path.basename(ret)}"
self.result = [
{
"info": "Die Frankierung war erfolgreich.",
"variant": ResponseVariant.SUCCESS.value,
"objects": [
"Falls der Download nicht automatisch startet, bitte "
f'<a href="{self.download_file_url}" '
'class="text-blue-700 dark:text-blue-400">hier</a> klicken.'
],
}
]

else:
self.result = [
{
"info": "Fehler beim Frankieren der Datei(en)",
"variant": ResponseVariant.ERROR.value,
"objects": ret,
}
]
return self.get(self.request)

Check warning on line 4552 in django/geno/views.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 4529-4552

def send_file(self, tmp_file_name):
tmp_file_path = os.path.normpath(os.path.join(self.tmpdir, tmp_file_name))
if not tmp_file_path.startswith(self.tmpdir):
logger.error(f"Can't send file {tmp_file_path}: Permission denied.")
raise PermissionDenied()
if os.path.isfile(tmp_file_path):
pdf_file_name = "PDF_frankiert"
resp = FileResponse(open(tmp_file_path, "rb"), content_type="application/pdf")
resp["Content-Disposition"] = "attachment; filename=%s.pdf" % pdf_file_name
logger.info(f"Send {tmp_file_path} (and delete).")
os.remove(tmp_file_path)
return resp

Check warning on line 4565 in django/geno/views.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 4555-4565
else:
raise Http404(f"File {tmp_file_name} not found.")

Expand Down Expand Up @@ -4586,18 +4605,13 @@
for chunk in f.chunks():
destination.write(chunk)
tmp_files.append(tmp_file.name)
cmd_out = subprocess.run(
["/usr/local/bin/webstamp", "-t", stamp_type] + tmp_files, stdout=subprocess.PIPE
)
ret.append(
{
"info": "Webstamp output:",
"objects": ["<pre>%s</pre>" % cmd_out.stdout.decode("utf-8")],
}
)
ret.append("Webstamp output: <pre>%s</pre>" % cmd_out.stdout.decode("utf-8"))
## Check if output files are there
for f in tmp_files:
outfile = f[0:-4] + "_stamp.pdf"

Check warning on line 4614 in django/geno/views.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 4608-4614
if os.path.isfile(outfile):
os.rename(outfile, f)
else:
Expand All @@ -4612,17 +4626,14 @@
stdout=subprocess.PIPE,
)
# print(pdfcat_out.stdout.decode('utf-8'))
ret.append(
{
"info": "PDFtk output:",
"objects": ["<pre>%s</pre>" % pdfcat_out.stdout.decode("utf-8")],
}
)
ret.append("PDFtk output: <pre>%s</pre>" % pdfcat_out.stdout.decode("utf-8"))
for f in tmp_files:
os.remove(f)
if os.path.isfile(tmp_file.name):
logger.info(" / ".join(ret))
return tmp_file.name
logger.warning(f"No stamped file found {tmp_file}: {' / '.join(ret)}")
return ret

Check warning on line 4636 in django/geno/views.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 4629-4636


class SysadminView(CohivaAdminViewMixin, TemplateView):
Expand Down
1 change: 1 addition & 0 deletions django/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lint.ignore = [
exclude = [
"NON_PUBLIC",
"geno/python-sepa", ## third party code
"*.bak",
]

[tool.pylint.main]
Expand Down
Loading