2016-03-03 20:52:42 +00:00
|
|
|
import json
|
2016-01-14 19:47:57 +00:00
|
|
|
import os
|
2016-01-23 02:57:29 +00:00
|
|
|
import time
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2016-03-03 20:52:42 +00:00
|
|
|
from django.core import serializers
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-03-03 20:52:42 +00:00
|
|
|
from documents.models import Document, Correspondent, Tag
|
2016-01-14 19:47:57 +00:00
|
|
|
from paperless.db import GnuPG
|
|
|
|
|
|
2016-02-11 22:05:38 +00:00
|
|
|
from ...mixins import Renderable
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-02-11 22:05:38 +00:00
|
|
|
|
|
|
|
|
class Command(Renderable, BaseCommand):
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
help = """
|
|
|
|
|
Decrypt and rename all files in our collection into a given target
|
2016-03-03 20:52:42 +00:00
|
|
|
directory. And include a manifest file containing document data for
|
|
|
|
|
easy import.
|
2016-01-14 19:47:57 +00:00
|
|
|
""".replace(" ", "")
|
|
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
|
parser.add_argument("target")
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
2016-03-03 20:52:42 +00:00
|
|
|
self.target = None
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
|
|
self.target = options["target"]
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(self.target):
|
|
|
|
|
raise CommandError("That path doesn't exist")
|
|
|
|
|
|
|
|
|
|
if not os.access(self.target, os.W_OK):
|
|
|
|
|
raise CommandError("That path doesn't appear to be writable")
|
|
|
|
|
|
|
|
|
|
if not settings.PASSPHRASE:
|
|
|
|
|
settings.PASSPHRASE = input("Please enter the passphrase: ")
|
|
|
|
|
|
2016-03-03 20:52:42 +00:00
|
|
|
documents = Document.objects.all()
|
|
|
|
|
document_map = {d.pk: d for d in documents}
|
|
|
|
|
manifest = json.loads(serializers.serialize("json", documents))
|
|
|
|
|
for document_dict in manifest:
|
|
|
|
|
|
|
|
|
|
document = document_map[document_dict["pk"]]
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-02-15 22:38:18 +00:00
|
|
|
target = os.path.join(self.target, document.file_name)
|
2016-03-03 20:52:42 +00:00
|
|
|
document_dict["__exported_file_name__"] = target
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-02-14 16:09:52 +00:00
|
|
|
print("Exporting: {}".format(target))
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
with open(target, "wb") as f:
|
2016-01-30 01:18:52 +00:00
|
|
|
f.write(GnuPG.decrypted(document.source_file))
|
2016-01-23 03:22:15 +00:00
|
|
|
t = int(time.mktime(document.created.timetuple()))
|
|
|
|
|
os.utime(target, times=(t, t))
|
2016-03-03 20:52:42 +00:00
|
|
|
|
|
|
|
|
manifest += json.loads(
|
|
|
|
|
serializers.serialize("json", Correspondent.objects.all()))
|
|
|
|
|
|
|
|
|
|
manifest += json.loads(serializers.serialize(
|
|
|
|
|
"json", Tag.objects.all()))
|
|
|
|
|
|
|
|
|
|
with open(os.path.join(self.target, "manifest.json"), "w") as f:
|
|
|
|
|
json.dump(manifest, f, indent=2)
|