Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-App-Chronos
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
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®
Official
AlekSIS-App-Chronos
Commits
7206d283
Commit
7206d283
authored
3 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Prefer alias instead of annotate and remove superfluous annotations
parent
af46a31c
No related branches found
No related tags found
1 merge request
!150
Resolve "Django 3.2 database improvements"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
aleksis/apps/chronos/managers.py
+36
-34
36 additions, 34 deletions
aleksis/apps/chronos/managers.py
aleksis/apps/chronos/views.py
+3
-3
3 additions, 3 deletions
aleksis/apps/chronos/views.py
with
39 additions
and
37 deletions
aleksis/apps/chronos/managers.py
+
36
−
34
View file @
7206d283
...
...
@@ -4,7 +4,7 @@ from typing import Dict, Iterable, List, Optional, Union
from
django.contrib.sites.managers
import
CurrentSiteManager
as
_CurrentSiteManager
from
django.db
import
models
from
django.db.models
import
Count
,
ExpressionWrapper
,
F
,
Func
,
Q
,
QuerySet
,
Value
from
django.db.models
import
ExpressionWrapper
,
F
,
Func
,
Q
,
QuerySet
,
Value
from
django.db.models.fields
import
DateField
from
django.db.models.functions
import
Concat
...
...
@@ -97,7 +97,12 @@ class LessonPeriodManager(CurrentSiteManager):
"
lesson__validity
"
,
"
lesson__validity__school_term
"
,
)
.
prefetch_related
(
"
lesson__groups
"
,
"
lesson__teachers
"
,
"
substitutions
"
)
.
prefetch_related
(
"
lesson__groups
"
,
"
lesson__groups__parent_groups
"
,
"
lesson__teachers
"
,
"
substitutions
"
,
)
)
...
...
@@ -209,6 +214,13 @@ class WeekQuerySetMixin:
_year
=
models
.
Value
(
week
.
year
,
models
.
IntegerField
()),
)
def
alias_week
(
self
,
week
:
Union
[
CalendarWeek
]):
"""
Add an alias to all lessons in the QuerySet with the number of the provided calendar week.
"""
return
self
.
alias
(
_week
=
models
.
Value
(
week
.
week
,
models
.
IntegerField
()),
_year
=
models
.
Value
(
week
.
year
,
models
.
IntegerField
()),
)
class
GroupByPeriodsMixin
:
def
group_by_periods
(
self
,
is_week
:
bool
=
False
)
->
dict
:
...
...
@@ -541,21 +553,13 @@ class LessonSubstitutionQuerySet(LessonDataQuerySet):
Return all teachers which are affected by
selected substitutions (as substituted or substituting).
"""
return
(
Person
.
objects
.
filter
(
Q
(
lessons_as_teacher__in
=
self
.
affected_lessons
())
|
Q
(
lesson_substitutions__in
=
self
)
)
.
annotate
(
lessons_count
=
Count
(
"
lessons_as_teacher
"
))
.
order_by
(
"
short_name
"
)
)
return
Person
.
objects
.
filter
(
Q
(
lessons_as_teacher__in
=
self
.
affected_lessons
())
|
Q
(
lesson_substitutions__in
=
self
)
).
order_by
(
"
short_name
"
)
def
affected_groups
(
self
):
"""
Return all groups which are affected by selected substitutions.
"""
return
(
Group
.
objects
.
filter
(
lessons__in
=
self
.
affected_lessons
())
.
annotate
(
lessons_count
=
Count
(
"
lessons
"
))
.
order_by
(
"
short_name
"
)
)
return
Group
.
objects
.
filter
(
lessons__in
=
self
.
affected_lessons
()).
order_by
(
"
short_name
"
)
class
DateRangeQuerySetMixin
:
...
...
@@ -596,25 +600,13 @@ class AbsenceQuerySet(DateRangeQuerySetMixin, SchoolTermRelatedQuerySet):
"""
QuerySet with custom query methods for absences.
"""
def
absent_teachers
(
self
):
return
(
Person
.
objects
.
filter
(
absences__in
=
self
)
.
annotate
(
absences_count
=
Count
(
"
absences
"
))
.
order_by
(
"
short_name
"
)
)
return
Person
.
objects
.
filter
(
absences__in
=
self
).
order_by
(
"
short_name
"
)
def
absent_groups
(
self
):
return
(
Group
.
objects
.
filter
(
absences__in
=
self
)
.
annotate
(
absences_count
=
Count
(
"
absences
"
))
.
order_by
(
"
short_name
"
)
)
return
Group
.
objects
.
filter
(
absences__in
=
self
).
order_by
(
"
short_name
"
)
def
absent_rooms
(
self
):
return
(
Person
.
objects
.
filter
(
absences__in
=
self
)
.
annotate
(
absences_count
=
Count
(
"
absences
"
))
.
order_by
(
"
short_name
"
)
)
return
Person
.
objects
.
filter
(
absences__in
=
self
).
order_by
(
"
short_name
"
)
class
HolidayQuerySet
(
QuerySet
,
DateRangeQuerySetMixin
):
...
...
@@ -736,6 +728,10 @@ class EventQuerySet(DateRangeQuerySetMixin, SchoolTermRelatedQuerySet, Timetable
"""
Annotate all events in the QuerySet with the provided date.
"""
return
self
.
annotate
(
_date
=
models
.
Value
(
day
,
models
.
DateField
()))
def
alias_day
(
self
,
day
:
date
):
"""
Add an alias to all events in the QuerySet with the provided date.
"""
return
self
.
alias
(
_date
=
models
.
Value
(
day
,
models
.
DateField
()))
class
ExtraLessonQuerySet
(
TimetableQuerySet
,
SchoolTermRelatedQuerySet
,
GroupByPeriodsMixin
):
"""
QuerySet with custom query methods for extra lessons.
"""
...
...
@@ -744,14 +740,15 @@ class ExtraLessonQuerySet(TimetableQuerySet, SchoolTermRelatedQuerySet, GroupByP
def
within_dates
(
self
,
start
:
date
,
end
:
date
):
"""
Filter all extra lessons within a specific time range.
"""
return
self
.
a
nnotate
_day
().
filter
(
day__gte
=
start
,
day__lte
=
end
)
return
self
.
a
lias
_day
().
filter
(
day__gte
=
start
,
day__lte
=
end
)
def
on_day
(
self
,
day
:
date
):
"""
Filter all extra lessons on a day.
"""
return
self
.
within_dates
(
day
,
day
)
def
annotate_day
(
self
):
weekday_to_date
=
ExpressionWrapper
(
def
_get_weekday_to_date
(
self
):
"""
Get DB function to convert a weekday to a date.
"""
return
ExpressionWrapper
(
Func
(
Concat
(
F
(
"
year
"
),
F
(
"
week
"
)),
Value
(
"
IYYYIW
"
),
...
...
@@ -761,14 +758,19 @@ class ExtraLessonQuerySet(TimetableQuerySet, SchoolTermRelatedQuerySet, GroupByP
+
F
(
"
period__weekday
"
),
output_field
=
DateField
(),
)
return
self
.
annotate
(
day
=
weekday_to_date
)
def
annotate_day
(
self
):
return
self
.
annotate
(
day
=
self
.
_get_weekday_to_date
())
def
alias_day
(
self
):
return
self
.
alias
(
day
=
self
.
_get_weekday_to_date
())
def
exclude_holidays
(
self
,
holidays
:
Iterable
[
"
Holiday
"
])
->
QuerySet
:
"""
Exclude all extra lessons which are in the provided holidays.
"""
q
=
Q
()
for
holiday
in
holidays
:
q
=
q
|
Q
(
day__lte
=
holiday
.
date_end
,
day__gte
=
holiday
.
date_start
)
return
self
.
a
nnotate
_day
().
exclude
(
q
)
return
self
.
a
lias
_day
().
exclude
(
q
)
class
GroupPropertiesMixin
:
...
...
This diff is collapsed.
Click to expand it.
aleksis/apps/chronos/views.py
+
3
−
3
View file @
7206d283
...
...
@@ -32,18 +32,18 @@ def all_timetables(request: HttpRequest) -> HttpResponse:
context
=
{}
teachers
=
(
Person
.
objects
.
a
nnotate
(
lessons_count
=
Count
(
"
lessons_as_teacher
"
))
Person
.
objects
.
a
lias
(
lessons_count
=
Count
(
"
lessons_as_teacher
"
))
.
filter
(
lessons_count__gt
=
0
)
.
order_by
(
"
short_name
"
,
"
last_name
"
)
)
groups
=
Group
.
objects
.
for_current_school_term_or_all
().
a
nnotate
(
groups
=
Group
.
objects
.
for_current_school_term_or_all
().
a
lias
(
lessons_count
=
Count
(
"
lessons
"
),
child_lessons_count
=
Count
(
"
child_groups__lessons
"
),
)
classes
=
groups
.
filter
(
lessons_count__gt
=
0
,
parent_groups
=
None
)
|
groups
.
filter
(
child_lessons_count__gt
=
0
,
parent_groups
=
None
).
order_by
(
"
short_name
"
,
"
name
"
)
rooms
=
(
Room
.
objects
.
a
nnotate
(
lessons_count
=
Count
(
"
lesson_periods
"
))
Room
.
objects
.
a
lias
(
lessons_count
=
Count
(
"
lesson_periods
"
))
.
filter
(
lessons_count__gt
=
0
)
.
order_by
(
"
short_name
"
,
"
name
"
)
)
...
...
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