Skip to content
Snippets Groups Projects
Commit c7421952 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

Use utility function to parse datetime strings

parent b079138c
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,7 @@ from django.utils.translation import gettext_lazy as _
from versatileimagefield.fields import VersatileImageField, PPOIField
from steering.settings import THUMBNAIL_SIZES, AUTO_SET_UNTIL_DATE_TO_END_OF_YEAR, AUTO_SET_UNTIL_DATE_TO_DAYS_IN_FUTURE
from .utils import parse_datetime
class Type(models.Model):
......@@ -718,9 +719,8 @@ class Schedule(models.Model):
errors = {}
for ts in conflicts['projected']:
# Ignore past dates
if timezone.make_aware(datetime.strptime(ts['start'], "%Y-%m-%d %H:%M:%S")) <= timezone.now():
if parse_datetime(ts['start']) <= timezone.now():
# Ignore past dates
continue
# If no solution necessary
......@@ -802,7 +802,7 @@ class Schedule(models.Model):
create.append(projected_ts)
existing_ts = TimeSlot.objects.get(pk=existing['id'])
existing_ts.start = datetime.strptime(ts['end'], '%Y-%m-%d %H:%M:%S')
existing_ts.start = parse_datetime(ts['end'])
update.append(existing_ts)
# theirs-start
......@@ -824,7 +824,7 @@ class Schedule(models.Model):
create.append(projected_ts)
existing_ts = TimeSlot.objects.get(pk=existing['id'])
existing_ts.end = datetime.strptime(ts['start'], '%Y-%m-%d %H:%M:%S')
existing_ts.end = parse_datetime(ts['start'])
update.append(existing_ts)
# theirs-both
......@@ -851,7 +851,7 @@ class Schedule(models.Model):
create.append(projected_ts)
existing_ts = TimeSlot.objects.get(pk=existing['id'])
existing_ts.end = datetime.strptime(ts['start'], '%Y-%m-%d %H:%M:%S')
existing_ts.end = parse_datetime(ts['start'])
update.append(existing_ts)
projected_ts = TimeSlot.objects.instantiate(ts['end'], existing['end'], schedule, show)
......@@ -941,8 +941,8 @@ class Schedule(models.Model):
class TimeSlotManager(models.Manager):
@staticmethod
def instantiate(start, end, schedule, show):
return TimeSlot(start=datetime.strptime(start, '%Y-%m-%d %H:%M:%S'),
end=datetime.strptime(end, '%Y-%m-%d %H:%M:%S'),
return TimeSlot(start=parse_datetime(start),
end=parse_datetime(end),
show=show, is_repetition=schedule.is_repetition, schedule=schedule).generate()
@staticmethod
......
......@@ -18,6 +18,18 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from datetime import datetime
from django.utils import timezone
def parse_datetime(date_string: str) -> datetime:
"""
parse a datetime string and return a timezone aware datetime object
"""
try:
parsed_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%H:%S")
except ValueError:
parsed_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S%z")
return timezone.make_aware(parsed_datetime)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment