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
4e97b71b
Commit
4e97b71b
authored
3 months ago
by
Hangzhi Yu
Browse files
Options
Downloads
Patches
Plain Diff
Cut existing absences on absence creation to avoid overlaps
parent
a29a3544
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
#193951
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/models/absence.py
+77
-0
77 additions, 0 deletions
aleksis/apps/kolego/models/absence.py
with
77 additions
and
0 deletions
aleksis/apps/kolego/models/absence.py
+
77
−
0
View file @
4e97b71b
from
datetime
import
datetime
,
time
from
django.db
import
models
from
django.db.models
import
Q
,
QuerySet
from
django.http
import
HttpRequest
...
...
@@ -145,6 +147,81 @@ class Absence(FreeBusy):
def
__str__
(
self
):
return
f
"
{
self
.
person
}
(
{
self
.
datetime_start
}
-
{
self
.
datetime_end
}
)
"
def
save
(
self
,
*
args
,
skip_overlap_handling
=
False
,
**
kwargs
):
if
not
skip_overlap_handling
:
events_within
=
Absence
.
get_objects
(
None
,
{
"
person
"
:
self
.
person
.
pk
},
start
=
self
.
datetime_start
or
self
.
date_start
,
end
=
self
.
datetime_end
or
self
.
date_end
,
)
# Convert dates of new event to datetimes in case dates are used
new_datetime_start
=
(
self
.
datetime_start
if
self
.
datetime_start
else
datetime
.
combine
(
self
.
date_start
,
time
())
).
astimezone
(
self
.
timezone
)
new_datetime_end
=
(
self
.
datetime_end
if
self
.
datetime_end
else
datetime
.
combine
(
self
.
date_end
,
datetime
.
max
.
time
())
).
astimezone
(
self
.
timezone
)
for
event_within
in
events_within
:
event_within_datetime_start
=
(
Absence
.
value_start_datetime
(
event_within
)
if
event_within
.
datetime_start
else
datetime
.
combine
(
Absence
.
value_start_datetime
(
event_within
),
time
())
)
event_within_datetime_end
=
(
Absence
.
value_end_datetime
(
event_within
)
if
event_within
.
datetime_end
else
datetime
.
combine
(
Absence
.
value_end_datetime
(
event_within
),
datetime
.
max
.
time
()
)
)
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
(
skip_overlap_handling
=
True
)
# Then, create new event filling up the remaining time span
end_filler_event
=
event_within
end_filler_event
.
pk
=
None
end_filler_event
.
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
(
skip_overlap_handling
=
True
)
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_end
>=
event_within_datetime_end
):
# Cut end of existing event
event_within
.
datetime_end
=
new_datetime_start
event_within
.
save
(
skip_overlap_handling
=
True
)
elif
(
new_datetime_start
<=
event_within_datetime_start
and
new_datetime_end
<
event_within_datetime_end
):
# Cut start of existing event
event_within
.
datetime_start
=
new_datetime_end
event_within
.
save
(
skip_overlap_handling
=
True
)
super
().
save
(
*
args
,
**
kwargs
)
class
Meta
:
verbose_name
=
_
(
"
Absence
"
)
verbose_name_plural
=
_
(
"
Absences
"
)
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