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
07dce848
Verified
Commit
07dce848
authored
6 months ago
by
Ernesto Rico Schmidt
Browse files
Options
Downloads
Patches
Plain Diff
refactor: move handling of PATCH request into partial_update
parent
9a84273e
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
program/views.py
+57
-55
57 additions, 55 deletions
program/views.py
with
57 additions
and
55 deletions
program/views.py
+
57
−
55
View file @
07dce848
...
...
@@ -1112,61 +1112,6 @@ class APIScheduleViewSet(viewsets.ModelViewSet):
if
not
self
.
request
.
user
.
has_perm
(
"
program.change_schedule
"
):
return
Response
(
status
=
status
.
HTTP_401_UNAUTHORIZED
)
if
request
.
method
==
"
PATCH
"
:
# only these fields can be updated without generating conflicts
allowed
=
{
"
default_playlist_id
"
,
"
is_repetition
"
,
"
last_date
"
}
update_fields
=
set
(
request
.
data
.
get
(
"
schedule
"
).
keys
())
if
update_fields
.
issubset
(
allowed
):
schedule
=
self
.
get_object
()
default_playlist_id
=
request
.
data
.
get
(
"
schedule
"
).
get
(
"
default_playlist_id
"
)
if
default_playlist_id
==
""
or
default_playlist_id
is
None
:
# "clear" the default_playlist_id if the field has no value
schedule
.
default_playlist_id
=
None
else
:
schedule
.
default_playlist_id
=
int
(
default_playlist_id
)
is_repetition
=
request
.
data
.
get
(
"
schedule
"
).
get
(
"
is_repetition
"
)
if
is_repetition
==
"
true
"
or
is_repetition
==
"
1
"
:
schedule
.
is_repetition
=
True
if
is_repetition
==
"
false
"
or
is_repetition
==
"
0
"
:
schedule
.
is_repetition
=
False
last_date
=
request
.
data
.
get
(
"
schedule
"
).
get
(
"
last_date
"
)
if
last_date
==
""
or
last_date
is
None
:
# "clear" the last_date if the field has no value
schedule
.
last_date
=
None
else
:
try
:
last_date
=
date
.
fromisoformat
(
last_date
)
except
ValueError
as
e
:
data
=
{
"
last_date
"
:
e
.
args
[
0
]}
return
Response
(
data
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
if
schedule
.
last_date
is
None
or
schedule
.
last_date
>
last_date
:
schedule
.
last_date
=
last_date
last_end
=
timezone
.
make_aware
(
datetime
.
combine
(
last_date
,
schedule
.
end_time
)
)
TimeSlot
.
objects
.
filter
(
schedule
=
schedule
,
start__gt
=
last_end
).
delete
()
else
:
data
=
{
"
last_date
"
:
"
This field cannot be updated to this date
"
}
return
Response
(
data
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
schedule
.
save
()
serializer
=
ScheduleSerializer
(
schedule
)
return
Response
(
serializer
.
data
)
else
:
bad_fields
=
update_fields
.
difference
(
allowed
)
data
=
{
field
:
"
This field cannot be updated with PATCH
"
for
field
in
bad_fields
}
return
Response
(
data
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
# Only allow updating when with the `schedule` JSON object
if
"
schedule
"
not
in
request
.
data
:
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
...
...
@@ -1196,6 +1141,63 @@ class APIScheduleViewSet(viewsets.ModelViewSet):
return
Response
(
resolution
)
def
partial_update
(
self
,
request
,
*
args
,
**
kwargs
):
"""
Partial update a schedule without generating timeslots, testing or resolving collisions.
"""
# only these fields can be updated without generating conflicts
allowed
=
{
"
default_playlist_id
"
,
"
is_repetition
"
,
"
last_date
"
}
update_fields
=
set
(
request
.
data
.
get
(
"
schedule
"
).
keys
())
if
update_fields
.
issubset
(
allowed
):
schedule
=
self
.
get_object
()
default_playlist_id
=
request
.
data
.
get
(
"
schedule
"
).
get
(
"
default_playlist_id
"
)
if
default_playlist_id
==
""
or
default_playlist_id
is
None
:
# "clear" the default_playlist_id if the field has no value
schedule
.
default_playlist_id
=
None
else
:
schedule
.
default_playlist_id
=
int
(
default_playlist_id
)
is_repetition
=
request
.
data
.
get
(
"
schedule
"
).
get
(
"
is_repetition
"
)
if
is_repetition
==
"
true
"
or
is_repetition
==
"
1
"
:
schedule
.
is_repetition
=
True
if
is_repetition
==
"
false
"
or
is_repetition
==
"
0
"
:
schedule
.
is_repetition
=
False
last_date
=
request
.
data
.
get
(
"
schedule
"
).
get
(
"
last_date
"
)
if
last_date
==
""
or
last_date
is
None
:
# "clear" the last_date if the field has no value
schedule
.
last_date
=
None
else
:
try
:
last_date
=
date
.
fromisoformat
(
last_date
)
except
ValueError
as
e
:
data
=
{
"
last_date
"
:
e
.
args
[
0
]}
return
Response
(
data
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
if
schedule
.
last_date
is
None
or
schedule
.
last_date
>
last_date
:
schedule
.
last_date
=
last_date
last_end
=
timezone
.
make_aware
(
datetime
.
combine
(
last_date
,
schedule
.
end_time
))
TimeSlot
.
objects
.
filter
(
schedule
=
schedule
,
start__gt
=
last_end
).
delete
()
else
:
data
=
{
"
last_date
"
:
"
This field cannot be updated to this date
"
}
return
Response
(
data
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
schedule
.
save
()
serializer
=
ScheduleSerializer
(
schedule
)
return
Response
(
serializer
.
data
)
else
:
bad_fields
=
update_fields
.
difference
(
allowed
)
data
=
{
field
:
"
This field cannot be updated with PATCH
"
for
field
in
bad_fields
}
return
Response
(
data
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
# TODO: Create is currently not implemented because timeslots are supposed to be inserted
# by creating or updating a schedule.
...
...
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