Answer the question
In order to leave comments, you need to log in
How to describe the storage of working hours in the database?
It is necessary to describe a model that will store the company's working hours for each day of the week. Including a serializer to it and a viewset. At the same time, it is necessary that in when it is displayed and accepted approximately in this format (non-working ones are simply not displayed):
working_hours: {
monday: [12:30, 12:40],
wednesday: [18:40, 19:40]
}
Answer the question
In order to leave comments, you need to log in
The model is simple - the day of the week, the beginning of the working day, the end of the working day.
Read the documentation for DRF, there is nothing complicated there. Redefining the save, update methods in serializers and other things.
1. Business hours are a str data type and not a datetime! Why? Because Datetime returns a specific date anyway. How to imagine it:
start_time: str = '12:20'
end_time: str = '16:00'
class WorkDay(models.Model):
DAYS_OF_WEEK = (
(1, _('Monday')),
(2, _('Tuesday')),
(3, _('Wednesday')),
(4, _('Thursday')),
(5, _('Friday')),
(6, _('Saturday')),
(7, _('Sunday')),
)
DAY_TYPES = (
('weekday', _('weekday')),
('holiday', _('holiday')),
)
day_of_the_week = models.PositiveIntegerField(
verbose_name=_('day of the week'),
choices=DAYS_OF_WEEK
)
start_time = models.CharField(
verbose_name=_('start time'),
max_length=5
)
end_time = models.CharField(
verbose_name=_('end time'),
max_length=5
)
day_type = models.CharField(
verbose_name=_('day type'),
max_length=255,
choices=DAY_TYPES
)
from django.contrib.postgres.fields import ArrayField
class WorkDay(models.Model):
DAYS_OF_WEEK = (
(1, _('Monday')),
(2, _('Tuesday')),
(3, _('Wednesday')),
(4, _('Thursday')),
(5, _('Friday')),
(6, _('Saturday')),
(7, _('Sunday')),
)
DAY_TYPES = (
('weekday', _('weekday')),
('holiday', _('holiday')),
)
day_of_the_week = models.PositiveIntegerField(
verbose_name=_('day of the week'),
choices=DAYS_OF_WEEK
)
working_hours = ArrayField(
ArrayField(
models.CharField(
max_length=5
),
size=2
),
verbose_name=_('working hours')
)
day_type = models.CharField(
verbose_name=_('day type'),
max_length=255,
choices=DAY_TYPES
)
from django.contrib.postgres.fields import JSONField
class WorkGraph(models.Model):
working_graph = JSONField(
verbose_name=_('working graph'),
default={
'monday': {
'hours': ['12:20', '16:00'],
'day_type': 'weekday'
}
}
)
import re
from django.core.exceptions import ValidationError
from typing import NoReturn
def validate_working_hours(
value: str
) -> NoReturn:
m = re.match(
r'^(?P<hours>\d{2}):(?P<minutes>\d{2})$',
value
)
if m is None:
raise ValidationError('please, use format HH:mm')
else:
hours = int(
m.group('hours')
)
if hours > 23:
raise ValidationError('hours is a value from 0 to 23')
minutes = int(
m.group('minutes')
)
if minutes > 59:
raise ValidationError('minutes is a value from 0 to 59')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question