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
178d5c3b
Verified
Commit
178d5c3b
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Fix code dulication
parent
a070ee6f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/apps/chronos/managers.py
+37
-70
37 additions, 70 deletions
aleksis/apps/chronos/managers.py
with
37 additions
and
70 deletions
aleksis/apps/chronos/managers.py
+
37
−
70
View file @
178d5c3b
...
...
@@ -32,7 +32,19 @@ class LessonSubstitutionManager(models.Manager):
)
class
LessonDataQuerySet
(
models
.
QuerySet
):
class
WeekQuerySetMixin
:
def
annotate_week
(
self
,
week
:
Union
[
CalendarWeek
,
int
]):
"""
Annotate all lessons in the QuerySet with the number of the provided calendar week.
"""
if
isinstance
(
week
,
CalendarWeek
):
week_num
=
week
.
week
else
:
week_num
=
week
return
self
.
annotate
(
_week
=
models
.
Value
(
week_num
,
models
.
IntegerField
()))
class
LessonDataQuerySet
(
models
.
QuerySet
,
WeekQuerySetMixin
):
"""
Overrides default QuerySet to add specific methods for lesson data.
"""
# Overridden in the subclasses. Swaps the paths to the base lesson period
...
...
@@ -125,16 +137,6 @@ class LessonDataQuerySet(models.QuerySet):
return
qs1
.
union
(
qs2
)
def
annotate_week
(
self
,
week
:
Union
[
CalendarWeek
,
int
]):
"""
Annotate all lessons in the QuerySet with the number of the provided calendar week.
"""
if
isinstance
(
week
,
CalendarWeek
):
week_num
=
week
.
week
else
:
week_num
=
week
return
self
.
annotate
(
_week
=
models
.
Value
(
week_num
,
models
.
IntegerField
()))
def
group_by_periods
(
self
,
is_person
:
bool
=
False
)
->
dict
:
per_period
=
{}
for
obj
in
self
:
...
...
@@ -154,46 +156,6 @@ class LessonDataQuerySet(models.QuerySet):
return
per_period
class
LessonPeriodQuerySet
(
LessonDataQuerySet
):
_period_path
=
""
_subst_path
=
"
substitutions__
"
def
next
(
self
,
reference
:
LessonPeriod
,
offset
:
Optional
[
int
]
=
1
)
->
LessonPeriod
:
"""
Get another lesson in an ordered set of lessons.
By default, it returns the next lesson in the set. By passing the offset argument,
the n-th next lesson can be selected. By passing a negative number, the n-th
previous lesson can be selected.
"""
index
=
list
(
self
.
values_list
(
"
id
"
,
flat
=
True
)).
index
(
reference
.
id
)
next_index
=
index
+
offset
if
next_index
>
self
.
count
()
-
1
:
next_index
%=
self
.
count
()
week
=
reference
.
_week
+
1
else
:
week
=
reference
.
_week
return
self
.
annotate_week
(
week
).
all
()[
next_index
]
def
filter_from_query
(
self
,
query_data
:
QueryDict
)
->
models
.
QuerySet
:
"""
Apply all filters from a GET or POST query.
This method expects a QueryDict, like the GET or POST attribute of a Request
object, that contains one or more of the keys group, teacher or room.
All three fields are filtered, in order.
"""
if
query_data
.
get
(
"
group
"
,
None
):
return
self
.
filter_group
(
int
(
query_data
[
"
group
"
]))
if
query_data
.
get
(
"
teacher
"
,
None
):
return
self
.
filter_teacher
(
int
(
query_data
[
"
teacher
"
]))
if
query_data
.
get
(
"
room
"
,
None
):
return
self
.
filter_room
(
int
(
query_data
[
"
room
"
]))
def
filter_from_type
(
self
,
type_
:
TimetableType
,
pk
:
int
)
->
Optional
[
models
.
QuerySet
]:
if
type_
==
TimetableType
.
GROUP
:
return
self
.
filter_group
(
pk
)
...
...
@@ -229,15 +191,29 @@ class LessonPeriodQuerySet(LessonDataQuerySet):
return
lesson_periods
def
per_period_one_day
(
self
)
->
OrderedDict
:
"""
Group selected lessons per period for one day
"""
per_period
=
{}
for
lesson_period
in
self
:
if
lesson_period
.
period
.
period
in
per_period
:
per_period
[
lesson_period
.
period
.
period
].
append
(
lesson_period
)
else
:
per_period
[
lesson_period
.
period
.
period
]
=
[
lesson_period
]
return
OrderedDict
(
sorted
(
per_period
.
items
()))
def
next
(
self
,
reference
:
LessonPeriod
,
offset
:
Optional
[
int
]
=
1
)
->
LessonPeriod
:
"""
Get another lesson in an ordered set of lessons.
By default, it returns the next lesson in the set. By passing the offset argument,
the n-th next lesson can be selected. By passing a negative number, the n-th
previous lesson can be selected.
"""
index
=
list
(
self
.
values_list
(
"
id
"
,
flat
=
True
)).
index
(
reference
.
id
)
next_index
=
index
+
offset
if
next_index
>
self
.
count
()
-
1
:
next_index
%=
self
.
count
()
week
=
reference
.
_week
+
1
else
:
week
=
reference
.
_week
return
self
.
annotate_week
(
week
).
all
()[
next_index
]
class
LessonPeriodQuerySet
(
LessonDataQuerySet
):
_period_path
=
""
_subst_path
=
"
substitutions__
"
class
LessonSubstitutionQuerySet
(
LessonDataQuerySet
):
...
...
@@ -306,17 +282,8 @@ class AbsenceQuerySet(DateRangeQuerySet):
class
HolidayQuerySet
(
DateRangeQuerySet
):
pass
class
SupervisionQuerySet
(
models
.
QuerySet
):
def
annotate_week
(
self
,
week
:
Union
[
CalendarWeek
,
int
]):
"""
Annotate all lessons in the QuerySet with the number of the provided calendar week.
"""
if
isinstance
(
week
,
CalendarWeek
):
week_num
=
week
.
week
else
:
week_num
=
week
return
self
.
annotate
(
_week
=
models
.
Value
(
week_num
,
models
.
IntegerField
()))
class
SupervisionQuerySet
(
models
.
QuerySet
,
WeekQuerySetMixin
):
def
filter_by_weekday
(
self
,
weekday
:
int
):
self
.
filter
(
Q
(
break_item__before_period__weekday
=
weekday
)
...
...
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