Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
django-cleavejs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Libraries
django-cleavejs
Commits
3048a6cf
Verified
Commit
3048a6cf
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Rewrite as dataclass and support blocks and delimiters
parent
21507c67
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
dj_cleavejs/widgets.py
+27
-3
27 additions, 3 deletions
dj_cleavejs/widgets.py
with
27 additions
and
3 deletions
dj_cleavejs/widgets.py
+
27
−
3
View file @
3048a6cf
from
typing
import
Any
,
Dict
,
List
,
Optional
import
json
from
dataclasses
import
InitVar
,
dataclass
from
typing
import
Any
,
Dict
,
List
,
Optional
,
Sequence
,
Union
from
django.conf
import
settings
from
django.forms.widgets
import
TextInput
...
...
@@ -7,7 +9,16 @@ from django.forms.widgets import TextInput
__all__
=
(
"
CleaveWidget
"
,)
@dataclass
class
CleaveWidget
(
TextInput
):
attr_prefix
:
str
=
"
data-dj-cleavejs
"
blocks
:
Optional
[
Sequence
[
int
]]
=
None
delimiter
:
Optional
[
Union
[
str
,
Sequence
[
str
]]]
=
None
delimiter_lazy_show
:
bool
=
True
attrs
:
InitVar
[
Optional
[
Dict
[
str
,
str
]]]
=
None
@property
def
media
(
self
)
->
Dict
[
str
,
List
[
str
]]:
js
=
[
"
dj_cleave.js
"
]
...
...
@@ -15,9 +26,22 @@ class CleaveWidget(TextInput):
js
.
append
(
settings
.
get
(
"
CLEAVE_JS
"
))
return
{
"
js
"
:
js
}
def
__init__
(
self
,
cleave_options
:
Dict
[
str
,
Any
],
attrs
:
Optional
[
Dict
[
str
,
str
]]
=
None
):
@classmethod
def
_add_cleaved_attr
(
cls
,
attrs
,
name
:
str
,
value
:
Any
):
attrs
[
f
"
{
cls
.
attr_prefix
}
-
{
name
}
"
]
=
json
.
dumps
(
value
)
def
__post_init__
(
self
,
attrs
:
Optional
[
Dict
[
str
,
str
]]
=
None
):
cleaved_attrs
=
{}
if
attrs
is
None
else
attrs
.
copy
()
cleaved_attrs
.
update
({
f
"
data-dj-cleavejs-
{
name
}
"
:
value
for
name
,
value
in
cleave_options
.
items
()})
if
self
.
blocks
is
not
None
:
self
.
_add_cleaved_attr
(
cleaved_attrs
,
"
blocks
"
,
self
.
blocks
)
if
self
.
delimiter
is
not
None
:
if
isinstance
(
self
.
delimiter
,
Sequence
):
if
self
.
blocks
is
None
or
len
(
self
.
delimiter
)
!=
len
(
self
.
blocks
)
-
1
:
raise
TypeError
(
"
If delimiters are given, they must be one less than blocks and blocks must be provided.
"
)
self
.
_add_cleaved_attr
(
cleaved_attrs
,
"
delimiters
"
,
self
.
delimiter
)
else
:
self
.
_add_cleaved_attr
(
cleaved_attrs
,
"
delimiter
"
,
self
.
delimiter
)
self
.
_add_cleaved_attr
(
cleaved_attrs
,
"
delimiter-lazy-show
"
,
self
.
delimiter_lazy_show
)
super
().
__init__
(
cleaved_attrs
)
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