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
c2890b57
Verified
Commit
c2890b57
authored
1 year ago
by
Ernesto Rico Schmidt
Browse files
Options
Downloads
Patches
Plain Diff
feat: add management command to add timeslots
Closes:
#108
parent
c1a30150
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Makefile
+3
-0
3 additions, 0 deletions
Makefile
program/management/commands/addtimeslots.py
+70
-0
70 additions, 0 deletions
program/management/commands/addtimeslots.py
with
73 additions
and
0 deletions
Makefile
+
3
−
0
View file @
c2890b57
...
...
@@ -46,6 +46,9 @@ loaddata.test:
addpermissions
:
$(
POETRY_RUN_MANAGE
)
addpermissions
addtimeslots
:
$(
POETRY_RUN_MANAGE
)
addtimeslots
removestaleimages
:
$(
POETRY_RUN_MANAGE
)
removestaleimages
...
...
This diff is collapsed.
Click to expand it.
program/management/commands/addtimeslots.py
0 → 100644
+
70
−
0
View file @
c2890b57
from
datetime
import
datetime
from
dateutil.rrule
import
rrule
from
django.core.management
import
BaseCommand
from
django.utils.timezone
import
make_aware
from
program.models
import
RRule
,
Schedule
,
TimeSlot
class
Command
(
BaseCommand
):
help
=
"
adds timeslots to the repeating schedules for one year
"
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
"
year
"
,
help
=
"
The year
"
,
type
=
int
,
)
parser
.
add_argument
(
"
--dry-run
"
,
action
=
"
store_true
"
,
help
=
"
Does not add the timeslots, just prints the number of timeslots to be added.
"
,
)
def
handle
(
self
,
*
args
,
**
options
):
year
=
options
[
"
year
"
]
dry_run
=
options
[
"
dry_run
"
]
begin_of_year
=
datetime
(
year
,
1
,
1
)
end_of_year
=
datetime
(
year
,
12
,
31
)
once
=
RRule
.
objects
.
get
(
freq
=
0
,
count
=
1
)
for
schedule
in
(
Schedule
.
objects
.
exclude
(
rrule
=
once
).
filter
(
last_date
=
None
).
order_by
(
"
show__name
"
)
):
last_timeslot
=
schedule
.
timeslots
.
last
()
# add timeslots to the schedules with no timeslot this year
# `begin_of_year` needs to be timezone aware for the comparison to work
if
last_timeslot
and
last_timeslot
.
start
<
make_aware
(
begin_of_year
):
freq
=
schedule
.
rrule
.
freq
interval
=
schedule
.
rrule
.
interval
dtstart
=
{
"
start
"
:
last_timeslot
.
start
,
"
end
"
:
last_timeslot
.
end
}
# `until` needs to be timezone aware because `dtstart` is
until
=
{
"
start
"
:
make_aware
(
end_of_year
),
"
end
"
:
make_aware
(
end_of_year
)}
# will need to skip the first recurrences, since they’re from the previous year
starts
=
rrule
(
freq
=
freq
,
interval
=
interval
,
dtstart
=
dtstart
[
"
start
"
],
until
=
until
[
"
start
"
]
)
ends
=
rrule
(
freq
=
freq
,
interval
=
interval
,
dtstart
=
dtstart
[
"
end
"
],
until
=
until
[
"
end
"
]
)
timeslots
=
[
TimeSlot
(
schedule
=
schedule
,
start
=
start
,
end
=
end
)
for
start
,
end
in
zip
(
starts
[
1
:],
ends
[
1
:])
]
if
dry_run
:
self
.
stdout
.
write
(
self
.
style
.
WARNING
(
f
"
would add
{
len
(
timeslots
)
}
timeslots to
{
schedule
}
"
)
)
else
:
TimeSlot
.
objects
.
bulk_create
(
timeslots
,
ignore_conflicts
=
True
)
self
.
stdout
.
write
(
self
.
style
.
SUCCESS
(
f
"
added
{
len
(
timeslots
)
}
timeslots to
{
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