Created
April 21, 2015 15:11
-
-
Save wolever/a472d008029b09aa84b3 to your computer and use it in GitHub Desktop.
Sends an email to managers with a list of similar emails so a human can help fix typos in password reset requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connection | |
def mail_managers_similar_emails(request, email): | |
""" Sends an email to managers with a list of emails similar to ``email`` | |
so a human can help fix typos in password reset requests. """ | |
try: | |
cur = connection.cursor() | |
cur.execute(""" | |
SELECT | |
* | |
FROM ( | |
SELECT | |
email, | |
levenshtein(email, %s) as levenshtein | |
FROM auth_user | |
) AS x | |
WHERE levenshtein < 5 | |
ORDER BY levenshtein ASC | |
LIMIT 15 | |
""", [email]) | |
err = None | |
email_dists = cur.fetchall() | |
except Exception as e: | |
err = e | |
email_dists = None | |
email_msg = ( | |
"(error finding similar emails: %s)" %(err, ) if err else | |
"\n".join( | |
"%s %s?email=%s" %( | |
email, request.build_absolute_uri("/account/reset"), email, | |
) | |
for (email, _) in email_dists | |
) | |
) | |
mail_managers("Bad pwd reset email: %s" %(email, ), "\n".join([ | |
"An attempt was made to reset the password for: %s" %(email, ), | |
"This e-mail address does not exist in the system.", | |
"Similar e-mail addresses:", | |
email_msg | |
])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment