Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nk20
Manage
Activity
Members
Labels
Plan
Issues
32
Issue boards
Milestones
Wiki
Code
Merge requests
6
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
BDE
nk20
Commits
32a37eae
Unverified
Commit
32a37eae
authored
5 years ago
by
me5na7qbjqbrp
Browse files
Options
Downloads
Patches
Plain Diff
Factorize automatic alias creation
parent
f865a6ac
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#7641
passed with stage
in 2 minutes and 35 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
note/admin.py
+1
-1
1 addition, 1 deletion
note/admin.py
note/models/notes.py
+45
-54
45 additions, 54 deletions
note/models/notes.py
with
46 additions
and
55 deletions
note/admin.py
+
1
−
1
View file @
32a37eae
...
...
@@ -28,7 +28,7 @@ class NoteAdmin(PolymorphicParentModelAdmin):
list_filter
=
(
PolymorphicChildModelFilter
,
'
is_active
'
,)
# Use a polymorphic list
list_display
=
(
'
__str__
'
,
'
balance
'
,
'
is_active
'
)
list_display
=
(
'
pretty
'
,
'
balance
'
,
'
is_active
'
)
polymorphic_list
=
True
# Organize notes by registration date
...
...
This diff is collapsed.
Click to expand it.
note/models/notes.py
+
45
−
54
View file @
32a37eae
...
...
@@ -3,9 +3,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import
unicodedata
from
django.conf
import
settings
from
django.core.exceptions
import
ValidationError
from
django.core.validators
import
RegexValidator
from
django.conf
import
settings
from
django.db
import
models
from
django.utils.translation
import
gettext_lazy
as
_
from
polymorphic.models
import
PolymorphicModel
...
...
@@ -46,6 +47,36 @@ class Note(PolymorphicModel):
verbose_name
=
_
(
"
note
"
)
verbose_name_plural
=
_
(
"
notes
"
)
def
pretty
(
self
):
"""
:return: Pretty name of this note
"""
return
'
Not implemented
'
pretty
.
short_description
=
_
(
'
Note
'
)
def
save
(
self
,
*
args
,
**
kwargs
):
"""
Save note with it
'
s alias (called in polymorphic children)
"""
aliases
=
Alias
.
objects
.
filter
(
name
=
str
(
self
))
if
aliases
.
exists
():
# Alias exists, so check if it is linked to this note
if
aliases
.
first
().
note
!=
self
:
raise
ValidationError
# Save note
super
().
save
(
*
args
,
**
kwargs
)
else
:
# Alias does not exist yet, so check if it can exist
a
=
Alias
(
name
=
str
(
self
))
a
.
clean
()
# Save note and alias
super
().
save
(
*
args
,
**
kwargs
)
a
.
note
=
self
a
.
save
(
force_insert
=
True
)
class
NoteUser
(
Note
):
"""
...
...
@@ -63,24 +94,10 @@ class NoteUser(Note):
verbose_name_plural
=
_
(
"
users note
"
)
def
__str__
(
self
):
return
_
(
"
%(user)s
'
s note
"
)
%
{
'
user
'
:
str
(
self
.
user
)
}
return
str
(
self
.
user
)
def
save
(
self
,
*
args
,
**
kwargs
):
"""
When saving, also create an alias
TODO: remove old alias
"""
created
=
self
.
pk
is
None
try
:
alias
=
Alias
.
objects
.
get
(
normalized_name
=
Alias
.
normalize
(
str
(
self
.
user
)))
return
except
Alias
.
DoesNotExist
:
if
created
:
super
().
save
(
*
args
,
**
kwargs
)
alias
=
Alias
.
objects
.
create
(
name
=
str
(
self
.
user
),
note
=
self
)
alias
.
save
()
if
not
created
:
super
().
save
(
*
args
,
**
kwargs
)
def
pretty
(
self
):
return
_
(
"
%(user)s
'
s note
"
)
%
{
'
user
'
:
str
(
self
.
user
)}
class
NoteClub
(
Note
):
...
...
@@ -99,24 +116,10 @@ class NoteClub(Note):
verbose_name_plural
=
_
(
"
clubs notes
"
)
def
__str__
(
self
):
return
_
(
"
Note for %(club)s club
"
)
%
{
'
club
'
:
str
(
self
.
club
)
}
return
str
(
self
.
club
)
def
save
(
self
,
*
args
,
**
kwargs
):
"""
When saving, also create an alias
TODO: remove old alias
"""
created
=
self
.
pk
is
None
try
:
alias
=
Alias
.
objects
.
get
(
normalized_name
=
Alias
.
normalize
(
str
(
self
.
club
)))
return
except
Alias
.
DoesNotExist
:
if
created
:
super
().
save
(
*
args
,
**
kwargs
)
alias
=
Alias
.
objects
.
create
(
name
=
str
(
self
.
club
),
note
=
self
)
alias
.
save
()
if
not
created
:
super
().
save
(
*
args
,
**
kwargs
)
def
pretty
(
self
):
return
_
(
"
Note for %(club)s club
"
)
%
{
'
club
'
:
str
(
self
.
club
)}
class
NoteSpecial
(
Note
):
...
...
@@ -141,22 +144,8 @@ class NoteSpecial(Note):
def
__str__
(
self
):
return
self
.
special_type
def
save
(
self
,
*
args
,
**
kwargs
):
"""
When saving, also create an alias
TODO: remove old alias
"""
created
=
self
.
pk
is
None
try
:
alias
=
Alias
.
objects
.
get
(
normalized_name
=
Alias
.
normalize
(
self
.
special_type
))
return
except
Alias
.
DoesNotExist
:
if
created
:
super
().
save
(
*
args
,
**
kwargs
)
alias
=
Alias
.
objects
.
create
(
name
=
str
(
self
.
special_type
),
note
=
self
)
alias
.
save
()
if
not
created
:
super
().
save
(
*
args
,
**
kwargs
)
def
pretty
(
self
):
return
self
.
special_type
class
Alias
(
models
.
Model
):
...
...
@@ -200,7 +189,8 @@ class Alias(models.Model):
return
''
.
join
(
char
for
char
in
unicodedata
.
normalize
(
'
NFKD
'
,
string
.
casefold
())
if
all
(
not
unicodedata
.
category
(
char
).
startswith
(
cat
)
for
cat
in
{
'
M
'
,
'
P
'
,
'
Z
'
,
'
C
'
})
if
all
(
not
unicodedata
.
category
(
char
).
startswith
(
cat
)
for
cat
in
{
'
M
'
,
'
P
'
,
'
Z
'
,
'
C
'
})
)
def
save
(
self
,
*
args
,
**
kwargs
):
...
...
@@ -208,7 +198,7 @@ class Alias(models.Model):
Handle normalized_name
"""
self
.
normalized_name
=
Alias
.
normalize
(
self
.
name
)
if
self
.
normalized_name
<
256
:
if
len
(
self
.
normalized_name
)
<
256
:
super
().
save
(
*
args
,
**
kwargs
)
def
clean
(
self
):
...
...
@@ -217,6 +207,7 @@ class Alias(models.Model):
raise
ValidationError
(
_
(
'
Alias too long.
'
))
try
:
if
self
!=
Alias
.
objects
.
get
(
normalized_name
=
normalized_name
):
raise
ValidationError
(
_
(
'
An alias with a similar name already exists.
'
))
raise
ValidationError
(
_
(
'
An alias with a similar name
'
'
already exists.
'
))
except
Alias
.
DoesNotExist
:
pass
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