My model consists of something like:
class Employee(models.Model):
#time work
monday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
monday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
tuesday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
tuesday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
wednesday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
wednesday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
thursday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
thursday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
friday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
friday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
saturday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
saturday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
sunday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
sunday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
I would like to filter employees who are currently working. So it's a reflection of their work, in a model that looks like this.
@property
def is_expired(self):
actual_hour = int(datetime.datetime.now().strftime('%H'))
if actual_hour in list(range(int(self.monday_st), int(self.monday_end))):
return True
return False
Returns 'True' in my views.py if the employee is currently working.
if instance.is_expired == True:
'the employee is working'
else:
'no working'
But it needs information if the employee only works on a given day, so I created a file with auxiliary functions that looks like this.
def day_st():
actual_day_number = datetime.datetime.today().weekday() + 1
if actual_day_number == 1:
return monday_st
if actual_day_number == 2:
return tuesday_st
if actual_day_number == 3:
return wednesday_st
if actual_day_number == 4:
return thursday_st
if actual_day_number == 5:
return friday_st
if actual_day_number == 6:
return saturday_st
if actual_day_number == 7:
return sunday_st
def day_end():
actual_day_number = datetime.datetime.today().weekday() + 1
if actual_day_number == 1:
return monday_end
if actual_day_number == 2:
return tuesday_end
if actual_day_number == 3:
return wednesday_end
if actual_day_number == 4:
return thursday_end
if actual_day_number == 5:
return friday_st
if actual_day_number == 6:
return saturday_end
if actual_day_number == 7:
return sunday_end
But I cannot use it in my model. I'm trying something like that.
from .my_helped_function import day_st, day_end
@property
def is_expired(self):
actual_hour = int(datetime.datetime.now().strftime('%H'))
if actual_hour in list(range(int(self.day_st), int(self.day_end))):
return True
return False
This above what I wrote does not work. How can I get the 'True' value from @property the fastest only for the current day.
My way seems to me quite complex, so I am open on how to do it faster.