import datetime

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

from clowder_server.emailer import send_alert
from clowder_server.models import Alert

class Command(BaseCommand):
    help = 'Checks and sends alerts'

    def handle(self, *args, **options):
        alerts = Alert.objects.filter(notify_at__lte=datetime.datetime.now)
        for alert in alerts:
            send_alert(request.user, alert.name)
            alert.notify_at = None
            alert.save()

import datetime

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

from clowder_account.models import ClowderUser
from clowder_server.emailer import send_alert
from clowder_server.models import Alert, Ping

class Command(BaseCommand):
    help = 'Checks and sends alerts'

    def handle(self, *args, **options):

        # delete old pings
        for user in ClowderUser.objects.all():
            pings = Ping.objects.filter(user=user)[:500]
            pings = list(pings) # forces database hit
            Ping.objects.exclude(pk__in=pings).delete()

        # send alerts
        alerts = Alert.objects.filter(notify_at__lte=datetime.datetime.now)
        for alert in alerts:
            send_alert(request.user, alert.name)
            alert.notify_at = None
            alert.save()

Delete old unused pings from users
Delete old unused pings from users

  import datetime
  
  from django.core.management.base import BaseCommand, CommandError
  
+ from clowder_account.models import ClowderUser
  from clowder_server.emailer import send_alert
- from clowder_server.models import Alert
+ from clowder_server.models import Alert, Ping
  
  class Command(BaseCommand):
      help = 'Checks and sends alerts'
  
      def handle(self, *args, **options):
+ 
+         # delete old pings
+         for user in ClowderUser.objects.all():
+             pings = Ping.objects.filter(user=user)[:500]
+             pings = list(pings) # forces database hit
+             Ping.objects.exclude(pk__in=pings).delete()
+ 
+         # send alerts
          alerts = Alert.objects.filter(notify_at__lte=datetime.datetime.now)
          for alert in alerts:
              send_alert(request.user, alert.name)
              alert.notify_at = None
              alert.save()
  
Delete old unused pings from users
## Code Before:
import datetime

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

from clowder_server.emailer import send_alert
from clowder_server.models import Alert

class Command(BaseCommand):
    help = 'Checks and sends alerts'

    def handle(self, *args, **options):
        alerts = Alert.objects.filter(notify_at__lte=datetime.datetime.now)
        for alert in alerts:
            send_alert(request.user, alert.name)
            alert.notify_at = None
            alert.save()

## Instruction:
Delete old unused pings from users
## Code After:
import datetime

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

from clowder_account.models import ClowderUser
from clowder_server.emailer import send_alert
from clowder_server.models import Alert, Ping

class Command(BaseCommand):
    help = 'Checks and sends alerts'

    def handle(self, *args, **options):

        # delete old pings
        for user in ClowderUser.objects.all():
            pings = Ping.objects.filter(user=user)[:500]
            pings = list(pings) # forces database hit
            Ping.objects.exclude(pk__in=pings).delete()

        # send alerts
        alerts = Alert.objects.filter(notify_at__lte=datetime.datetime.now)
        for alert in alerts:
            send_alert(request.user, alert.name)
            alert.notify_at = None
            alert.save()
