def validate_username(form, user_id): cleaned_data = form.cleaned_data username = cleaned_data.get("username") # validate username string if username: if len(username) < 5: form._errors["username"] = form.error_class(["'Username' must contain at least 5 characters."]) elif len(username) >30: form._errors["username"] = form.error_class(["'Username' must not exceed 30 characters."]) elif re.search(INVALID_USERNAME_CHARS, username): form._errors["username"] = form.error_class(["'Username' can only contain letters, digits and @/./+/-/_"]) if settings.ESGF_CONFIG: # check that the corresponding OpenID is available in the local CoG database if user_id is None: # do not check when instance is updated openid = esgfDatabaseManager.buildOpenid(username) if esgfDatabaseManager.checkOpenid(openid): form._errors["username"] = form.error_class(["Username/OpenID already taken in database."]) else: # save this openid in the form data so it can be used by the view POST method form.cleaned_data['openid'] = openid # once the openid is validated, choose the closest possible username _username = createUsername(username) print 'Created username=%s from=%s' % (_username, username) cleaned_data['username'] = _username # override form data else: # django will automatically check that the username is unique in the CoG database pass
def validate_username(form, user_id): cleaned_data = form.cleaned_data username = cleaned_data.get("username") # validate username string if username: if len(username) < 5: form._errors["username"] = form.error_class(["'Username' must contain at least 5 characters."]) elif len(username) > 30: form._errors["username"] = form.error_class(["'Username' must not exceed 30 characters."]) elif re.search(INVALID_USERNAME_CHARS, username): form._errors["username"] = form.error_class(["'Username' can only contain letters, " "digits and @/./+/-/_"]) if settings.ESGF_CONFIG: # check that the corresponding OpenID is available in the local CoG database if user_id is None: # do not check when instance is updated openid = esgfDatabaseManager.buildOpenid(username) if esgfDatabaseManager.checkOpenid(openid): form._errors["username"] = form.error_class(["Username/OpenID already taken in database."]) else: # save this openid in the form data so it can be used by the view POST method form.cleaned_data['openid'] = openid # once the openid is validated, choose the closest possible username _username = createUsername(username) print 'Created username=%s from=%s' % (_username, username) cleaned_data['username'] = _username # override form data else: # django will automatically check that the username is unique in the CoG database pass
def account_created_receiver(sender, **kwargs): # retrieve arguments userp = kwargs['instance'] created = kwargs['created'] #print 'Signal received: UserProfile post_save: username=%s created=%s openids=%s' % (userp.user.username, created, userp.openids()) # if username is like "openiduserXXX", then change it to the last part of the openid (+XXX if necessary) if created and userp.openid() is not None: # only for new accounts created with external openid if userp.user.username.startswith("openiduser"): # typically from "https://ceda.ac.uk/openid/..." # change the username lastPartOfOpenid = userp.openid().split("/")[-1] username = createUsername(lastPartOfOpenid) print "New user: changing the username from: %s to: %s" % (userp.user.username, username) userp.user.username = username userp.user.save()
def account_created_receiver(sender, **kwargs): # retrieve arguments userp = kwargs['instance'] created = kwargs['created'] #print 'Signal received: UserProfile post_save: username=%s created=%s openids=%s' % (userp.user.username, created, userp.openids()) # if username is like "openiduserXXX", then change it to the last part of the openid (+XXX if necessary) if created and userp.openid( ) is not None: # only for new accounts created with external openid if userp.user.username.startswith( "openiduser" ): # typically from "https://ceda.ac.uk/openid/..." # change the username lastPartOfOpenid = userp.openid().split("/")[-1] username = createUsername(lastPartOfOpenid) print "New user: changing the username from: %s to: %s" % ( userp.user.username, username) userp.user.username = username userp.user.save()