"""
Define a command that should be run from a crontab.
Check offene oder Rueckfragen Leistungsmeldungen. 
Send an email to the person in charge. 
"""

from django.core.management.base import BaseCommand, CommandError
from django.core.management import call_command
from django.utils import translation
from django.conf import settings
from django.template import loader, Context

from post_office import mail

import boote.models as models

import datetime
import pprint

class Command(BaseCommand):
    """Go through all reservations 
    and send out 
    """
    
    args = ""
    help = "Send out Reservations."
    

    def handle(self, *args, **options):
        # set the locale right, to get the dates represented correctly
        translation.activate(settings.LANGUAGE_CODE)


        # EMAIL RESERVATION
        t = loader.get_template("boote/email_booking.html")        
        # get records        
        for booking in models.Booking.objects.filter(notified = False).order_by('-date'):
            c = Context({ 'booking': booking})
            payload = t.render(c)
            sbj = '[SVPB] Reservation (' + booking.date.strftime('%d, %b %Y') + ') - ' + booking.boat.type.name + " \"" + booking.boat.name + "\"" 
            self.stdout.write('From: ' +  settings.DEFAULT_FROM_EMAIL)
            self.stdout.write('To: ' + booking.user.email)
            self.stdout.write('Subject: ' + sbj)
            self.stdout.write('Content:\n\r' + payload)
            
            mail.send(
                  [booking.user.email], 
                  settings.DEFAULT_FROM_EMAIL,
                  subject=sbj,                  
                  html_message=payload,
            )
            
            booking.notified = True
            booking.save()     
            
        # EMAIL ISSUES
        t = loader.get_template("boote/email_issue.html")        
        # get records        
        for issue in models.BoatIssue.objects.filter(notified = False):
            c = Context({ 'issue': issue})
            payload = t.render(c)
            sbj = '[SVPB]  Schadensmeldung - ' + issue.boat.type.name + " \"" + issue.boat.name + "\"" 
            self.stdout.write('From: ' +  settings.DEFAULT_FROM_EMAIL)
            self.stdout.write('To: ' + issue.boat.owner.email)
            self.stdout.write('CC: ' + issue.reported_by.email)
            self.stdout.write('Subject: ' + sbj)
            self.stdout.write('Content:\n\r' + payload)
            
            mail.send(
                  [issue.boat.owner.email], 
                  settings.DEFAULT_FROM_EMAIL,
                  subject=sbj,                  
                  html_message=payload,
            )
            
            issue.notified = True
            issue.save()  

        call_command('send_queued_mail')

        translation.deactivate()
@@ -999,16 +999,37 @@
 date'):%0A
+            try:%0A    
         
@@ -1071,32 +1071,36 @@
 g%7D)%0A            
+    
 payload = t.rend
@@ -1097,32 +1097,36 @@
 d = t.render(c)%0A
+    
             sbj 
@@ -1261,32 +1261,36 @@
 %22%22 %0A            
+ 
+   
 self.stdout.writ
@@ -1336,32 +1336,36 @@
 IL)%0A            
+    
 self.stdout.writ
@@ -1399,32 +1399,36 @@
 il)%0A            
+   
+ 
 self.stdout.writ
@@ -1440,32 +1440,36 @@
 bject: ' + sbj)%0A
+    
             self
@@ -1525,32 +1525,36 @@
    %0A            
+    
 mail.send(%0A     
@@ -1566,16 +1566,18 @@
         
+ 
+ 
 %5Bbooking
@@ -1583,32 +1583,34 @@
 g.user.email%5D, %0A
+  
                 
@@ -1632,32 +1632,34 @@
 ULT_FROM_EMAIL,%0A
+  
                 
@@ -1701,32 +1701,34 @@
                 
+  
 html_message=pay
@@ -1737,32 +1737,36 @@
 ad,%0A            
+   
+ 
 )%0A            %0A 
@@ -1756,32 +1756,36 @@
  )%0A            %0A
+    
             book
@@ -1808,32 +1808,36 @@
 rue%0A            
+    
 booking.save()  
@@ -1834,16 +1834,79 @@
 g.save()
+%0A            except:%0A                print(%22Unexpected error %22)
      %0A  
@@ -2107,16 +2107,37 @@
 False):%0A
+            try:%0A    
         
@@ -2175,32 +2175,36 @@
 e%7D)%0A            
+    
 payload = t.rend
@@ -2213,32 +2213,36 @@
 (c)%0A            
+ 
+   
 sbj = '%5BSVPB%5D  S
@@ -2321,32 +2321,36 @@
 %22%22 %0A            
+    
 self.stdout.writ
@@ -2396,32 +2396,36 @@
 IL)%0A            
+   
+ 
 self.stdout.writ
@@ -2451,32 +2451,36 @@
 at.owner.email)%0A
+    
             self
@@ -2531,32 +2531,36 @@
 il)%0A            
+    
 self.stdout.writ
@@ -2584,32 +2584,36 @@
 bj)%0A            
+ 
+   
 self.stdout.writ
@@ -2644,33 +2644,41 @@
 ad)%0A            
-%0A
+    %0A    
             mail
@@ -2702,16 +2702,26 @@
         
+         
+ 
 %5Bissue.b
@@ -2731,32 +2731,42 @@
 .owner.email%5D, %0A
+          
                 
@@ -2806,32 +2806,42 @@
                 
+         
+ 
 subject=sbj,    
@@ -2865,32 +2865,42 @@
                 
+          
 html_message=pay
@@ -2909,32 +2909,48 @@
 ad,%0A            
+        
+        
 )%0A            %0A 
@@ -2940,32 +2940,36 @@
  )%0A            %0A
+    
             issu
@@ -2994,24 +2994,28 @@
             
+    
 issue.save()
@@ -3014,16 +3014,79 @@
 e.save()
+%0A            except:%0A                print(%22Unexpected error %22)
   %0A%0A    

bugfix. try-catch