def validate_username(username, realname): """Validates a username and realname pair to ensure: * Username isn't already in use * Username is based on realname * Username isn't restricted.""" if search.user_exists(username): raise ValidationError('Username {} already exists.'.format(username)) try: validators.validate_username(username) except ValueError as ex: raise ValidationError(str(ex)) SIMILARITY_THRESHOLD = 2 if similarity_heuristic(realname, username) > SIMILARITY_THRESHOLD: raise ValidationWarning( 'Username {} not based on real name {}'.format(username, realname)) if any(word in username for word in constants.BAD_WORDS): raise ValidationWarning('Username {} contains bad words'.format(username)) if any(word in username for word in constants.RESTRICTED_WORDS): raise ValidationWarning('Username {} contains restricted words'.format(username))
def email_for_user(username, check_exists=True): """Return email for a user. Currently, just appends @ocf.berkeley.edu, but could eventually do something more complicated. """ if check_exists: from ocflib.account.search import user_exists if not user_exists(username): raise ValueError('Account "{}" does not exist.'.format(username)) return '{}@ocf.berkeley.edu'.format(username)
def clean_ocf_account(self): data = self.cleaned_data['ocf_account'] if not search.user_exists(data): raise forms.ValidationError('OCF user account does not exist.') ocf_accounts = get_authorized_accounts_for(self.calnet_uid) if data not in ocf_accounts: raise forms.ValidationError( 'OCF user account and CalNet UID mismatch.') return data
def clean_ocf_account(self): data = self.cleaned_data['ocf_account'] if not user_exists(data): raise forms.ValidationError('OCF user account does not exist.') extra = '' ocf_accounts = get_accounts_for(self.calnet_uid) try: ocf_accounts += get_accounts_signatory_for(self.calnet_uid) except ConnectionError: extra = CALLINK_ERROR_MSG + '\n' if data not in ocf_accounts: raise forms.ValidationError( extra + 'OCF user account and CalNet UID mismatch.') return data
def get_quota(c, user): """Return a UserQuota representing the user's quota.""" if user == 'pubstaff': return UserQuota('pubstaff', 500, 500) if not user_exists(user) or user_is_group(user): return UserQuota(user, 0, 0) c.execute( 'SELECT `today`, `semester` FROM `printed` WHERE `user` = %s', (user,) ) row = c.fetchone() if not row: row = {'today': 0, 'semester': 0} semesterly = max(0, SEMESTERLY_QUOTA - int(row['semester'])) return UserQuota( user=user, daily=max(0, min(semesterly, daily_quota() - int(row['today']))), semesterly=semesterly, )
def get_quota(c, user): """Return a UserQuota representing the user's quota.""" if is_in_group(user, 'opstaff'): return UserQuota(user, 500, 500) if not user_exists(user) or user_is_group(user): return UserQuota(user, 0, 0) c.execute( 'SELECT `today`, `semester` FROM `printed` WHERE `user` = %s', (user,) ) row = c.fetchone() if not row: row = {'today': 0, 'semester': 0} semesterly = max(0, SEMESTERLY_QUOTA - int(row['semester'])) return UserQuota( user=user, daily=max(0, min(semesterly, daily_quota() - int(row['today']))), semesterly=semesterly, )
def test_user_exists(user, exists): assert user_exists(user) == exists
def clean_ocf_account(self): data = self.cleaned_data['ocf_account'] if not user_exists(data): raise forms.ValidationError('OCF user account does not exist.') return data