Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Openlobby Server
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
TO
openlobby
Openlobby Server
Commits
d32ca713
Commit
d32ca713
authored
7 years ago
by
jan.bednarik
Browse files
Options
Downloads
Patches
Plain Diff
Instead of complicated name collision ids for Users just set flag has colliding name.
parent
2c10c1c5
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
openlobby/core/migrations/0010_user_has_colliding_name.py
+3
-3
3 additions, 3 deletions
openlobby/core/migrations/0010_user_has_colliding_name.py
openlobby/core/models.py
+10
-7
10 additions, 7 deletions
openlobby/core/models.py
tests/test_models.py
+28
-21
28 additions, 21 deletions
tests/test_models.py
with
41 additions
and
31 deletions
openlobby/core/migrations/0010_user_
name
_colli
sion_id
.py
→
openlobby/core/migrations/0010_user_
has
_colli
ding_name
.py
+
3
−
3
View file @
d32ca713
# Generated by Django 2.0.2 on 2018-02-2
0 20:28
# Generated by Django 2.0.2 on 2018-02-2
1 11:45
from
django.db
import
migrations
,
models
...
...
@@ -12,7 +12,7 @@ class Migration(migrations.Migration):
operations
=
[
migrations
.
AddField
(
model_name
=
'
user
'
,
name
=
'
name
_colli
sion_id
'
,
field
=
models
.
Integer
Field
(
default
=
0
),
name
=
'
has
_colli
ding_name
'
,
field
=
models
.
Boolean
Field
(
default
=
False
),
),
]
This diff is collapsed.
Click to expand it.
openlobby/core/models.py
+
10
−
7
View file @
d32ca713
...
...
@@ -13,15 +13,16 @@ class User(AbstractUser):
openid_uid
=
models
.
CharField
(
max_length
=
255
,
null
=
True
)
extra
=
JSONField
(
null
=
True
,
blank
=
True
)
is_author
=
models
.
BooleanField
(
default
=
False
)
name
_colli
sion_id
=
models
.
Integer
Field
(
default
=
0
)
has
_colli
ding_name
=
models
.
Boolean
Field
(
default
=
False
)
def
save
(
self
,
*
args
,
**
kwargs
):
# deal with first name and last name collisions
collisions
=
User
.
objects
.
filter
(
first_name
=
self
.
first_name
,
last_name
=
self
.
last_name
)
\
.
order_by
(
'
-name_collision_id
'
)
if
len
(
collisions
)
>
0
and
self
not
in
collisions
:
self
.
name_collision_id
=
collisions
[
0
].
name_collision_id
+
1
# TODO when we allow name change, it should also reset name_collision_id
if
self
.
is_author
:
collisions
=
User
.
objects
.
filter
(
first_name
=
self
.
first_name
,
last_name
=
self
.
last_name
,
is_author
=
True
).
exclude
(
id
=
self
.
id
)
if
collisions
.
count
()
>
0
:
self
.
has_colliding_name
=
True
collisions
.
update
(
has_colliding_name
=
True
)
super
().
save
(
*
args
,
**
kwargs
)
...
...
@@ -66,4 +67,6 @@ class Report(models.Model):
def
save
(
self
,
*
args
,
**
kwargs
):
super
().
save
(
*
args
,
**
kwargs
)
User
.
objects
.
filter
(
id
=
self
.
author
.
id
).
update
(
is_author
=
True
)
if
not
self
.
author
.
is_author
:
self
.
author
.
is_author
=
True
self
.
author
.
save
()
This diff is collapsed.
Click to expand it.
tests/test_models.py
+
28
−
21
View file @
d32ca713
...
...
@@ -77,27 +77,34 @@ def test_login_attempt__default_expiration():
def
test_user__no_name_collision
():
User
.
objects
.
create
(
username
=
'
a
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
first_name
=
'
Burt
'
,
last_name
=
'
Reynolds
'
)
u
ser
=
User
.
objects
.
create
(
username
=
'
c
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Reynolds
'
)
assert
u
ser
.
name_collision_id
==
0
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Reynolds
'
)
as
ser
t
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
False
assert
U
ser
.
objects
.
get
(
username
=
'
b
'
).
has_colliding_name
is
False
def
test_user__name_collision
():
u1
=
User
.
objects
.
create
(
username
=
'
a
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
u2
=
User
.
objects
.
create
(
username
=
'
b
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
u3
=
User
.
objects
.
create
(
username
=
'
c
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
assert
u1
.
name_collision_id
==
0
assert
u2
.
name_collision_id
==
1
assert
u3
.
name_collision_id
==
2
def
test_user__name_collision_is_not_updated_for_existing_user
():
u1
=
User
.
objects
.
create
(
username
=
'
a
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Reynolds
'
)
u2
=
User
.
objects
.
create
(
username
=
'
b
'
,
first_name
=
'
Ryan
'
,
last_name
=
'
Reynolds
'
)
assert
u1
.
name_collision_id
==
0
assert
u2
.
name_collision_id
==
1
u1
.
save
()
u2
.
save
()
assert
u1
.
name_collision_id
==
0
assert
u2
.
name_collision_id
==
1
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
assert
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
True
assert
User
.
objects
.
get
(
username
=
'
b
'
).
has_colliding_name
is
True
def
test_user__name_collision_affects_only_authors
():
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
User
.
objects
.
create
(
username
=
'
b
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
User
.
objects
.
create
(
username
=
'
c
'
,
is_author
=
False
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
assert
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
False
assert
User
.
objects
.
get
(
username
=
'
b
'
).
has_colliding_name
is
False
assert
User
.
objects
.
get
(
username
=
'
c
'
).
has_colliding_name
is
False
User
.
objects
.
create
(
username
=
'
d
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
assert
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
False
assert
User
.
objects
.
get
(
username
=
'
b
'
).
has_colliding_name
is
True
assert
User
.
objects
.
get
(
username
=
'
c
'
).
has_colliding_name
is
False
assert
User
.
objects
.
get
(
username
=
'
d
'
).
has_colliding_name
is
True
def
test_user__name_collision_excludes_self_on_update
():
u
=
User
.
objects
.
create
(
username
=
'
a
'
,
is_author
=
True
,
first_name
=
'
Ryan
'
,
last_name
=
'
Gosling
'
)
u
.
save
()
assert
User
.
objects
.
get
(
username
=
'
a
'
).
has_colliding_name
is
False
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