From 909f202136fd2f235b923ea2a9c480106d92f517 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann 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/model.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'archivist/model.py') 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-54-g00ecf