from django.core.management.base import BaseCommand, CommandError

from lasana.models import Meal

from django.utils import timezone

class Command(BaseCommand):
    args = ''
    help = 'Deletes expired meals'

    def handle(self, *args, **options):
        now = timezone.now()
        old_meals = Meal.objects.all().order_by("expiration_time")

        for meal in old_meals:
            if meal.is_expired(now):
                meal_id = meal.id

                meal.delete()
                self.stdout.write('Thrown away expired meal "%s"\n' % meal_id)
            else:
                break

@@ -1,20 +1,58 @@
+from __future__ import print_function%0A
 from django.core.man
@@ -283,16 +283,33 @@
 tions):%0A
+        try:%0A    
         
@@ -329,16 +329,20 @@
 e.now()%0A
+    
         
@@ -409,16 +409,20 @@
         
+ 
+   
 for meal
@@ -436,16 +436,20 @@
 _meals:%0A
+    
         
@@ -477,16 +477,20 @@
 d(now):%0A
+    
         
@@ -528,24 +528,28 @@
             
+    
 meal.delete(
@@ -550,16 +550,20 @@
 elete()%0A
+    
         
@@ -645,16 +645,20 @@
         
+   
+ 
 else:%0A  
@@ -675,10 +675,310 @@
     
+    
 break%0A
+        except:%0A            import traceback%0A            import sys%0A            print(%22----------------------------------------%22)%0A            print(%22Failed to wash meals.%22, file=sys.stderr)%0A            traceback.print_exc(sys.stderr)%0A            print(%22----------------------------------------%22)%0A

Print backtrace if washing fails