From 0c58317302c0eeea57dcbafa28b5150b82eba3de Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Thu, 22 Jan 2015 15:04:09 +0100 Subject: Implement support for secondary email addresses Each user can add a secondary email (well, more than one) and then pick one of those when sending email. Addresses are validated by sending a token to the newly added address, with a link to click to confirm it. Only a fully confirmed address can actually be used. --- pgcommitfest/userprofile/util.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 pgcommitfest/userprofile/util.py (limited to 'pgcommitfest/userprofile/util.py') diff --git a/pgcommitfest/userprofile/util.py b/pgcommitfest/userprofile/util.py new file mode 100644 index 0000000..af50caf --- /dev/null +++ b/pgcommitfest/userprofile/util.py @@ -0,0 +1,31 @@ +from Crypto.Hash import SHA256 +from Crypto import Random + +from models import UserProfile + +def generate_random_token(): + """ + Generate a random token of 64 characters. This token will be + generated using a strong random number, and then hex encoded to make + sure all characters are safe to put in emails and URLs. + """ + s = SHA256.new() + r = Random.new() + s.update(r.read(250)) + return s.hexdigest() + + +class UserWrapper(object): + def __init__(self, user): + self.user = user + + @property + def email(self): + try: + up = UserProfile.objects.get(user=self.user) + if up.selectedemail and up.selectedemail.confirmed: + return up.selectedemail.email + else: + return self.user.email + except UserProfile.DoesNotExist: + return self.user.email -- cgit v1.2.3