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
5
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
f865a6ac
Commit
f865a6ac
authored
5 years ago
by
Benjamin Graillot
Browse files
Options
Downloads
Patches
Plain Diff
[note] Handle alias creation and uniqueness
parent
9ecc40ad
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#7640
failed with stage
in 2 minutes and 20 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
note/models/notes.py
+69
-13
69 additions, 13 deletions
note/models/notes.py
note_kfet/settings.py
+2
-0
2 additions, 0 deletions
note_kfet/settings.py
with
71 additions
and
13 deletions
note/models/notes.py
+
69
−
13
View file @
f865a6ac
...
...
@@ -2,6 +2,9 @@
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import
unicodedata
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
_
...
...
@@ -65,15 +68,19 @@ class NoteUser(Note):
def
save
(
self
,
*
args
,
**
kwargs
):
"""
When saving, also create an alias
TODO: check availability of alias
TODO: remove old alias
"""
created
=
self
.
pk
is
None
if
created
:
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
()
super
().
save
(
*
args
,
**
kwargs
)
if
not
created
:
super
().
save
(
*
args
,
**
kwargs
)
class
NoteClub
(
Note
):
...
...
@@ -97,15 +104,19 @@ class NoteClub(Note):
def
save
(
self
,
*
args
,
**
kwargs
):
"""
When saving, also create an alias
TODO: check availability of alias
TODO: remove old alias
"""
created
=
self
.
pk
is
None
if
created
:
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
()
super
().
save
(
*
args
,
**
kwargs
)
if
not
created
:
super
().
save
(
*
args
,
**
kwargs
)
class
NoteSpecial
(
Note
):
...
...
@@ -133,15 +144,19 @@ class NoteSpecial(Note):
def
save
(
self
,
*
args
,
**
kwargs
):
"""
When saving, also create an alias
TODO: check availability of alias
TODO: remove old alias
"""
created
=
self
.
pk
is
None
if
created
:
alias
=
Alias
.
objects
.
create
(
name
=
str
(
self
.
club
),
note
=
self
)
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
()
super
().
save
(
*
args
,
**
kwargs
)
if
not
created
:
super
().
save
(
*
args
,
**
kwargs
)
class
Alias
(
models
.
Model
):
...
...
@@ -152,6 +167,18 @@ class Alias(models.Model):
verbose_name
=
_
(
'
name
'
),
max_length
=
255
,
unique
=
True
,
validators
=
[
RegexValidator
(
regex
=
settings
.
ALIAS_VALIDATOR_REGEX
,
message
=
_
(
'
Invalid alias
'
)
)
]
if
settings
.
ALIAS_VALIDATOR_REGEX
else
[]
)
normalized_name
=
models
.
CharField
(
max_length
=
255
,
unique
=
True
,
default
=
''
,
editable
=
False
)
note
=
models
.
ForeignKey
(
Note
,
...
...
@@ -164,3 +191,32 @@ class Alias(models.Model):
def
__str__
(
self
):
return
self
.
name
@staticmethod
def
normalize
(
string
):
"""
Normalizes a string: removes most diacritics and does casefolding
"""
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
'
})
)
def
save
(
self
,
*
args
,
**
kwargs
):
"""
Handle normalized_name
"""
self
.
normalized_name
=
Alias
.
normalize
(
self
.
name
)
if
self
.
normalized_name
<
256
:
super
().
save
(
*
args
,
**
kwargs
)
def
clean
(
self
):
normalized_name
=
Alias
.
normalize
(
self
.
name
)
if
len
(
normalized_name
)
>=
255
:
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.
'
))
except
Alias
.
DoesNotExist
:
pass
This diff is collapsed.
Click to expand it.
note_kfet/settings.py
+
2
−
0
View file @
f865a6ac
...
...
@@ -156,6 +156,8 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL
=
'
/static/
'
ALIAS_VALIDATOR_REGEX
=
r
''
try
:
from
.settings_local
import
*
except
ImportError
:
...
...
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