Search code examples
pythondjangodjango-rest-frameworklogic

Django rest framework, returning item status booked or free based on current date/time


I am currently working on a website for my students where it will help them to book some items on the classroom. I am already done with the booking logic and form so no two students are allowed to book the same item at the same time. I would like to create a table for them where it shows the item name and the status of the item based on the current time. So I have a webpage/django view that have a list of all the booking made including the item name, starting date, ending date, and the person who booked the item. So, based on this booking list, I would like to create some logic that will go through this booking list and compare it against the current time/date. In simple words, if the current time/date falls in between the start and end date of a booked item , it should return the value "booked" other wise "free". I am not sure how to achieve this. I brainstormed for days, and search the Internet for some hints but nothing. I would be very thankful if someone helped me. This is my models:

class Items(models.Model):
   ITEMS_CATEGORIES=(
        ('item-1', 'item-1'),
        ('item-2', 'item-2'),
        ('item-3', 'item-3'),
        ('item-4', 'item-4'),
        ('item-5', 'item-5'),
          ) 
    category = models.CharField(max_length=5, choices=ITEM_CATEGORIES)
    def __str__(self):
       return self.category

class Booking(models.Model):
   
    user = models.CharField(max_length=50, editable=False)
    note = models.CharField(max_length=300, default='SOMESTRING')
    item = models.ForeignKey(Subsystem, on_delete=models.CASCADE)
    check_in = models.DateTimeField()
    check_out = models.DateTimeField()
  
   
    def __str__(self):
       return f' {self.item} is booked successfully by {self.user} from {self.check_in.strftime("%d-%b-%Y %H:%M")} to {self.check_out.strftime("%d-%b-%Y %H:%M")} for {self.note} '

So I thought of something like this but this code 100% is wrong, but something similar to this

serializers:
class BookingSerializer(serializers.ModelSerializer):
  class Meta:
    model = Booking
    fields = (
      'item','check_in','check_out', 'user'
    )
    current_time = datetime.datetime.now()

                 /
                 /
                 some logic //
     if current_time = (check_in and check_out)
        return "Booked"
    else
       return "Free"

Solution

  • You can use a SerializerMethodField to add another field that returns a value from a calculation you need:

    import django.utils import timezone
    
    
    class BookingSerializer(serializers.ModelSerializer):
        status = serializers.SerializerMethodField()
        class Meta:
            model = Booking
            fields = (
                'item', 'check_in', 'check_out', 'user', 'status'
            )
    
        def get_status(self, obj):
            return 'Booked' if obj.check_in <= timezone.now() < obj.check_out else 'Free'