Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
steering
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
Container Registry
Model registry
Operate
Environments
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
AURA
steering
Commits
5090c2d9
Verified
Commit
5090c2d9
authored
8 months ago
by
Ernesto Rico Schmidt
Browse files
Options
Downloads
Patches
Plain Diff
refactor: remove duplicated code and simplify updating links
parent
d9d66531
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#8282
failed
8 months ago
Stage: check
Stage: test
Stage: build
Stage: deploy
Stage: release
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
program/serializers.py
+4
-13
4 additions, 13 deletions
program/serializers.py
program/utils.py
+23
-4
23 additions, 4 deletions
program/utils.py
with
27 additions
and
17 deletions
program/serializers.py
+
4
−
13
View file @
5090c2d9
...
...
@@ -52,7 +52,7 @@ from program.models import (
Type
,
UserProfile
,
)
from
program.utils
import
dele
te_links
from
program.utils
import
upda
te_links
SOLUTION_CHOICES
=
{
"
theirs
"
:
"
Discard projected timeslot. Keep existing timeslot(s).
"
,
...
...
@@ -462,10 +462,7 @@ class HostSerializer(serializers.ModelSerializer):
# optional nested objects
if
"
links
"
in
validated_data
:
instance
=
delete_links
(
instance
)
for
link_data
in
validated_data
.
get
(
"
links
"
):
HostLink
.
objects
.
create
(
host
=
instance
,
**
link_data
)
instance
=
update_links
(
instance
,
validated_data
.
get
(
"
links
"
))
instance
.
updated_by
=
self
.
context
.
get
(
"
request
"
).
user
.
username
...
...
@@ -764,10 +761,7 @@ class ShowSerializer(serializers.HyperlinkedModelSerializer):
# optional nested objects
if
"
links
"
in
validated_data
:
instance
=
delete_links
(
instance
)
for
link_data
in
validated_data
.
get
(
"
links
"
):
ShowLink
.
objects
.
create
(
show
=
instance
,
**
link_data
)
instance
=
update_links
(
instance
,
validated_data
.
get
(
"
links
"
))
instance
.
updated_by
=
self
.
context
.
get
(
"
request
"
).
user
.
username
...
...
@@ -1170,10 +1164,7 @@ class NoteSerializer(serializers.ModelSerializer):
# optional nested objects
if
"
links
"
in
validated_data
:
instance
=
delete_links
(
instance
)
for
link_data
in
validated_data
.
get
(
"
links
"
):
NoteLink
.
objects
.
create
(
note
=
instance
,
**
link_data
)
instance
=
update_links
(
instance
,
validated_data
.
get
(
"
links
"
))
instance
.
updated_by
=
self
.
context
.
get
(
"
request
"
).
user
.
username
...
...
This diff is collapsed.
Click to expand it.
program/utils.py
+
23
−
4
View file @
5090c2d9
...
...
@@ -21,7 +21,7 @@
import
json
import
typing
from
datetime
import
date
,
datetime
,
time
from
typing
import
Dict
,
Optional
,
Tuple
,
Union
from
typing
import
Dict
,
Optional
,
Tuple
,
TypedDict
,
Union
import
requests
...
...
@@ -29,7 +29,12 @@ from django.conf import settings
from
django.utils
import
timezone
if
typing
.
TYPE_CHECKING
:
from
program.models
import
Host
,
Note
,
Show
from
program.models
import
Host
,
HostLink
,
Note
,
NoteLink
,
Show
,
ShowLink
class
Link
(
TypedDict
):
type_id
:
int
url
:
str
def
parse_datetime
(
date_string
:
str
|
None
)
->
datetime
|
None
:
...
...
@@ -134,11 +139,25 @@ def get_values(
return
int_if_digit
(
values
[
0
])
def
delete_links
(
instance
:
Union
[
"
Host
"
,
"
Note
"
,
"
Show
"
])
->
Union
[
"
Host
"
,
"
Note
"
,
"
Show
"
]:
"""
Delete the links associated with the instance.
"""
def
update_links
(
instance
:
Union
[
"
Host
"
,
"
Note
"
,
"
Show
"
],
links
:
list
[
Link
]
)
->
Union
[
"
Host
"
,
"
Note
"
,
"
Show
"
]:
"""
Update the links associated with the instance
"""
# delete the links associated with the instance
if
instance
.
links
.
count
()
>
0
:
for
link
in
instance
.
links
.
all
():
link
.
delete
(
keep_parents
=
True
)
match
type
(
instance
):
case
"
Host
"
:
for
link_data
in
links
:
HostLink
.
objects
.
create
(
host
=
instance
,
**
link_data
)
case
"
Note
"
:
for
link_data
in
links
:
NoteLink
.
objects
.
create
(
note
=
instance
,
**
link_data
)
case
"
Show
"
:
for
link_data
in
links
:
ShowLink
.
objects
.
create
(
show
=
instance
,
**
link_data
)
return
instance
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