'Compare Day of the Week with ManyToMany Relationship
I'm new to Django and learning, and i'm trying to do the following: The user registers the days of the week and the time period he is available.
class DiasDaSemana(models.Model):
diasdasemana = models.CharField(max_length=20)
def __str__(self):
return self.diasdasemana
class RelacaoHorarioSemana(models.Model):
usuario = models.ForeignKey(User, on_delete=models.CASCADE)
relacao_dia_semana = models.ManyToManyField(DiasDaSemana)
start_horario = models.TimeField(auto_now=False,
verbose_name='Horário Inicial',blank=False, null=False)
end_horario = models.TimeField(auto_now=False,
verbose_name='Horário Final',blank=False, null=False)
obs = models.TextField(max_length=200, null=True)
class Meta:
verbose_name = "Usuário e Horário"
verbose_name_plural = "Usuários e Horários"
def dias_da_semana(self):
return ", ".join([str(p) for p in self.relacao_dia_semana.all()])
def __str__(self):
return str('Cadastre o Horário de Trabalho do Usuário:')
I want to add a logic that checks if the user is available on the present time and day, so i can filter the results to display in a template. I'm trying to compare the timezone.isoweekday IDs with the user inputted IDs (Which are the same, Monday = 1 and Segunda-feira(Monday in Portuguese) is = 1)... I've thought about querying the ManyToMany field but i'm clueless as how to do it. So far, i've got to this:
@property
def checar_disponibilidade_horario_dia(self):
data_comeco = timezone.localtime(self.start_horario)
data_fim = timezone.localtime(self.end_horario)
data_agora = timezone.localtime(timezone.now())
# Segunda é 1 e Domingo é 7
diasemana_hoje = timezone.isoweekday(timezone.now())
diasemana_disponivel = ###this is where i 'm having trouble####
if diasemana_hoje == diasemana_disponivel:
if data_agora >= data_comeco and data_agora <= data_fim:
dado_disponibilidade = {'Disponível': 'S'}
return dado_disponibilidade
elif data_agora > data_fim:
dado_disponibilidade = {'Disponível': 'N'}
return dado_disponibilidade
elif data_agora < data_comeco:
dado_disponibilidade = {'Disponível': 'N'}
return dado_disponibilidade
elif diasemana_hoje != diasemana_disponivel:
dado_disponibilidade = {'Disponível': 'N'}
return dado_disponibilidade
pass
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
