Maddie Graham
Maddie Graham

Reputation: 2177

@property with two functions inside of models.py in Django

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.

Upvotes: 1

Views: 248

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476554

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.

Upvotes: 1

NS0
NS0

Reputation: 6096

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()))):
    # . . .

Upvotes: 1

Related Questions