Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide file picker #2

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# bw-file-resubmit

**bw-file-resubmit** is a drop-in replacement for [**django-file-resubmit**](https://github.com/un1t/django-file-resubmit), which appears to be abandoned.
**bw-file-resubmit** is a drop-in replacement for [**django-file-resubmit**](https://github.com/un1t/django-file-resubmit), maintained by the BookWyrm project.

In a Django project you may have forms using `FileField` or `ImageField`. Everything works great, but
when a `ValidationError` is raised, or you want to add some other check before finally submitting the form, you have to reselect all files and images again. This is because HTML `<input type="file">` fields cannot be prepopulated with data due to basic browser security.

**bw-file-resubmit** solves this problem by caching the file and adding it again in the background. It works with `FileField`, `ImageField` and `sorl.thumbnail.ImageField` (see [`sorl-thumbnail`](https://github.com/jazzband/sorl-thumbnail).
**bw-file-resubmit** solves this problem by caching the file and adding it again in the background. It works with `FileField`, `ImageField` and `sorl.thumbnail.ImageField` (see [sorl-thumbnail](https://github.com/jazzband/sorl-thumbnail)).

## How does it work?

Expand All @@ -18,11 +18,9 @@ Special widgets can be used to replace `FileField` and `ImageField`. When you su

## Installation

SOON

<!-- ```sh
```sh
pip install bw-file-resubmit
``` -->
```

## Configuration

Expand Down Expand Up @@ -69,6 +67,9 @@ class Page(models.Model):
### Form widgets

#### Ordinary form

forms.py

```py
from django.forms import ModelForm
from file_resubmit.widgets import ResubmitImageWidget, ResubmitFileWidget
Expand All @@ -89,6 +90,11 @@ class Book(forms.Form):
```

#### Admin form

admin.py

**NOTE:** `admin.AdminResubmitFileWidget` has been dropped from `bw-file-resubmit`. If you need to update a legacy `django-file-resubmit` project, use `widgets.ResubmitFileWidget` instead.

```py
from django.forms import ModelForm
from file_resubmit.admin import AdminResubmitImageWidget,
Expand All @@ -112,6 +118,8 @@ admin.site.register(Page, PageAdmin)

### Use as a model mixin in admin

admin.py

```py
from django.contrib import admin
from file_resubmit.admin import AdminResubmitMixin
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[project]
name = "bw-file-resubmit"
version = "0.6.0rc1"
version = "0.6.0rc2"
license = {text = "MIT License"}
authors = [
{ name="Ilya Shalyapin" },
{ name="Ilya Shalyapin", email="ishalyapin@gmail.com"},
{ name="Hugh Rundle", email="hugh@hughrundle.net" }
]
maintainers = [
Expand Down
7 changes: 5 additions & 2 deletions src/file_resubmit/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ def render(
self, name, value, attrs=None, **kwargs
): # pylint: disable=unused-argument
"""override render function to add hidden input"""
output = ClearableFileInput.render(self, name, value, attrs)
output += self.output_extra_data(value)
if self.cache_key:
output = self.output_extra_data(value)
else:
output = ClearableFileInput.render(self, name, value, attrs)
output += self.output_extra_data(value)
return mark_safe(output)


Expand Down