Commit 46575a1c authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

force dump_infos_to_fs after handle_uploaded_file

parent c99550fd
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -154,6 +154,7 @@ def handle_uploaded_file(f: File, user: User, force_file_name=None) -> int:
                        "IIIF conversion stopped with status {}".format(status)
                    )  # what to do but cry ?
                call(["chmod", "755", settings.IIIF_DIR, "-R"])
    file.dump_infos_to_fs()
    return file.id


+26 −23
Original line number Diff line number Diff line
@@ -197,6 +197,31 @@ class File(Resource):
    def local_path(self):
        return hash_to_local_path(self.hash)

    def dump_infos_to_fs(self):
        data = {
            "title": self.title,
            "original_name": self.original_name,
            "type": str(self.file_type),
            "hash": self.hash,
            "owner": self.owner.username,
            "collections": [],
            "tags": [],
            "metas": [],
        }
        for collection in self.collections.all():
            path = []
            for ancestor in collection.ancestors():
                path.append(ancestor.title)
            path.append(collection.title)
            data["collections"].append(path)
        for tag in self.tags.all():
            data["tags"].append(tag.uid)
        for prop in self.metadataresourcevalue_set.all().order_by("metadata__title"):
            data["metas"].append({str(prop.metadata): prop.value})
        metas_file_path = "{}.{}.json".format(self.local_path(), self.id)
        with open(metas_file_path, "w") as f:
            json.dump(data, f, indent=2)

    def save(self, *args, **kwargs):
        self.ptr_owner = self.owner
        super(File, self).save(*args, **kwargs)
@@ -351,26 +376,4 @@ class CollectionMembership(models.Model):

@receiver(post_save, sender=File)
def dump_file_infos_to_fs(sender, instance: File, **kwargs):
    data = {
        "title": instance.title,
        "original_name": instance.original_name,
        "type": str(instance.file_type),
        "hash": instance.hash,
        "owner": instance.owner.username,
        "collections": [],
        "tags": [],
        "metas": [],
    }
    for collection in instance.collections.all():
        path = []
        for ancestor in collection.ancestors():
            path.append(ancestor.title)
        path.append(collection.title)
        data["collections"].append(path)
    for tag in instance.tags.all():
        data["tags"].append(tag.uid)
    for prop in instance.metadataresourcevalue_set.all().order_by("metadata__title"):
        data["metas"].append({str(prop.metadata): prop.value})
    metas_file_path = "{}.{}.json".format(instance.local_path(), instance.id)
    with open(metas_file_path, "w") as f:
        json.dump(data, f, indent=2)
    instance.dump_infos_to_fs()