1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Maddie Graham
  • 2,019
  • 4
  • 25
  • 51
  • Just a side note. If i were you I would prefer to have a table with four columns: employee, day/date, start time and end time. It makes more sense to me, it's easier to query, analyze etc. – Borut May 10 '19 at 20:51
  • Can I ask you to show an example in full answer. Thank you in advance. – Maddie Graham May 10 '19 at 21:02

2 Answers2

1

Since you are importing those helper functions from outside the Model class, try doing this instead:

# . . .
def is_expired(self):
    # . . .
    if actual_hour in list(range(int(day_st()), int(day_end()))):
    # . . .
NS0
  • 6,016
  • 1
  • 15
  • 14
1

By simplifying the code a bit, this will probably way easier to tackle, and make it more efficient as well.

For example we can write the check as:

Employee(models.Model):

    # ...

    @property
    def is_expired(self):
        hour = datetime.datetime.now().hour
        return hour in range(int(self.day_st), int(self.day_end))

Now we can simply define two additional @propertys day_st and day_end, with:

    @property
    def day_st(self):
        day = datetime.datetime.today().weekday()
        return (
            self.monday_st,
            self.tuesday_st,
            self.wednesday_st,
            self.thursday_st,
            self.friday_st,
            self.saturday_st,
            self.sunday_st)[day]

    @property
    def day_end(self):
        day = datetime.datetime.today().weekday()
        return (
            self.monday_end,
            self.tuesday_end,
            self.wednesday_end,
            self.thursday_end,
            self.friday_end,
            self.saturday_end,
            self.sunday_end)[day]

So by declaring three @propertys in the Employee model, we are done.

Note: since it appears that you are using integers in your Employee model for monday_st, monday_end, etc., you should use IntegerField fields [Django-doc]. By using an IntegerField, your database can help guard the type integrity of your models, and furthermore you do not have to do a lot of manual casting yourself.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • but what if I want the field to look like this to the user. Example Time 9.00. In my choices.py '('9', '9.00'),' Is it also possible to use integerfield in this situation? how? Using '(9, '9.00'), or cover it in the form? – Maddie Graham May 10 '19 at 20:55
  • 1
    @MaddieGraham: yes, see [here](https://stackoverflow.com/questions/1117564/set-django-integerfield-by-choices-name) for example. As choices you can then use `choices=[(0, '0:00'), (1, '1:00')]`, so with as first element the "key" (the value you put in the model), and as second item the "value" (the textual representation). – Willem Van Onsem May 10 '19 at 20:57