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
58887516
Commit
58887516
authored
7 years ago
by
Todd Dembrey
Browse files
Options
Downloads
Patches
Plain Diff
Add much custom code to validate that the required blocks appear
parent
2137dde9
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
opentech/apply/funds/blocks.py
+57
-3
57 additions, 3 deletions
opentech/apply/funds/blocks.py
with
57 additions
and
3 deletions
opentech/apply/funds/blocks.py
+
57
−
3
View file @
58887516
from
collections
import
Counter
from
django.core.exceptions
import
ValidationError
from
django.forms.utils
import
ErrorList
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.text
import
mark_safe
...
...
@@ -14,6 +18,51 @@ class CustomFormFieldsBlock(FormFieldsBlock):
child_blocks
=
[(
block
.
name
,
block
(
group
=
_
(
'
Required
'
)))
for
block
in
MustIncludeFieldBlock
.
__subclasses__
()]
super
().
__init__
(
child_blocks
,
*
args
,
**
kwargs
)
def
clean
(
self
,
value
):
try
:
value
=
super
().
clean
(
value
)
except
ValidationError
as
e
:
error_dict
=
e
.
params
else
:
error_dict
=
dict
()
required_block_names
=
[
block
.
name
for
block
in
MustIncludeFieldBlock
.
__subclasses__
()]
block_types
=
[
block
.
block_type
for
block
in
value
]
missing
=
set
(
required_block_names
)
-
set
(
block_types
)
counted_types
=
Counter
(
block_types
)
duplicates
=
[
name
for
name
,
count
in
counted_types
.
items
()
if
name
in
required_block_names
and
count
>
1
]
all_errors
=
list
()
if
missing
:
all_errors
.
append
(
'
You are missing the following required fields: {}
'
.
format
(
'
,
'
.
join
(
missing
).
title
())
)
if
duplicates
:
all_errors
.
append
(
'
You have duplicates of the following required fields: {}
'
.
format
(
'
,
'
.
join
(
duplicates
).
title
())
)
for
name
in
duplicates
:
for
i
,
block_name
in
enumerate
(
block_types
):
if
block_name
==
name
:
try
:
error_dict
[
i
].
data
[
0
].
params
[
'
info
'
]
=
ErrorList
([
'
Duplicate
'
])
except
KeyError
:
error_dict
[
i
]
=
ErrorList
(
[
ValidationError
(
'
Error
'
,
params
=
{
'
info
'
:
ErrorList
([
'
Duplicate
'
])})]
)
if
all_errors
:
error_dict
[
'
__all__
'
]
=
all_errors
raise
ValidationError
(
'
Error
'
,
params
=
error_dict
)
return
value
class
MustIncludeStatic
(
StaticBlock
):
def
__init__
(
self
,
*
args
,
description
=
''
,
**
kwargs
):
...
...
@@ -24,15 +73,20 @@ class MustIncludeStatic(StaticBlock):
admin_text
=
'
Much be included in the form once.
'
def
render_form
(
self
,
*
args
,
**
kwargs
):
errors
=
kwargs
.
pop
(
'
errors
'
)
if
errors
:
error_message
=
'
<div class=
"
error
"
><input readonly placeholder=
"
{}
"
></div>
'
.
format
(
errors
[
0
])
else
:
error_message
=
''
form
=
super
().
render_form
(
*
args
,
**
kwargs
)
form
=
'
<br>
'
.
join
([
self
.
description
,
form
])
form
=
'
<br>
'
.
join
([
self
.
description
,
form
])
+
error_message
return
mark_safe
(
form
)
class
MustIncludeFieldBlock
(
FormFieldBlock
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
info_name
=
f
'
{
self
.
name
}
_f
ield
'
child_blocks
=
[(
info
_name
,
MustIncludeStatic
(
description
=
self
.
description
))]
info_name
=
f
'
{
self
.
name
.
title
()
}
F
ield
'
child_blocks
=
[(
'
info
'
,
MustIncludeStatic
(
label
=
info_name
,
description
=
self
.
description
))]
super
().
__init__
(
child_blocks
,
*
args
,
**
kwargs
)
...
...
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