From 909f202136fd2f235b923ea2a9c480106d92f517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sun, 12 Mar 2017 15:39:32 +0100 Subject: Split document content into its own table to avoid having to load it each time. Move document creation into the model --- archivist/cli.py | 14 ++++---------- archivist/model.py | 23 ++++++++++++++++++----- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/archivist/cli.py b/archivist/cli.py index b10e11b..31646aa 100644 --- a/archivist/cli.py +++ b/archivist/cli.py @@ -237,8 +237,10 @@ def doc(): def add_doc(file, tags, create_tags, ignore_missing_tags): """Add a new document together with the given tags.""" - from .model import Document, Tag, db, DocumentTag + from .model import db, Document + import magic + mimetype = magic.from_file(file.name, mime=True) with db.atomic(): if tags: @@ -247,15 +249,7 @@ def add_doc(file, tags, create_tags, ignore_missing_tags): else: tags = fetch_tags(tags, ignore_missing_tags) - mimetype = magic.from_file(file.name, mime=True) - - doc = Document.create(content = file.read(), - file_type = mimetype, - original_path = file.name, - direction = Document.Direction.IN) - - for t in tags: - DocumentTag.create(document = doc, tag = t) + Document.create_from_file(file, tags, direction = Document.Direction.IN, file_type = mimetype) @doc.command('find') @click.argument('tags', type=TAG, nargs=-1) diff --git a/archivist/model.py b/archivist/model.py index cbbc462..86c2cf1 100644 --- a/archivist/model.py +++ b/archivist/model.py @@ -62,6 +62,10 @@ class CompressedField(_CompressedField): def python_value(self, value): return value if value is None else self.decompress(value) +@table +class DocumentContent(BaseModel): + blob = CompressedField() + @table class Document(BaseModel): @unique @@ -69,19 +73,27 @@ class Document(BaseModel): IN = 0 OUT = 1 - content = CompressedField() created = DateField(default=datetime.date.today) inserted = DateTimeField(default=datetime.datetime.now) description = CharField(null=True) original_path = CharField(null=True) file_type = CharField(null=True) direction = EnumField(Direction, null=True) + content = ForeignKeyField(DocumentContent, related_name = 'document') @classmethod - def matches(cls, prefix, value): - return query_pseudo_prefix(prefix, value) or Document.id << ( - DocumentTag.select(DocumentTag.document) - .join(Tag, on=Tag.matches(prefix, value))) + @db.atomic() + def create_from_file(cls, from_file, tags, **kwargs): + content = DocumentContent.create(blob=from_file.read()) + + doc = cls.create(content = content, + original_path = from_file.name, + **kwargs) + + for t in tags: + DocumentTag.create(document = doc, tag = t) + + return doc @table class Prefix(BaseModel): @@ -95,6 +107,7 @@ class Prefix(BaseModel): def create(cls, **query): inst = super().create(**query) Tag.create_prefix_default(inst) + return inst @property def default_tag(self): -- cgit v1.2.3