Skip to content

Commit 13d6ffe

Browse files
committed
fixed bug on next ride computation for monthly periodicity
1 parent 4e5efd5 commit 13d6ffe

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- debug documentation updated: the uwsgi launch command has been tested on a demo project
1111

12+
## [2.2.10]
13+
### Fixed
14+
- bug in computation for next ride in case of months periodicity
15+
1216
## [2.2.9]
1317
### Fixed
1418
- a race condition on report's save, that generated a DatabaseError has been fixed
@@ -184,7 +188,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
184188
- First release
185189

186190

187-
[unreleased]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.7...master
191+
[unreleased]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.10...master
192+
[2.2.10]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.9...v2.2.10
193+
[2.2.9]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.8...v2.2.9
194+
[2.2.8]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.7...v2.2.8
188195
[2.2.7]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.6...v2.2.7
189196
[2.2.6]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.5...v2.2.6
190197
[2.2.5]: https://github.com/openpolis/django-uwsgi-taskmanager/compare/v2.2.4...v2.2.5

taskmanager/models.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Define Django models for the taskmanager app."""
2+
import calendar
23
import datetime
34
import os
45
import re
@@ -244,7 +245,7 @@ def get_next_ride(self) -> datetime.datetime:
244245
"""Get the next ride."""
245246
utc_tz = pytz.timezone('UTC')
246247
if self.repetition_period and self.status in [self.STATUS_SPOOLED, self.STATUS_STARTED]:
247-
now = self.last_invocation_datetime
248+
now = self.last_invocation_datetime or datetime.datetime.now()
248249

249250
if self.repetition_rate in (None, 0):
250251
# consider 1 as default repetition_rate
@@ -278,19 +279,19 @@ def get_next_ride(self) -> datetime.datetime:
278279
)
279280
)
280281
else:
281-
offset = datetime.timedelta(months=self.repetition_rate)
282+
# monthly scheduling requires a different computation
283+
def add_months(sourcedate, months):
284+
month = sourcedate.month - 1 + months
285+
year = sourcedate.year + month // 12
286+
month = month % 12 + 1
287+
day = min(sourcedate.day, calendar.monthrange(year,month)[1])
288+
return datetime.datetime(year, month, day)
289+
282290
_next = (
283-
datetime.datetime(
284-
now.year,
285-
now.month,
286-
0, 0, 0, 0,
287-
tzinfo=utc_tz
288-
) + offset
291+
add_months(now, self.repetition_rate)
289292
+ datetime.timedelta(
290-
days=self.scheduling.day,
291293
hours=self.scheduling.hour,
292294
minutes=self.scheduling.minute,
293-
seconds=self.scheduling.second
294295
)
295296
)
296297

0 commit comments

Comments
 (0)