Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-App-Kolego
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
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
Service Desk
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Onboarding
AlekSIS-App-Kolego
Commits
a7a6b029
Verified
Commit
a7a6b029
authored
3 months ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Add migration for existing overlapping absences
parent
812c00fe
No related branches found
Branches containing commit
No related tags found
1 merge request
!52
Resolve "Only allow one absence at any point of time"
Pipeline
#194084
passed
3 months ago
Stage: prepare
Stage: test
Stage: build
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/apps/kolego/migrations/0005_fix_overlapping_absences.py
+96
-0
96 additions, 0 deletions
...s/apps/kolego/migrations/0005_fix_overlapping_absences.py
with
96 additions
and
0 deletions
aleksis/apps/kolego/migrations/0005_fix_overlapping_absences.py
0 → 100644
+
96
−
0
View file @
a7a6b029
# Generated by Django 4.2.13 on 2024-07-16 11:01
from
datetime
import
datetime
,
time
from
django.db
import
migrations
,
models
from
django.db.models
import
Q
def
_run_forward
(
apps
,
schema_editor
):
Absence
=
apps
.
get_model
(
"
kolego
"
,
"
Absence
"
)
for
absence
in
Absence
.
objects
.
order_by
(
"
-datetime_start
"
,
"
-date_start
"
):
# Convert dates of new event to datetimes in case dates are used
new_datetime_start
=
(
absence
.
datetime_start
if
absence
.
datetime_start
else
datetime
.
combine
(
absence
.
date_start
,
time
.
min
)
).
astimezone
(
absence
.
timezone
)
new_datetime_end
=
(
absence
.
datetime_end
if
absence
.
datetime_end
else
datetime
.
combine
(
absence
.
date_end
,
time
.
max
)
).
astimezone
(
absence
.
timezone
)
events_within
=
Absence
.
objects
.
filter
(
person
=
absence
.
person
,
).
filter
(
Q
(
datetime_start__lte
=
new_datetime_end
,
datetime_end__gte
=
new_datetime_start
)
|
Q
(
date_start__lte
=
new_datetime_end
.
date
(),
date_end__gte
=
new_datetime_start
.
date
())
)
for
event_within
in
events_within
:
event_within_datetime_start
=
(
event_within
.
datetime_start
if
event_within
.
datetime_start
else
datetime
.
combine
(
event_within
.
date_start
,
time
.
min
)
)
event_within_datetime_end
=
(
event_within
.
datetime_end
if
event_within
.
datetime_end
else
datetime
.
combine
(
event_within
.
date_end
,
time
.
max
)
)
# If overlapping absence has the same reason, just extend it
if
event_within
.
reason
==
absence
.
reason
:
event_within
.
datetime_start
=
min
(
new_datetime_start
,
event_within_datetime_start
)
event_within
.
datetime_end
=
max
(
new_datetime_end
,
event_within_datetime_end
)
event_within
.
save
()
else
:
if
(
new_datetime_start
>
event_within_datetime_start
and
new_datetime_end
<
event_within_datetime_end
):
# Cut existing event in two parts
# First, cut end date of existing one
event_within
.
datetime_end
=
new_datetime_start
event_within
.
save
()
# Then, create new event based on existing one filling up the remaining time
end_filler_event
=
event_within
end_filler_event
.
pk
=
None
end_filler_event
.
id
=
None
end_filler_event
.
calendarevent_ptr_id
=
None
end_filler_event
.
freebusy_ptr_id
=
None
end_filler_event
.
_state
.
adding
=
True
end_filler_event
.
datetime_start
=
new_datetime_end
end_filler_event
.
datetime_end
=
event_within_datetime_end
end_filler_event
.
save
()
elif
(
new_datetime_start
<=
event_within_datetime_start
and
new_datetime_end
>=
event_within_datetime_end
):
# Delete existing event
event_within
.
delete
()
elif
(
new_datetime_start
>
event_within_datetime_start
and
new_datetime_start
<
event_within_datetime_end
and
new_datetime_end
>=
event_within_datetime_end
):
# Cut end of existing event
event_within
.
datetime_end
=
new_datetime_start
event_within
.
save
()
elif
(
new_datetime_start
<=
event_within_datetime_start
and
new_datetime_end
<
event_within_datetime_end
and
new_datetime_end
>
event_within_datetime_start
):
# Cut start of existing event
event_within
.
datetime_start
=
new_datetime_end
event_within
.
save
()
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
"
kolego
"
,
"
0004_absencereasontag_absencereason_tags
"
),
]
operations
=
[
migrations
.
RunPython
(
_run_forward
)]
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