[Django] #32452: Simplify storing strings/bytes in a Storage

20 views
Skip to first unread message

Django

unread,
Feb 16, 2021, 9:31:07 AM2/16/21
to django-...@googlegroups.com
#32452: Simplify storing strings/bytes in a Storage
------------------------------------------------+------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: File uploads/storage | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Docs: https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects

> path = default_storage.save('path/to/file', ContentFile(b'new content'))

Things would be bit easier, if `save()` would support strings/bytes
directly.

This way a developer needs to type and import less code:

```
path = default_storage.save('path/to/file', b'new content')
```

```
path = default_storage.save('path/to/file', 'new unicode string, encoded
automatically with utf-8')
```

Would you accept a patch to `Storage.save()`?

--
Ticket URL: <https://code.djangoproject.com/ticket/32452>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 16, 2021, 9:31:38 AM2/16/21
to django-...@googlegroups.com
#32452: Simplify storing strings/bytes in a Storage
-------------------------------------+-------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Thomas Güttler:

Old description:

> Docs: https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects
>
> > path = default_storage.save('path/to/file', ContentFile(b'new
> content'))
>
> Things would be bit easier, if `save()` would support strings/bytes
> directly.
>
> This way a developer needs to type and import less code:
>
> ```
> path = default_storage.save('path/to/file', b'new content')
> ```
>
> ```
> path = default_storage.save('path/to/file', 'new unicode string, encoded
> automatically with utf-8')
> ```
>
> Would you accept a patch to `Storage.save()`?

New description:

Docs: https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects

> path = default_storage.save('path/to/file', ContentFile(b'new content'))

Things would be bit easier, if `save()` would support strings/bytes
directly.

This way a developer needs to type and import less code:

{{{
path = default_storage.save('path/to/file', b'new content')
}}}

{{{
path = default_storage.save('path/to/file', 'new unicode string, encoded
automatically with utf-8')
}}}

Would you accept a patch to `Storage.save()`?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32452#comment:1>

Django

unread,
Feb 16, 2021, 9:51:18 AM2/16/21
to django-...@googlegroups.com
#32452: Simplify storing strings/bytes in a Storage
-------------------------------------+-------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Thomas Güttler:

Old description:

> Docs: https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects


>
> > path = default_storage.save('path/to/file', ContentFile(b'new
> content'))
>
> Things would be bit easier, if `save()` would support strings/bytes
> directly.
>
> This way a developer needs to type and import less code:
>
> {{{
> path = default_storage.save('path/to/file', b'new content')
> }}}
>
> {{{
> path = default_storage.save('path/to/file', 'new unicode string, encoded
> automatically with utf-8')
> }}}
>
> Would you accept a patch to `Storage.save()`?

New description:

Docs: https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects

> path = default_storage.save('path/to/file', ContentFile(b'new content'))

Things would be bit easier, if `save()` would support strings/bytes
directly.

This way a developer needs to type and import less code:

{{{
path = default_storage.save('path/to/file', b'new content')
}}}

{{{
path = default_storage.save('path/to/file', 'new unicode string, encoded
automatically with utf-8')
}}}

Would you accept a patch to `Storage.save()`?

Background: I am writing a unittest which mocked away the storage calls to
save() will put the data into the mock.

Then I check if the data is the way I want it.

Now it gets complicated again: I need to access the data which is inside
`ContentFile` grrr.

This would be much easier if you could add strings/bytes to the `save()`
directly.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32452#comment:2>

Django

unread,
Feb 16, 2021, 2:50:54 PM2/16/21
to django-...@googlegroups.com
#32452: Simplify storing strings/bytes in a Storage
-------------------------------------+-------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: closed

Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

> Things would be bit easier, if save() would support strings/bytes
directly.

IMO it would be confusing. `Storage.save()` save content to the file, so
it supports files and any file-like objects, e.g. `StringIO` or `BytesIO`.
Making mocking easier is not a sufficient argument for me, it's also not
clear why this is much harder with the current API.

You can start a discussion on DevelopersMailingList if you don't agree.

--
Ticket URL: <https://code.djangoproject.com/ticket/32452#comment:3>

Reply all
Reply to author
Forward
0 new messages