-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic paperless import interface
- Loading branch information
Showing
8 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"])) |
1 change: 1 addition & 0 deletions
1
fragdenstaat_de/fds_paperless/templates/fds_paperless/add_postal_message.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends "foirequest/upload_postal_message.html" %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters