summaryrefslogtreecommitdiff
path: root/pgcommitfest/userprofile/util.py
diff options
context:
space:
mode:
authorMagnus Hagander2015-01-22 14:04:09 +0000
committerMagnus Hagander2015-01-22 14:04:09 +0000
commit0c58317302c0eeea57dcbafa28b5150b82eba3de (patch)
tree2f02f27f4780e89b1bd9918ceb7f6f1477328ad8 /pgcommitfest/userprofile/util.py
parentc80b7e1d58f3bac19a6df99ddf3d4bfc50e6a2ee (diff)
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.
Diffstat (limited to 'pgcommitfest/userprofile/util.py')
-rw-r--r--pgcommitfest/userprofile/util.py31
1 files changed, 31 insertions, 0 deletions
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