def test_clean_username(self): """The username should sanitize the input name.""" eq_(unique_username('Allan Lasser'), 'AllanLasser', 'Spaces should be removed.') eq_(unique_username('*****@*****.**'), '*****@*****.**', 'Emails should be valid usernames.') eq_(unique_username('???dark$$$money!!!'), 'darkmoney', 'Illegal symbols should be removed.') eq_( unique_username('?security=vulnerability'), 'securityvulnerability', 'Names that are URL keyword arguments are DEFINITELY not allowed.')
def test_existing_username(self): """ If the expected username is already registered, the username should get a cool number appended to it. If multiple sequential usernames exist, the number will be incremented until a username is available. """ name = "Highlander" # there can only be one! username = unique_username(name) user = UserFactory(username=username) eq_(user.username, "Highlander") ok_(re.match(name + r"_[a-zA-Z]{8}", unique_username(name))) lower_name = name.lower() ok_(re.match(lower_name + r"_[a-zA-Z]{8}", unique_username(lower_name)))
def test_existing_username(self): """ If the expected username is already registered, the username should get a cool number appended to it. If multiple sequential usernames exist, the number will be incremented until a username is available. """ name = 'Highlander' # there can only be one! username = unique_username(name) user = UserFactory(username=username) eq_(user.username, 'Highlander', 'If no previous user, the name should be valid.') eq_(unique_username(name), 'Highlander1', 'If previous user, the name should have a number appended.') eq_(unique_username('highlander'), 'highlander1', 'Capitals should be ignored when determining uniqueness.')
def test_clean_username(self): """The username should sanitize the input name.""" eq_(unique_username("Allan Lasser"), "AllanLasser", "Spaces should be removed.") eq_( unique_username("*****@*****.**"), "*****@*****.**", "Emails should be valid usernames.", ) eq_( unique_username("???dark$$$money!!!"), "darkmoney", "Illegal symbols should be removed.", ) eq_( unique_username("?security=vulnerability"), "securityvulnerability", "Names that are URL keyword arguments are DEFINITELY not allowed.", )
def get_user(self): """Get the agency user for this agency""" try: return self.profile.user except Profile.DoesNotExist: user = User.objects.create_user(unique_username(self.name)) Profile.objects.create( user=user, acct_type='agency', date_update=date.today(), agency=self, ) return user
def miniregister(self, full_name, email, newsletter=False): """Create a new user from their full name and email and login""" password = generate_key(12) full_name = full_name.strip() username = unique_username(full_name) first_name, last_name = split_name(full_name) # create a new User user = User.objects.create_user(username, email, password, first_name=first_name, last_name=last_name) # create a new Profile Profile.objects.create(user=user, acct_type='basic', monthly_requests=settings.MONTHLY_REQUESTS.get( 'basic', 0), date_update=date.today()) # send the new user a welcome email welcome_miniregister.delay(user) user = authenticate( username=user.username, password=password, ) login(self.request, user) if newsletter: mailchimp_subscribe( self.request, user.email, source='Mini-Register: {}'.format(self.minireg_source), url='https://{}{}'.format(settings.MUCKROCK_URL, self.request.path), ) mixpanel_event( self.request, 'Sign Up', { 'Source': 'Mini-Register: {}'.format(self.minireg_source), 'Newsletter': newsletter, }, signup=True, ) return user
def import_users(self): """Import all of the users""" count = 0 self.stdout.write('Importing users...') key = self.bucket.get_key('foiamachine/data/fm_users.csv') user_log_key = self.bucket.new_key('foiamachine/data/user_log.csv') with smart_open(key) as users_file, smart_open(user_log_key, 'wb') as user_log_file: users = csv.reader(users_file, **CSV_OPTS) user_log = csv.writer(user_log_file, **CSV_OPTS) user_log.writerow([ 'muckrock username', 'foia machine username', 'email', 'first_name', 'last_name', 'new account', 'new username', ]) next(users) # drop the headers for (username, first_name, last_name, email, password, is_active, last_login, date_joined, mailing_address, mailing_city, mailing_state, mailing_zip, phone, is_verified) in users: # only create users who do not already have an account # associated with their email new_username = unique_username(username) defaults = { 'username': new_username, 'first_name': first_name, 'last_name': last_name, 'password': password, 'is_staff': False, 'is_active': is_active == '1', 'is_superuser': False, 'last_login': parser.parse(last_login), 'date_joined': parser.parse(date_joined), } if email: user, created = User.objects.get_or_create( email=email, defaults=defaults, ) else: continue user_log.writerow([ user.username, username, email, first_name, last_name, created, user.username != username, ]) # if we created a new user, create their corresponding profile if created: count += 1 if mailing_state.strip(): mailing_state = mailing_state.strip().lower().replace( '.', '') try: state = STATES_NORMALIZED[mailing_state] except KeyError: if '-' in mailing_state: try: state = STATES_NORMALIZED[ mailing_state.split('-')[0].strip()] except KeyError: # print 'Bad state:', mailing_state pass elif ' ' in mailing_state: try: state = STATES_NORMALIZED[ mailing_state.split(' ')[0].strip()] except KeyError: # print 'Bad state:', mailing_state pass else: # print 'Bad state:', mailing_state pass else: state = '' if phone: try: phone = USPhoneNumberField().clean(phone) except ValidationError: # print 'Bad phone:', phone pass else: phone = '' if len(mailing_zip) > 10: # print 'Bad zip:', mailing_zip mailing_zip = '' if len(mailing_address) > 50: # print 'Bad address:', mailing_address mailing_address = '' Profile.objects.create( user=user, address1=mailing_address, city=mailing_city, state=state, zip_code=mailing_zip, phone=phone, email_confirmed=is_verified, acct_type='basic', num_requests=5, source='foia machine', date_update=date.today(), ) print 'Imported %s new users' % count