-
Notifications
You must be signed in to change notification settings - Fork 5
/
member_util.py
142 lines (110 loc) · 3.85 KB
/
member_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
"""Utility functions for dealing with members."""
from google.appengine.ext import db
from google.appengine.api import mail
import freesidemodels
import random_util
def MakeMember(*args, **kwargs):
"""Helper function for creating a new member.
This does not add the member to datastore.
If password is passed as a keyword argument, it is automatically
encrypted using the encryption algorithm from
freesidemodels.Person.EncryptPassword.
"""
if 'password' in kwargs:
kwargs['password'] = freesidemodels.Person.EncryptPassword(
kwargs['password'])
return freesidemodels.Member(*args, **kwargs)
def SaveMember(member):
"""Saves a member to datastore, with a transaction.
Args:
member: freesidemodels.Member
Returns:
freesidemodels.Member
"""
def DoPut(member):
member.put()
db.run_in_transaction(DoPut, member)
return member
def SaveIfChanged(new_member):
"""Saves the member to datastore if any of its fields have changed.
Args:
new_member: freesidemodels.Member, possibly changed Member object.
Returns:
freesidemodels.member
"""
orig_member = freesidemodels.Member.get_by_key_name(str(new_member.key()))
def _CompareAttr(attr):
return getattr(orig_member, attr) != getattr(new_member, attr)
if any(map(_CompareAttr, dir(orig_member))):
return SaveMember(new_member)
return new_member
def GetActiveMembers():
"""Gets all active members from datastore.
Returns:
list of freesidemodels.Member
"""
return freesidemodels.Member.all().filter('active =', True).fetch(1000)
def GetMemberByUsername(username, active=True):
"""Gets a member by his or her username.
Args:
username: str, the member's username.
active: bool, whether to fetch only an active member.
Returns:
freesidemodels.Member or None
"""
q = freesidemodels.Member.all().filter('username =', username)
if active:
q.filter('active =', True)
result = q.fetch(1)
if len(result) == 1:
return result[0]
else:
return None
def GetMemberByEmail(email, active=True):
"""Gets a member by his or her email address.
Args:
email: str, the member's email address.
active: bool, whether to fetch only an active member.
Returns:
freesidemodels.Member or None
"""
q = freesidemodels.Member.all().filter('email =', email)
if active:
q.filter('active =', True)
result = q.fetch(1)
if len(result) == 1:
return result[0]
else:
return None
def IsActiveMember(person):
"""Determines if the person is an active Freeside member.
Args:
person: freesidemodels.Person, person to check.
Returns:
bool, whether the person is an active Freeside member.
"""
return isinstance(person, freesidemodels.Member) and person.active
def ResetAndEmailPassword(member):
"""Scramble a members password and email it to them.
Args:
member: freesidemodels.Member
"""
# Change the members password and write it to the datastore
temp_password = random_util.UnencryptedPassword()
member.password = freesidemodels.Person.EncryptPassword(temp_password)
member.password_expired = True
SaveMember(member)
# Email the new password to the member
member_address = member.email
sender_address = '[email protected]'
subject = 'Your Freeside member password has been reset'
body = """
This email is to inform you that your password for the freeside-members portal
has been reset. Your temp password is listed below. Please login to the URL
below and change your password.
Member portal URL: http://freeside-members.appspot.com
Your username: %s
Temp password: %s
""" % (member.username, temp_password)
mail.send_mail(sender_address, member_address, subject, body)