Skip to content

Commit

Permalink
Add basic paperless import interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Jun 28, 2024
1 parent d6e8aeb commit 45c07b1
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 0 deletions.
Empty file.
57 changes: 57 additions & 0 deletions fragdenstaat_de/fds_paperless/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import re
from datetime import date

from django import forms
from django.core.files.base import ContentFile

from froide.foirequest.forms.message import PostalReplyForm
from froide.helper.widgets import BootstrapCheckboxSelectMultiple

from .paperless import get_document_data


class PaperlessPostalReplyForm(PostalReplyForm):
files = None
paperless_ids = forms.MultipleChoiceField(
label="Paperless Dokumente",
widget=BootstrapCheckboxSelectMultiple,
required=True,
)

def __init__(self, *args, **kwargs):
paperless_docs = kwargs.pop("paperless_docs")
super().__init__(*args, **kwargs)
self.fields["paperless_ids"].choices = [
(
doc["id"],
"{date}: ({title}) - {content}...".format(
date=doc["added"],
title=doc["title"],
content=doc["content"][:100],
),
)
for doc in paperless_docs
]
if paperless_docs:
DATE_RE = re.compile(r"(\d{1,2})\.(\d{1,2})\.(\d{4})")
match = DATE_RE.search(paperless_docs[0]["content"])
if match:
self.fields["date"].initial = date(
int(match.group(3)), int(match.group(2)), int(match.group(1))
)

def clean(self):
# Do not enforce presence of either files or text
pass

def save(self):
message = super().save()
files = []
for paperless_id in self.cleaned_data["paperless_ids"]:
meta_data, file_contents = get_document_data(paperless_id)
file = ContentFile(file_contents, name=meta_data["original_file_name"])
file.content_type = "application/pdf"
files.append(file)

self.save_attachments(files, message)
return message
37 changes: 37 additions & 0 deletions fragdenstaat_de/fds_paperless/paperless.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from datetime import timedelta

from django.conf import settings
from django.utils import timezone

import requests


def get_paperless_client():
session = requests.Session()
session.headers.update({"Authorization": settings.PAPERLESS_API_TOKEN})
return session


def get_document_data(paperless_document_id):
API_URL = settings.PAPERLESS_API_URL + "/documents/{}/".format(
paperless_document_id
)
client = get_paperless_client()
meta_data = client.get(API_URL).json()

DOCUMENT_URL = settings.PAPERLESS_API_URL + "/documents/{}/download/".format(
paperless_document_id
)
file_data = client.get(DOCUMENT_URL).content

return meta_data, file_data


def list_documents():
client = get_paperless_client()
last_week = timezone.now() - timedelta(days=7)
API_URL = settings.PAPERLESS_API_URL + "/documents/?created__date__gt={}".format(
last_week.date()
)
data = client.get(API_URL).json()
return list(reversed(data["results"]))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "foirequest/upload_postal_message.html" %}
16 changes: 16 additions & 0 deletions fragdenstaat_de/fds_paperless/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.urls import path

from .views import add_postal_message, paperless_start

urlpatterns = [
path(
"<int:pk>/",
paperless_start,
name="paperless_index",
),
path(
"import/<slug:slug>/",
add_postal_message,
name="paperless_import",
),
]
44 changes: 44 additions & 0 deletions fragdenstaat_de/fds_paperless/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.shortcuts import get_object_or_404, redirect, render

from froide.foirequest.auth import can_write_foirequest
from froide.foirequest.decorators import allow_write_foirequest
from froide.foirequest.models import FoiRequest
from froide.helper.utils import render_403

from .forms import PaperlessPostalReplyForm
from .paperless import list_documents


def paperless_start(request, pk):
foirequest = get_object_or_404(FoiRequest, pk=pk)
if not can_write_foirequest(foirequest, request):
return render_403(request)
return redirect("paperless_import", slug=foirequest.slug)


@allow_write_foirequest
def add_postal_message(request, foirequest):
paperless_docs = list_documents()
if request.method == "POST":
form = PaperlessPostalReplyForm(
request.POST, foirequest=foirequest, paperless_docs=paperless_docs
)
if form.is_valid():
message = form.save()
FoiRequest.message_received.send(
sender=foirequest, message=message, user=request.user
)
return redirect(message)
else:
form = PaperlessPostalReplyForm(
foirequest=foirequest, paperless_docs=paperless_docs
)

return render(
request,
"fds_paperless/add_postal_message.html",
{
"object": foirequest,
"form": form,
},
)
4 changes: 4 additions & 0 deletions fragdenstaat_de/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def INSTALLED_APPS(self):
"fragdenstaat_de.fds_mailing.apps.FdsMailingConfig",
"fragdenstaat_de.fds_ogimage.apps.FdsOgImageConfig",
"fragdenstaat_de.fds_fximport.apps.FdsFxImportConfig",
"fragdenstaat_de.fds_paperless",
# Additional CMS plugins
"djangocms_text_ckeditor",
"djangocms_picture",
Expand Down Expand Up @@ -844,3 +845,6 @@ def FROIDE_CONFIG(self):
DJANGOCMS_ICON_SETS = [
("fontawesome4", "fa", "Font Awesome 4", "4.7.0"),
]

PAPERLESS_API_URL = os.environ.get("PAPERLESS_API_URL", "")
PAPERLESS_API_TOKEN = os.environ.get("PAPERLESS_API_TOKEN", "")
1 change: 1 addition & 0 deletions fragdenstaat_de/theme/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
path("payment/", include("froide_payment.urls")),
path("contractor/", include("contractor.urls")),
path("fax/", include("froide_fax.urls")),
path("paperless/", include("fragdenstaat_de.fds_paperless.urls")),
path("newsletter/update/", include("fragdenstaat_de.fds_newsletter.urls")),
path("newsletter/archive/", include("fragdenstaat_de.fds_mailing.urls")),
path(
Expand Down

0 comments on commit 45c07b1

Please sign in to comment.