def clean_user(self): """ Makes sure the user is in LDAP, and creates the user in the system if it doesn't exist already The user is submitted as something like "Matt Johnson (mdj2)", so we have to parse out the "mdj2" part """ user = self.cleaned_data['user'].strip() # if the user isn't filled out, so be it if user == "": return "" matches = re.search(r"\((.+)\)$", user) if not matches: raise forms.ValidationError("user must be of the form 'name (odin)'") user = matches.group(1) if not is_ldap_user(user): raise forms.ValidationError("Not a valid ODIN username") # create the user in the system if it doesn't exist email = User.username_to_email(user) try: user = User.objects.get(email=email) except User.DoesNotExist: user = User(email=email, is_active=False) user.save() return user