1

I have a simple model to show Companies opening times

class Company(Model):
    class Meta:
        verbose_name = _(u"Empresa")
        verbose_name_plural = _(u"Empresas")
        ordering = ('published_dt',)

    user = ForeignKey('auth.User', verbose_name=_(u"Responsável"), null=True, blank=True)
    category = ForeignKey(CompanyCategory, verbose_name=_(u"Categoria"), help_text=_(u"Categoria que a empresa faz parte. Eg:. Alimentação, Pet Shop"), null=True, blank=True)

    # Company Fields
    title = CharField(max_length=150, verbose_name=_(u"Nome da Empresa"), null=True, blank=True)
    slogan = CharField(max_length=150, verbose_name=_(u"Slogan"), help_text=_(u"Um slogan ou descrição curta. Ex:. 'Compramos sua moto e pagamos á vista'"), null=True, blank=True)
    image = ImageField(blank=True, null=True, upload_to='uploaded_images')
    crop = CharField(choices=CROP, max_length=200, null=True, blank=True, verbose_name=_(u"Cortar Imagem"), help_text=_(u"Selecione o ponto de referência para cortarmos a imagem."))
    email = EmailField(max_length=200, verbose_name=_(u"Email"), null=True, blank=True)
    website = URLField(max_length=200, verbose_name=_(u"Website"), null=True, blank=True)
    published = BooleanField(default=True, verbose_name=_(u"Publicado"))
    published_dt = DateTimeField(null=True, blank=True, default=datetime.now(), help_text=_(u"Date of publication - Not necessary to complete if you do not know what it is."), verbose_name=_(u"Published Date"))
    description = HTMLField(null=True, blank=True, verbose_name=_(u"Descrição"), help_text=_(u"Uma descrição da sua empresa em poucas linhas."))

    enable_comments = BooleanField(default=True, verbose_name=_(u"Habilitar Comentários"))
    # Admin fields
    slug = SlugField(verbose_name=_(u'Slug / URL'), blank=True, null=True, max_length=300,)
    order = PositiveIntegerField(verbose_name=_(u"Ordem"), null=True, blank=True)

    def __unicode__(self):
        return u'%s' % self.title

    def get_address(self):
        if self.address and self.city and self.number and self.uf:
            return u'%s, %s - %s, %s' % (self.address, self.number, self.city, self.uf)

    def get_absolute_url(self):
        code = codifica(self.pk)
        return reverse('website:company_detail', kwargs={'slug':self.slug, 'pk':self.pk})

class OpeningHours(Model):
    class Meta:
        verbose_name = _(u"Horário de Abertura")
        verbose_name_plural = _(u"Horários de Abertura")
        unique_together = ('company', 'weekday')

    company = ForeignKey(Company, related_name="opening_times", verbose_name=_(u"Empresa"))
    weekday = IntegerField(choices=WEEKDAYS, verbose_name=_(u"Dia da Semana"))
    fromHour = TimeField(verbose_name=_(u"Abre ás:"), null=True, blank=True)
    toHour = TimeField(verbose_name=_(u"Fecha ás:"), null=True, blank=True)

    def __unicode__(self):
        return "%s %s (%s - %s)" % (self.company, self.weekday, self.fromHour, self.toHour)

And I need a way to have something like {{ company.is_open }} on my template, and Company.objects.all().order_by('open'). How can I do that using my structure?

1 Answer 1

2

Add a method to Company:

import datetime

...

class Company(Model):

    ...

    def is_open(self):
        today_hours = self.opening_times.filter(weekday=datetime.datetime.today().weekday())[0] # you may want to add code to make sure that there is an entry for this weekday
        if datetime.datetime.now().time() > today_hours.fromHour and datetime.datetime.now().time() < today_hours.toHour:
            return True
        else:
            return False

You will then be able to call this using {{ company.is_open }} in your templates.

Unfortunately, you will not be able to query on this or order by it in your database calls. However, you can sort it after the query is done:

companies = sorted(Company.objects.all(), key=lambda c: c.is_open(), reverse=True)
Sign up to request clarification or add additional context in comments.

7 Comments

Looks like this comparison are not working very well "if datetime.datetime.now() > today_hours.fromHour and datetime.datetime.now() < today_hours.toHour: " I'm trying to understand what I did wrong.
Changed that line to add .time(). Try it now? You might still have timezone issues.
Worked like a charm. I'm using the "food_companies = sorted(Company.objects.filter(category__where_eat=True)[0:5], key=lambda c: c.show_open())" just like that, but it's ordering inverse.
You mean it's putting closed companies before open ones, right? You can reverse that by adding reverse=True to the sorted() call. I edited to do so.
The companies are still showing as closed even when it's open. Take a look at full code: dpaste.de/1JL2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.