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
3d8ebb63
Commit
3d8ebb63
authored
6 years ago
by
Todd Dembrey
Committed by
Fredrik Jonsson
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add the concept of single include block as alternative to required
parent
b4289e4b
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/funds/blocks.py
+14
-2
14 additions, 2 deletions
opentech/apply/funds/blocks.py
opentech/apply/utils/blocks.py
+18
-9
18 additions, 9 deletions
opentech/apply/utils/blocks.py
with
32 additions
and
11 deletions
opentech/apply/funds/blocks.py
+
14
−
2
View file @
3d8ebb63
...
...
@@ -6,7 +6,16 @@ from django.utils.translation import ugettext_lazy as _
from
addressfield.fields
import
AddressField
from
opentech.apply.categories.blocks
import
CategoryQuestionBlock
from
opentech.apply.stream_forms.blocks
import
FormFieldsBlock
from
opentech.apply.utils.blocks
import
MustIncludeFieldBlock
,
CustomFormFieldsBlock
,
RichTextFieldBlock
from
opentech.apply.utils.blocks
import
(
CustomFormFieldsBlock
,
MustIncludeFieldBlock
,
RichTextFieldBlock
,
SingleIncludeBlock
,
)
class
ApplicationSingleIncludeFieldBlock
(
SingleIncludeBlock
):
pass
class
ApplicationMustIncludeFieldBlock
(
MustIncludeFieldBlock
):
...
...
@@ -36,7 +45,7 @@ class EmailBlock(ApplicationMustIncludeFieldBlock):
icon
=
'
mail
'
class
AddressFieldBlock
(
Application
Must
IncludeFieldBlock
):
class
AddressFieldBlock
(
Application
Single
IncludeFieldBlock
):
name
=
'
address
'
description
=
'
The postal address of the user
'
...
...
@@ -105,6 +114,9 @@ class ApplicationCustomFormFieldsBlock(CustomFormFieldsBlock, FormFieldsBlock):
category
=
CategoryQuestionBlock
(
group
=
_
(
'
Custom
'
))
rich_text
=
RichTextFieldBlock
(
group
=
_
(
'
Fields
'
))
required_blocks
=
ApplicationMustIncludeFieldBlock
.
__subclasses__
()
single_blocks
=
ApplicationSingleIncludeFieldBlock
.
__subclasses__
()
REQUIRED_BLOCK_NAMES
=
[
block
.
name
for
block
in
ApplicationMustIncludeFieldBlock
.
__subclasses__
()]
SINGLE_BLOCK_NAMES
=
[
block
.
name
for
block
in
ApplicationSingleIncludeFieldBlock
.
__subclasses__
()]
This diff is collapsed.
Click to expand it.
opentech/apply/utils/blocks.py
+
18
−
9
View file @
3d8ebb63
...
...
@@ -8,7 +8,7 @@ from django.utils.text import mark_safe
from
wagtail.core.blocks
import
StaticBlock
,
StreamValue
,
StreamBlock
from
opentech.apply.stream_forms.blocks
import
FormFieldBlock
,
TextFieldBlock
from
opentech.apply.stream_forms.blocks
import
FormFieldBlock
,
OptionalFormFieldBlock
,
TextFieldBlock
from
opentech.apply.utils.options
import
RICH_TEXT_WIDGET
...
...
@@ -45,10 +45,13 @@ class RichTextFieldBlock(TextFieldBlock):
class
CustomFormFieldsBlock
(
StreamBlock
):
rich_text
=
RichTextFieldBlock
(
group
=
_
(
'
Fields
'
))
required_blocks
=
[]
single_blocks
=
[]
def
__init__
(
self
,
*
args
,
**
kwargs
):
child_blocks
=
[(
block
.
name
,
block
(
group
=
_
(
'
Required
'
)))
for
block
in
self
.
required_blocks
]
child_blocks
+=
[(
block
.
name
,
block
(
group
=
_
(
'
Custom
'
)))
for
block
in
self
.
single_blocks
]
self
.
required_block_names
=
[
block
.
name
for
block
in
self
.
required_blocks
]
self
.
single_block_names
=
[
block
.
name
for
block
in
self
.
single_blocks
]
+
self
.
required_block_names
super
().
__init__
(
child_blocks
,
*
args
,
**
kwargs
)
...
...
@@ -65,7 +68,7 @@ class CustomFormFieldsBlock(StreamBlock):
duplicates
=
[
name
for
name
in
find_duplicates
(
block_types
)
if
name
in
self
.
required
_block_names
if
name
in
self
.
single
_block_names
]
all_errors
=
list
()
...
...
@@ -76,7 +79,7 @@ class CustomFormFieldsBlock(StreamBlock):
if
duplicates
:
all_errors
.
append
(
'
You have duplicates of the following
required
fields: {}
'
.
format
(
'
,
'
.
join
(
prettify_names
(
duplicates
)))
'
You have duplicates of the following
non duplicate
fields: {}
'
.
format
(
'
,
'
.
join
(
prettify_names
(
duplicates
)))
)
for
i
,
block_name
in
enumerate
(
block_types
):
if
block_name
in
duplicates
:
...
...
@@ -110,7 +113,7 @@ class CustomFormFieldsBlock(StreamBlock):
return
StreamValue
(
self
,
value
,
is_lazy
=
True
)
class
Must
IncludeStatic
(
StaticBlock
):
class
Single
IncludeStatic
(
StaticBlock
):
"""
Helper block which displays additional information about the must include block and
helps display the error in a noticeable way.
"""
...
...
@@ -137,15 +140,21 @@ class MustIncludeStatic(StaticBlock):
return
(
'
wagtail.core.blocks.static_block.StaticBlock
'
,
(),
{})
class
MustIncludeFieldBlock
(
FormFieldBlock
):
"""
Any block inheriting from this will need to be included in the application forms
This data will also be available to query on the submission object
"""
class
SingleIncludeMixin
:
def
__init__
(
self
,
*
args
,
**
kwargs
):
info_name
=
f
'
{
self
.
name
.
title
()
}
Field
'
child_blocks
=
[(
'
info
'
,
Must
IncludeStatic
(
label
=
info_name
,
description
=
self
.
description
))]
child_blocks
=
[(
'
info
'
,
Single
IncludeStatic
(
label
=
info_name
,
description
=
self
.
description
))]
super
().
__init__
(
child_blocks
,
*
args
,
**
kwargs
)
class
SingleIncludeBlock
(
SingleIncludeMixin
,
OptionalFormFieldBlock
):
"""
A block that is only allowed to be included once in the form, but is optional
"""
class
MustIncludeFieldBlock
(
SingleIncludeMixin
,
FormFieldBlock
):
"""
Any block inheriting from this will need to be included in the application forms
This data will also be available to query on the submission object
"""
def
get_field_kwargs
(
self
,
struct_value
):
kwargs
=
super
().
get_field_kwargs
(
struct_value
)
kwargs
[
'
required
'
]
=
True
...
...
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