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
f70897b4
Verified
Commit
f70897b4
authored
5 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Move prev/next functions to TimePeriod model
Close
#65
parent
d477b2bf
No related branches found
No related tags found
1 merge request
!38
Move prev/next functions to TimePeriod model
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
aleksis/apps/chronos/models.py
+42
-0
42 additions, 0 deletions
aleksis/apps/chronos/models.py
aleksis/apps/chronos/util/prev_next.py
+0
-48
0 additions, 48 deletions
aleksis/apps/chronos/util/prev_next.py
aleksis/apps/chronos/views.py
+10
-11
10 additions, 11 deletions
aleksis/apps/chronos/views.py
with
52 additions
and
59 deletions
aleksis/apps/chronos/models.py
+
42
−
0
View file @
f70897b4
...
...
@@ -9,6 +9,8 @@ from django.db import models
from
django.db.models
import
F
,
Max
,
Min
,
Q
from
django.db.models.functions
import
Coalesce
from
django.http.request
import
QueryDict
from
django.urls
import
reverse
from
django.utils
import
timezone
from
django.utils.decorators
import
classproperty
from
django.utils.translation
import
ugettext_lazy
as
_
...
...
@@ -246,6 +248,46 @@ class TimePeriod(models.Model):
return
wanted_week
[
self
.
weekday
]
@classmethod
def
get_next_relevant_day
(
cls
,
day
:
Optional
[
date
]
=
None
,
time
:
Optional
[
time
]
=
None
,
prev
:
bool
=
False
)
->
date
:
"""
Returns next (previous) day with lessons depending on date and time
"""
if
day
is
None
:
day
=
timezone
.
now
().
date
()
if
time
is
not
None
and
not
prev
:
if
time
>
cls
.
time_max
:
day
+=
timedelta
(
days
=
1
)
cw
=
CalendarWeek
.
from_date
(
day
)
if
day
.
weekday
()
>
cls
.
weekday_max
:
if
prev
:
day
=
cw
[
cls
.
weekday_max
]
else
:
cw
+=
1
day
=
cw
[
cls
.
weekday_min
]
elif
day
.
weekday
()
<
TimePeriod
.
weekday_min
:
if
prev
:
cw
-=
1
day
=
cw
[
cls
.
weekday_max
]
else
:
day
=
cw
[
cls
.
weekday_min
]
return
day
@classmethod
def
get_prev_next_by_day
(
cls
,
day
:
date
,
url
:
str
)
->
Tuple
[
str
,
str
]:
"""
Build URLs for previous/next day
"""
day_prev
=
cls
.
get_next_relevant_day
(
day
-
timedelta
(
days
=
1
),
prev
=
True
)
day_next
=
cls
.
get_next_relevant_day
(
day
+
timedelta
(
days
=
1
))
url_prev
=
reverse
(
url
,
args
=
[
day_prev
.
year
,
day_prev
.
month
,
day_prev
.
day
])
url_next
=
reverse
(
url
,
args
=
[
day_next
.
year
,
day_next
.
month
,
day_next
.
day
])
return
url_prev
,
url_next
@classproperty
def
period_min
(
cls
)
->
int
:
return
cls
.
objects
.
aggregate
(
period__min
=
Coalesce
(
Min
(
"
period
"
),
1
)).
get
(
"
period__min
"
)
...
...
This diff is collapsed.
Click to expand it.
aleksis/apps/chronos/util/prev_next.py
deleted
100644 → 0
+
0
−
48
View file @
d477b2bf
from
datetime
import
timedelta
,
date
,
time
from
typing
import
Optional
,
Tuple
from
calendarweek
import
CalendarWeek
from
django.urls
import
reverse
from
django.utils
import
timezone
from
..models
import
TimePeriod
def
get_next_relevant_day
(
day
:
Optional
[
date
]
=
None
,
time
:
Optional
[
time
]
=
None
,
prev
:
bool
=
False
)
->
date
:
"""
Returns next (previous) day with lessons depending on date and time
"""
if
day
is
None
:
day
=
timezone
.
now
().
date
()
if
time
is
not
None
and
not
prev
:
if
time
>
TimePeriod
.
time_max
:
day
+=
timedelta
(
days
=
1
)
cw
=
CalendarWeek
.
from_date
(
day
)
if
day
.
weekday
()
>
TimePeriod
.
weekday_max
:
if
prev
:
day
=
cw
[
TimePeriod
.
weekday_max
]
else
:
cw
+=
1
day
=
cw
[
TimePeriod
.
weekday_min
]
elif
day
.
weekday
()
<
TimePeriod
.
weekday_min
:
if
prev
:
cw
-=
1
day
=
cw
[
TimePeriod
.
weekday_max
]
else
:
day
=
cw
[
TimePeriod
.
weekday_min
]
return
day
def
get_prev_next_by_day
(
day
:
date
,
url
:
str
)
->
Tuple
[
str
,
str
]:
"""
Build URLs for previous/next day
"""
day_prev
=
get_next_relevant_day
(
day
-
timedelta
(
days
=
1
),
prev
=
True
)
day_next
=
get_next_relevant_day
(
day
+
timedelta
(
days
=
1
))
url_prev
=
reverse
(
url
,
args
=
[
day_prev
.
year
,
day_prev
.
month
,
day_prev
.
day
])
url_next
=
reverse
(
url
,
args
=
[
day_next
.
year
,
day_next
.
month
,
day_next
.
day
])
return
url_prev
,
url_next
This diff is collapsed.
Click to expand it.
aleksis/apps/chronos/views.py
+
10
−
11
View file @
f70897b4
...
...
@@ -19,7 +19,6 @@ from .forms import LessonSubstitutionForm
from
.models
import
LessonPeriod
,
LessonSubstitution
,
TimePeriod
,
Room
from
.tables
import
LessonsTable
from
.util.js
import
date_unix
from
.util.prev_next
import
get_next_relevant_day
,
get_prev_next_by_day
from
.util.weeks
import
CalendarWeek
,
get_weeks_for_year
from
aleksis.core.util.core_helpers
import
has_person
...
...
@@ -56,9 +55,9 @@ def my_timetable(
if
day
:
wanted_day
=
timezone
.
datetime
(
year
=
year
,
month
=
month
,
day
=
day
).
date
()
wanted_day
=
get_next_relevant_day
(
wanted_day
)
wanted_day
=
TimePeriod
.
get_next_relevant_day
(
wanted_day
)
else
:
wanted_day
=
get_next_relevant_day
(
timezone
.
now
().
date
(),
datetime
.
now
().
time
())
wanted_day
=
TimePeriod
.
get_next_relevant_day
(
timezone
.
now
().
date
(),
datetime
.
now
().
time
())
if
has_person
(
request
.
user
):
person
=
request
.
user
.
person
...
...
@@ -98,7 +97,7 @@ def my_timetable(
context
[
"
periods
"
]
=
TimePeriod
.
get_times_dict
()
context
[
"
smart
"
]
=
True
context
[
"
url_prev
"
],
context
[
"
url_next
"
]
=
get_prev_next_by_day
(
context
[
"
url_prev
"
],
context
[
"
url_next
"
]
=
TimePeriod
.
get_prev_next_by_day
(
wanted_day
,
"
my_timetable_by_date
"
)
...
...
@@ -209,9 +208,9 @@ def lessons_day(
if
day
:
wanted_day
=
timezone
.
datetime
(
year
=
year
,
month
=
month
,
day
=
day
).
date
()
wanted_day
=
get_next_relevant_day
(
wanted_day
)
wanted_day
=
TimePeriod
.
get_next_relevant_day
(
wanted_day
)
else
:
wanted_day
=
get_next_relevant_day
(
timezone
.
now
().
date
(),
datetime
.
now
().
time
())
wanted_day
=
TimePeriod
.
get_next_relevant_day
(
timezone
.
now
().
date
(),
datetime
.
now
().
time
())
# Get lessons
lesson_periods
=
LessonPeriod
.
objects
.
on_day
(
wanted_day
)
...
...
@@ -229,7 +228,7 @@ def lessons_day(
"
dest
"
:
reverse
(
"
lessons_day
"
)
}
context
[
"
url_prev
"
],
context
[
"
url_next
"
]
=
get_prev_next_by_day
(
context
[
"
url_prev
"
],
context
[
"
url_next
"
]
=
TimePeriod
.
get_prev_next_by_day
(
wanted_day
,
"
lessons_day_by_date
"
)
...
...
@@ -305,9 +304,9 @@ def substitutions(
if
day
:
wanted_day
=
timezone
.
datetime
(
year
=
year
,
month
=
month
,
day
=
day
).
date
()
wanted_day
=
get_next_relevant_day
(
wanted_day
)
wanted_day
=
TimePeriod
.
get_next_relevant_day
(
wanted_day
)
else
:
wanted_day
=
get_next_relevant_day
(
timezone
.
now
().
date
(),
datetime
.
now
().
time
())
wanted_day
=
TimePeriod
.
get_next_relevant_day
(
timezone
.
now
().
date
(),
datetime
.
now
().
time
())
day_number
=
config
.
CHRONOS_SUBSTITUTIONS_PRINT_DAY_NUMBER
day_contexts
=
{}
...
...
@@ -316,7 +315,7 @@ def substitutions(
next_day
=
wanted_day
for
i
in
range
(
day_number
):
day_contexts
[
next_day
]
=
{
"
day
"
:
next_day
}
next_day
=
get_next_relevant_day
(
next_day
+
timedelta
(
days
=
1
))
next_day
=
TimePeriod
.
get_next_relevant_day
(
next_day
+
timedelta
(
days
=
1
))
else
:
day_contexts
=
{
wanted_day
:
{
"
day
"
:
wanted_day
}}
...
...
@@ -332,7 +331,7 @@ def substitutions(
"
dest
"
:
reverse
(
"
substitutions
"
),
}
context
[
"
url_prev
"
],
context
[
"
url_next
"
]
=
get_prev_next_by_day
(
context
[
"
url_prev
"
],
context
[
"
url_next
"
]
=
TimePeriod
.
get_prev_next_by_day
(
wanted_day
,
"
substitutions_by_date
"
)
...
...
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