I'm currently showing all dates that appointments are not booked.
from datetime import date, timedelta
num_days = 5
start_date = date.today()
timeframe = [start_date timedelta(days=d) for d in range(num_days)]
exclude = list(Appointment.objects.values_list("start_appointment__date", flat=True).distinct())
set(timeframe) - set(exclude)
Model of appointments
class Appointment(models.Model):
user = models.ForeignKey(Account, on_delete=models.CASCADE)
seat = models.ForeignKey(Seat, on_delete=models.SET_NULL, null=True)
work = models.ManyToManyField(Therapy, related_name='work', verbose_name=_('Work'), blank=True)
start_appointment = models.DateTimeField(default=timezone.now, blank=True)
end_appointment = models.DateTimeField(default=timezone.now, blank=True)
For example i have appointments booked for:
[datetime.date(2021, 12, 8), datetime.date(2021, 12, 7), datetime.date(2021, 12, 7), datetime.date(2021, 12, 7)]
And the dates not booked:
{datetime.date(2021, 12, 10), datetime.date(2021, 12, 9), datetime.date(2021, 12, 11)}
I would like to show also the available times for the not booked dates with a time interval between 11:00AM to 21:00PM:.
Is that possible with the current implementation? If yes how can i achieve that? If not could you please suggest an alternative way?
Thank you
CodePudding user response:
It's hard to judge from the information provided.
- What does the database models look like
- What is an 'appointment'?
- is 'Transaction' a set of 'appointments', or a set of time slots?
- Assuming 'Transactions' is a set of time slots, how do you know whether they are booked? If you're using a boolean flag, why not filter for that one?
kind regards //S
CodePudding user response:
If you need to show available times, i suggest to separate your response by dates, so if you have one day like datetime(2021, 12, 8) and you want to show times between 11AM and 21PM you can use something like:
from datetime import date, datetime, time
date_of_reference = datetime(2021, 12, 8, 0, 0) #your the object of database
initial_time = datetime.combine(date_of_reference.date(), time(11, 0))
final_time = datetime.combine(date_of_reference.date(), time(21,0))
reserved_times = [datetime(2021, 12, 8, 11, 0), datetime(2021, 12, 8, 15, 0)] #reserved_times from database here
hours_between = [datetime.combine(date_of_reference.date(), time(hour initial_time.hour, 0)) for hour in range(((final_time - initial_time).seconds // 3600) 1)]
avaliable_hours = [hour for hour in hours_between if hour not in reserved_times]
If you have difficulties implementing this solution let me know so I try to write based on your models