def test_get_remove_url(): alertsmsg = MailRequest( "fakepeer", sender, "*****@*****.**", open(home("tests/data/emails/alerts.msg")).read() ) url = alerts.get_remove_url(alertsmsg.body()) assert ( url == "/alerts/remove?s=AB2Xq4jsDy4ienBZYuYgWbzBWQ5i6LiD5L4y8JY&hl=en&gl=us&source=alertsmail&cd=4Ya67t6E3e4&cad=:s7:f1:v1:" ) msg = MailRequest( "fakepeer", sender, "*****@*****.**", open(home("tests/data/emails/confirmation.msg")).read() ) url = alerts.get_remove_url(msg.body()) assert url == "/alerts/remove?gl=us&hl=en&s=AB2Xq4jsDy4ienBZYuYgWbzBWQ5i6LiD5L4y8JY"
def CONFIRMING(message, alert_id=None, host=None): """ Waiting for an email with a confirmation link. """ try: queue.Queue('run/all').push(message) alert = Alert.objects.get(pk=alert_id) # google alerts sends alerts from a different address than the confirmation email. Lamson # state key includes the sender, so I have to add this extra piece. if alert.confirmed: LOG.debug("Alert for alert id %s was already confirmed." % alert_id) return ALERTING(message, alert_id=alert_id, host=host) alerts.confirm_alert(message) alert.confirmed = True alert.removeurl = alerts.get_remove_url(message.body()) if not alert.removeurl: q = queue.Queue('run/error') q.push(message) alert.save() LOG.debug("The removeurl for alert %s is %s" % (alert.id, alert.removeurl)) return ALERTING except Exception as e: q = queue.Queue('run/error') q.push(message) LOG.debug("Something bad happened in the alerts handler: %s" % str(e)) return CONFIRMING
def START(message, alert_id=None, host=None): """ This reads in the alert emails, parses them and sticks them in the database as blurbs. """ try: alert = Alert.objects.get(pk=int(alert_id)) if not alert.disabled: gtext = "".join([b.text for b in alert.client.blurb_set.filter(approved=True).all()]) rtext = "".join([b.text for b in alert.client.blurb_set.filter(rejected=True).all()]) url = alerts.get_remove_url(message.body()) LOG.debug("The removeurl for alert %s is %s" % (alert.id, url)) if url: alert.removeurl = url else: q = queue.Queue("run/error") q.push(message) alert.save() blurbs = alerts.create_blurbs(message, alert) # if there are examples of good and rejected text, # then try to filter them. if gtext and rtext: b = bayes.Bayes(gtext, rtext) for blurb in blurbs: blurb.junk_score = b.rejected_given_text(blurb.text) blurb.save() transaction.commit() else: LOG.debug("Received alerts for a disabled alert with id %s and remove url %s" % (alert.id, alert.removeurl)) except Exception as e: #queue up any messages that failed so we can diagnose #and try later. LOG.debug("Something bad happened with the alerts queue: %s" % str(e)) transaction.rollback() q = queue.Queue("run/error") q.push(message)