<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,5 @@
 import logging, smtplib
-from datetime import datetime
+from datetime import datetime, timedelta
 
 from django.core.mail import send_mail
 from django.core.management.base import BaseCommand
@@ -7,43 +7,28 @@ from django.db.models import get_model
 from django.template import Context
 from django.template.loader import get_template
 
-from monitor.models import Log, Moniton
-
-crime_model = get_model('crime', 'crime')
+from crime.models import Crime
+from monitor.models import Moniton
 
 
 class Command(BaseCommand):
     &quot;&quot;&quot;
     Called by cron job to iterate thru all monitons and send emails accordingly.
+    Only check crime reports for the last 24 hours.
     &quot;&quot;&quot;
     def handle(self, *args, **options):
-        now = datetime.fromtimestamp(int(args[0]))
-        try:
-            last = Log.objects.latest('start')
-        except:
-            # No record yet, so create the very first one. Try again tomorrow.
-            Log.objects.create(start=now, end=now)
-            return
-
-        # Create log instance and record start time.
-        log = Log.objects.create(start=now)
-
-        # Iterate all monitons.
-        for m in Moniton.objects.filter(registered=True).all():
-            crimes = crime_model._default_manager.filter(
-                created_at__gt=last.start,
-                lat__lte=m.north,
-                lat__gte=m.south,
-                lng__lte=m.east,
-                lng__gte=m.west)
-            try:
-                send_mail('[Malaysia Crime] Notification of Crimes',
-                    get_template('monitor/notification_email.txt').render(Context({'moniton': m, 'crimes': crimes})),
-                    'noreply@malaysiacrime.com', [m.email])
-            except smtplib.SMTPException, e:
-                logging.error(e)
-                mail_admins('Monitor Error', e)
-
-        # Record end time and calculate duraction.
-        log.end = datetime.now()
-        log.save()
+        # Get crime reports in the last 24 hours.
+        now = datetime(*args) if args else datetime.now()
+        past = now - timedelta(1)
+        latest_crimes = Crime.objects.filter(created_at__gt=past)
+
+        # Iterate all monitons. Send emails on crimes that are of interest.
+        for m in Moniton.objects.all():
+            crimes = [x for x in latest_crimes if m.top &gt;= x.lat and m.bottom &lt;= m.right &gt;= x.lng and m.left &lt;= x.lng]
+            if crimes:
+                try:
+                    send_mail('[Malaysia Crime] Notification of Crimes',
+                        get_template('monitor/notification_email.txt').render(Context({'uuid': m.uuid, 'crimes': crimes})),
+                        'noreply@malaysiacrime.com', [m.email])
+                except smtplib.SMTPException, e:
+                    logging.error(e)</diff>
      <filename>monitor/management/commands/send_all.py</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>27e3aefd6cd9be35256e9e817c71869082035fa4</id>
    </parent>
  </parents>
  <author>
    <name>Kegan Gan</name>
    <email>kegan@kebook.local</email>
  </author>
  <url>http://github.com/kegan/malaysiacrime/commit/45f1aabf5b1efb9a98f56eb9d2eed253c2fd4afe</url>
  <id>45f1aabf5b1efb9a98f56eb9d2eed253c2fd4afe</id>
  <committed-date>2009-10-31T00:44:48-07:00</committed-date>
  <authored-date>2009-10-31T00:44:48-07:00</authored-date>
  <message>Updated cron job of sending notifications.</message>
  <tree>d28c01b991a0906300648b2bd5676e33a9e72fab</tree>
  <committer>
    <name>Kegan Gan</name>
    <email>kegan@kebook.local</email>
  </committer>
</commit>
