Skip to content

Comments

Fix upload_attachments: specify file name in parameter dict#15

Open
raph-topo wants to merge 1 commit intoricpol:mainfrom
raph-topo:fix-upload-attachments
Open

Fix upload_attachments: specify file name in parameter dict#15
raph-topo wants to merge 1 commit intoricpol:mainfrom
raph-topo:fix-upload-attachments

Conversation

@raph-topo
Copy link

Rationale: path on disk may not exist or may not be significant (as with NamedTemporaryFile()).

Rationale: path on disk may not exist or may not be significant (as with `NamedTemporaryFile()`).
@ricpol
Copy link
Owner

ricpol commented Feb 18, 2026

Non sure about this - a NamedTemporaryFile is just a file that happens to be managed by tempfile... but you may still get its path with "f.name".
It would be different if you wanted to stream the attachment upload, like this - this, however, involves calling the Requests api with a "data" parameter, which we don't use at the moment.
Support for streaming may be useful though - don't know if the underlying Grist api allows this. I'll add this to my todo list.

or - maybe I didn't understand what you are trying to do here? What use case do you have in mind?

@raph-topo
Copy link
Author

raph-topo commented Feb 21, 2026

Non sure about this - a NamedTemporaryFile is just a file that happens to be managed by tempfile... but you may still get its path with "f.name".

Yes, a random name, which it makes no sense to store in Grist. (But Pygrister download_attachment & upload_attachments seem to need NamedTemporaryFile instead of TemporaryFile if I recall my first tests.)

or - maybe I didn't understand what you are trying to do here? What use case do you have in mind?

I'm using my pygrister fork including this PR to copy attachments from one Grist doc to another, without loosing their name:

        ...
        for att in fr.attachments:
            with NamedTemporaryFile() as file:
                fr.grist.download_attachment(
                    filename=Path(file.name),
                    attachment_id=att.id,
                )
                to_id: int = to.grist.upload_attachments(
                    {att.fields.fileName: Path(file.name)}
                )[1][0]
                ...

(fr.grist & to.grist are instances pygrister.api.GristApi with .open_session() to one Grist doc each)

NamedTemporaryFile has the huge advantage of cleaning itself up when the with block ends. But without the PR, upload_attachments() would store and have Grist display that random name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants