Guest User

Untitled

a guest
Nov 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. from django.utils import timezone
  2.  
  3. now = timezone.now()
  4. active_bookings = Booking.objects.filter(
  5. start_time__gte=now,
  6. end_time__lt=now
  7. )
  8.  
  9. # myapp/models.py
  10. class Booking(models.Model):
  11.  
  12. @property
  13. def is_active(self):
  14. now = timezone.now()
  15. return self.start_time >= now and self.end_time < now
  16.  
  17. # In your view or in a utility function
  18. myBooking = Booking.objects.get(id=12345)
  19. print(myBooking.is_active)
  20.  
  21. # myapp/management/commands/update_active_bookings.py
  22.  
  23. from django.core.management.base import BaseCommand
  24.  
  25. class Command(BaseCommand):
  26.  
  27. def handle(self, *args, **kwargs):
  28. now = timezone.now()
  29. for booking in Booking.objects.all().iterator():
  30. is_active = self.start_time <= now and self.end_time > now
  31. if self.is_active != is_active:
  32. # Only saving the model if we have to
  33. self.is_active = is_active
  34. self.save()
  35.  
  36. */10 * * * * source /path/to/venv/bin/activate && /path/to/app/manage.py update_active_bookings
Add Comment
Please, Sign In to add comment