Home > front end >  Django Sending Email Alerts on only new alerts
Django Sending Email Alerts on only new alerts

Time:09-22

I am trying to send email alerts for all new alerts that have just been created. I have tried

last_alert = Alert.objects.filter(kiosk=kiosk).last()

But that only gets the last alert and it triggers the same one all the time. It is possible to have 3 alerts be triggered at once. I am trying to implement a flag to know whether or not an alert has been sent. I'm probably using latest wrong here.

last_alert = Alert.objects.filter(kiosk=kiosk).latest('pk')
   
    if last_alert.created_on:
        alert_status = HTTP_208_ALREADY_REPORTED
        send_alert_email = False
    else:
        alert_status = HTTP_201_CREATED
        send_alert_email = True
        last_alert.created_on = datetime.now(last_alert.kiosk.location.timezone)
        Alert.create(kiosk=kiosk, created_on=datetime.now(last_alert.kiosk.location.timezone))
        last_alert.save()
    # Get Timezone aware date and time
    current_dt = datetime.now().astimezone(kiosk.location.timezone)
    current_time = current_dt.strftime('%I:%M %p')
    current_date = current_dt.strftime('%m/%d/%Y')
    email_props2 = {
        'method': 'EMAIL',
        'email': '[email protected]',
        'data': {
                'facility': last_alert.kiosk.location.name,
                'description': last_alert.description,
                'pk': last_alert.pk,
                'time': current_time,
                'date': current_date,
                'kioskName': kiosk.name,
                'alert_type_display': last_alert.alert_type_display
        } 
    }
    
    if send_alert_email:
        _send_email(
                [email_props2['email']],
                {'data': email_props2['data']},
                ALERT_TEMPLATE_ID
        )  

Maybe I am approaching this problem wrong with the flag. Any help is very much appreciated.

thanks in advance

CodePudding user response:

I have a solution. I added a processed field to the Alert model default it to False. Then filter all Alerts with field processed=False. Loop through all of the Alerts, if processed=False send an email, then set processed=True.

last_alert = Alert.objects.filter(kiosk=kiosk, processed=False)

    # Get Timezone aware date and time
    for alert in last_alert:
        if alert.processed == False:
            current_dt = datetime.now().astimezone(kiosk.location.timezone)
            current_time = current_dt.strftime('%I:%M %p')
            current_date = current_dt.strftime('%m/%d/%Y')
            email_props2 = {
                'method': 'EMAIL',
                'email': '[email protected]',
                'data': {
                        'facility': alert.kiosk.location.name,
                        'description': alert.description,
                        'pk': alert.pk,
                        'time': current_time,
                        'date': current_date,
                        'kioskName': kiosk.name,
                        'alert_type_display': alert.alert_type_display
                } 
            }
            # Straight up send it, dude
            _send_email(
                    [email_props2['email']],
                    {'data': email_props2['data']},
                    ALERT_TEMPLATE_ID
            )
            alert.processed = True
            alert.save()
  • Related