Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hypha
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ots
hypha
Commits
f59a2e84
Commit
f59a2e84
authored
6 years ago
by
Todd Dembrey
Browse files
Options
Downloads
Patches
Plain Diff
Totally rework how the multifile upload works
parent
7354ea71
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
opentech/apply/stream_forms/fields.py
+47
-8
47 additions, 8 deletions
opentech/apply/stream_forms/fields.py
opentech/apply/stream_forms/templates/stream_forms/fields/multi_file_field.html
+13
-0
13 additions, 0 deletions
...forms/templates/stream_forms/fields/multi_file_field.html
with
60 additions
and
8 deletions
opentech/apply/stream_forms/fields.py
+
47
−
8
View file @
f59a2e84
from
django.forms
import
FileInput
,
FileField
from
django.forms
import
Clearable
FileInput
,
FileField
,
CheckboxInput
class
MultiFileInput
(
FileInput
):
class
MultiFileInput
(
Clearable
FileInput
):
"""
"""
File Input only returns one file from its clean method.
File Input only returns one file from its clean method.
This passes all files through the clean method and means we have a list of
This passes all files through the clean method and means we have a list of
files available for post processing
files available for post processing
"""
"""
def
__init__
(
self
,
*
args
,
attrs
=
{},
**
kwargs
):
template_name
=
'
stream_forms/fields/multi_file_field.html
'
attrs
[
'
multiple
'
]
=
True
super
().
__init__
(
*
args
,
attrs
=
attrs
,
**
kwargs
)
input_text
=
''
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
multiple
=
kwargs
.
pop
(
'
multiple
'
,
True
)
super
().
__init__
(
*
args
,
**
kwargs
)
def
is_initial
(
self
,
value
):
is_initial
=
super
().
is_initial
return
all
(
is_initial
(
file
)
for
file
in
value
)
def
render
(
self
,
name
,
value
,
attrs
=
None
):
if
self
.
multiple
:
attrs
[
'
multiple
'
]
=
'
multiple
'
return
super
().
render
(
name
,
value
,
attrs
)
def
value_from_datadict
(
self
,
data
,
files
,
name
):
def
value_from_datadict
(
self
,
data
,
files
,
name
):
return
files
.
getlist
(
name
)
if
hasattr
(
files
,
'
getlist
'
):
upload
=
files
.
getlist
(
name
)
else
:
upload
=
files
.
get
(
name
)
if
not
isinstance
(
upload
,
list
):
upload
=
[
upload
]
checkbox_name
=
self
.
clear_checkbox_name
(
name
)
checkboxes
=
{
k
for
k
in
data
if
checkbox_name
in
k
}
cleared
=
{
i
for
i
,
checkbox
in
enumerate
(
checkboxes
)
if
CheckboxInput
().
value_from_datadict
(
data
,
files
,
checkbox
)
}
return
{
'
files
'
:
upload
,
'
cleared
'
:
cleared
,
}
class
MultiFileField
(
FileField
):
class
MultiFileField
(
FileField
):
widget
=
MultiFileInput
widget
=
MultiFileInput
def
clean
(
self
,
value
,
initial
):
def
clean
(
self
,
value
,
initial
):
if
not
value
:
files
=
value
[
'
files
'
]
cleared
=
value
[
'
cleared
'
]
if
not
files
and
not
cleared
:
return
initial
return
initial
return
[
FileField
().
clean
(
file
,
initial
)
for
file
in
value
]
new
=
[
FileField
().
clean
(
file
,
initial
)
for
file
in
files
]
old
=
[
file
for
i
,
file
in
enumerate
(
initial
)
if
i
not
in
cleared
]
return
old
+
new
This diff is collapsed.
Click to expand it.
opentech/apply/stream_forms/templates/stream_forms/fields/multi_file_field.html
0 → 100644
+
13
−
0
View file @
f59a2e84
{% if widget.is_initial %}{{ widget.initial_text }}:
<p>
{{ widget.clear_checkbox_label }}
</p>
{% for file in widget.value %}
<p>
<input
type=
"checkbox"
name=
"{{ widget.checkbox_name }}-{{ forloop.counter }}"
id=
"{{ widget.checkbox_id }}-{{ forloop.counter }}"
>
<label
for=
"{{ widget.checkbox_id }}-{{ forloop.counter }}"
></label>
<a
href=
"{{ file.url }}"
>
{{ file.filename }}
</a>
</p>
{% endfor %}
{{ widget.input_text }}{% endif %}
<input
type=
"{{ widget.type }}"
name=
"{{ widget.name }}"
{%
include
"
django
/
forms
/
widgets
/
attrs.html
"
%}
>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment