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
e4a78534
Commit
e4a78534
authored
3 years ago
by
sandeepsajan0
Committed by
Fredrik Jonsson
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add allowed extension validation in public form
parent
9ca6ef80
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
hypha/public/forms/models.py
+19
-0
19 additions, 0 deletions
hypha/public/forms/models.py
with
19 additions
and
0 deletions
hypha/public/forms/models.py
+
19
−
0
View file @
e4a78534
...
@@ -5,6 +5,7 @@ from django.conf import settings
...
@@ -5,6 +5,7 @@ from django.conf import settings
from
django.core.files.storage
import
get_storage_class
from
django.core.files.storage
import
get_storage_class
from
django.core.serializers.json
import
DjangoJSONEncoder
from
django.core.serializers.json
import
DjangoJSONEncoder
from
django.db
import
models
from
django.db
import
models
from
django.core.exceptions
import
ValidationError
from
django.forms
import
FileField
,
FileInput
from
django.forms
import
FileField
,
FileInput
from
django.utils.decorators
import
method_decorator
from
django.utils.decorators
import
method_decorator
from
django.utils.translation
import
gettext_lazy
as
_
from
django.utils.translation
import
gettext_lazy
as
_
...
@@ -44,6 +45,19 @@ class ExtendedFormBuilder(FormBuilder):
...
@@ -44,6 +45,19 @@ class ExtendedFormBuilder(FormBuilder):
def
create_document_field
(
self
,
field
,
options
):
def
create_document_field
(
self
,
field
,
options
):
return
FileField
(
widget
=
FileInput
(
attrs
=
{
'
accept
'
:
settings
.
FILE_ALLOWED_EXTENSIONS
}),
**
options
)
return
FileField
(
widget
=
FileInput
(
attrs
=
{
'
accept
'
:
settings
.
FILE_ALLOWED_EXTENSIONS
}),
**
options
)
def
clean
(
self
):
cleaned_data
=
self
.
cleaned_data
for
name
,
field
in
self
.
fields
.
items
():
if
isinstance
(
field
,
FileField
):
file_data
=
cleaned_data
[
name
]
if
file_data
:
file_name
=
file_data
.
name
file_extension
=
file_name
.
split
(
'
.
'
)[
-
1
]
if
file_extension
not
in
settings
.
FILE_ALLOWED_EXTENSIONS
:
self
.
add_error
(
name
,
f
"
File extension
'
{
file_extension
}
'
is not allowed.
"
f
"
Allowed extensions are:
'
{
'
,
'
.
join
(
settings
.
FILE_ALLOWED_EXTENSIONS
)
}
'"
)
return
self
.
cleaned_data
@method_decorator
(
never_cache
,
name
=
'
serve
'
)
@method_decorator
(
never_cache
,
name
=
'
serve
'
)
class
FormPage
(
AbstractEmailForm
,
BasePage
):
class
FormPage
(
AbstractEmailForm
,
BasePage
):
...
@@ -70,6 +84,11 @@ class FormPage(AbstractEmailForm, BasePage):
...
@@ -70,6 +84,11 @@ class FormPage(AbstractEmailForm, BasePage):
],
"
Email
"
),
],
"
Email
"
),
]
]
def
get_form
(
self
,
*
args
,
**
kwargs
):
form
=
super
(
FormPage
,
self
).
get_form
(
*
args
,
**
kwargs
)
form
.
clean
=
self
.
form_builder
.
clean
.
__get__
(
form
)
return
form
def
process_form_submission
(
self
,
form
):
def
process_form_submission
(
self
,
form
):
cleaned_data
=
form
.
cleaned_data
cleaned_data
=
form
.
cleaned_data
...
...
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