Factor out an isIdAvailable() function from EditForm.clean_id() in
soc/views/site/user/profile.py and add it to soc/logic/site/id_user.py.
Addresses some comments on r614.
Patch by: Todd Larsen
Review by: Pawel Solyga
Review URL: http://codereviews.googleopensourceprograms.com/1202
--- a/app/soc/logic/site/id_user.py Thu Oct 02 15:37:18 2008 +0000
+++ b/app/soc/logic/site/id_user.py Thu Oct 02 20:22:15 2008 +0000
@@ -265,6 +265,38 @@
return user.is_developer
+def isIdAvailable(new_id, existing_user=None, existing_key_name=None):
+ """Returns True if Google Account is available for use by existing User.
+
+ Args:
+ new_id: a Google Account (users.User) object with a (possibly) new email
+ existing_user: an existing User entity; default is None, in which case
+ existing_key_name is used to look up the User entity
+ existing_key_name: the key_name of an existing User entity, used
+ when existing_user is not supplied; default is None
+ """
+ if not existing_user:
+ existing_user = getUserFromKeyName(existing_key_name)
+
+ if existing_user:
+ old_email = existing_user.id.email()
+ else:
+ old_email = None
+
+ if new_id.email() == old_email:
+ # "new" email is same as existing User wanting it, so it is "available"
+ return True
+ # else: "new" email truly is new to the existing User, so keep checking
+
+ if not isIdUser(new_id):
+ # new email address also does not belong to any other User,
+ # so it is available
+ return True
+
+ # email does not already belong to this User, but to some other User
+ return False
+
+
def getUserFromLinkName(link_name):
"""Returns User entity for link_name or None if not found.
@@ -471,4 +503,4 @@
prop.__set__(user, value)
user.put()
- return user
\ No newline at end of file
+ return user
--- a/app/soc/views/site/user/profile.py Thu Oct 02 15:37:18 2008 +0000
+++ b/app/soc/views/site/user/profile.py Thu Oct 02 20:22:15 2008 +0000
@@ -223,13 +223,10 @@
return link_name
def clean_id(self):
- new_email = self.cleaned_data.get('id')
- form_id = users.User(email=new_email)
- key_name = self.data.get('key_name')
- old_email = id_user.getUserFromKeyName(key_name).id.email()
- if new_email != old_email:
- if id_user.isIdUser(form_id):
- raise forms.ValidationError("This account is already in use.")
+ form_id = users.User(email=self.cleaned_data.get('id'))
+ if not id_user.isIdAvailable(
+ form_id, existing_key_name=self.data.get('key_name')):
+ raise forms.ValidationError("This account is already in use.")
return form_id