def merge_duplicate_users(): print('Starting {}...'.format(sys._getframe().f_code.co_name)) start = timezone.now() from framework.mongo.handlers import database duplicates = database.user.aggregate([{ "$group": { "_id": "$username", "ids": { "$addToSet": "$_id" }, "count": { "$sum": 1 } } }, { "$match": { "count": { "$gt": 1 } } }, { "$sort": { "count": -1 } }]).get('result') # [ # { # 'count': 5, # '_id': '*****@*****.**', # 'ids': [ # 'listo','fidst','hatma','tchth','euser','name!' # ] # } # ] print('Found {} duplicate usernames.'.format(len(duplicates))) for duplicate in duplicates: print('Found {} copies of {}'.format(len(duplicate.get('ids')), duplicate.get('_id'))) if duplicate.get('_id'): # _id is an email address, merge users keeping the one that was logged into last users = list( MODMUser.find(MQ('_id', 'in', duplicate.get('ids'))).sort('-last_login')) best_match = users.pop() for user in users: print('Merging user {} into user {}'.format( user._id, best_match._id)) best_match.merge_user(user) else: # _id is null, set all usernames to their guid users = MODMUser.find(MQ('_id', 'in', duplicate.get('ids'))) for user in users: print('Setting username for {}'.format(user._id)) user.username = user._id user.save() print('Done with {} in {} seconds...'.format( sys._getframe().f_code.co_name, (timezone.now() - start).total_seconds()))
def save_bare_system_tags(page_size=10000): print('Starting save_bare_system_tags...') start = timezone.now() things = list(MODMNode.find(MQ( 'system_tags', 'ne', [])).sort('-_id')) + list( MODMUser.find(MQ('system_tags', 'ne', [])).sort('-_id')) system_tag_ids = [] for thing in things: for system_tag in thing.system_tags: system_tag_ids.append(system_tag) unique_system_tag_ids = set(system_tag_ids) total = len(unique_system_tag_ids) system_tags = [] for system_tag_id in unique_system_tag_ids: system_tags.append(Tag(name=system_tag_id, system=True)) created_system_tags = Tag.objects.bulk_create(system_tags) print('MODM System Tags: {}'.format(total)) print('django system tags: {}'.format( Tag.objects.filter(system=True).count())) print('Done with {} in {} seconds...'.format( sys._getframe().f_code.co_name, (timezone.now() - start).total_seconds()))
def update_comments_viewed_timestamp(): users = User.find(Q('comments_viewed_timestamp', 'ne', None) & Q('comments_viewed_timestamp', 'ne', {})) for user in users: if user.comments_viewed_timestamp: timestamps = {} dirty = False for node_id in user.comments_viewed_timestamp: node_timestamps = user.comments_viewed_timestamp[node_id] if isinstance(node_timestamps, dict): # node timestamp if node_timestamps.get('node', None): timestamps[node_id] = node_timestamps['node'] dirty = True # file timestamps file_timestamps = node_timestamps.get('files', None) if file_timestamps: for file_id in file_timestamps: timestamps[file_id] = file_timestamps[file_id] dirty = True else: timestamps[node_id] = node_timestamps if dirty: user.comments_viewed_timestamp = timestamps user.save() logger.info('Migrated timestamp for user {0}'.format(user._id))
def get_queryset(self): log = self.get_log() associated_contrib_ids = log.params.get('contributors') if associated_contrib_ids is None: return [] associated_users = User.find(Q('_id', 'in', associated_contrib_ids)) return associated_users
def test_all_users_have_wiki_osfstorage_enabled(self): all_user_count = User.find().count() results = AddonSnapshot().get_events() osfstorage_res = [res for res in results if res['provider']['name'] == 'osfstorage'][0] wiki_res = [res for res in results if res['provider']['name'] == 'osfstorage'][0] assert_equal(osfstorage_res['users']['enabled'], all_user_count) assert_equal(wiki_res['users']['enabled'], all_user_count)
def update_comments_viewed_timestamp(): users = User.find(Q('comments_viewed_timestamp', 'ne', None) | Q('comments_viewed_timestamp', 'ne', {})) for user in users: if user.comments_viewed_timestamp: for node in user.comments_viewed_timestamp: user.comments_viewed_timestamp[node] = {'node': user.comments_viewed_timestamp[node]} user.save() logger.info('Migrated timestamp for user {0}'.format(user._id))
def test_all_users_have_wiki_osfstorage_enabled(self): all_user_count = User.find().count() results = AddonSnapshot().get_events() osfstorage_res = [res for res in results if res["provider"]["name"] == "osfstorage"][0] wiki_res = [res for res in results if res["provider"]["name"] == "osfstorage"][0] assert_equal(osfstorage_res["users"]["enabled"], all_user_count) assert_equal(wiki_res["users"]["enabled"], all_user_count)
def get_targets(): logger.info('Acquiring targets...') targets = [ u for u in User.find() if Node.find( Q('is_bookmark_collection', 'eq', True) & Q('is_deleted', 'eq', False) & Q('creator', 'eq', u._id)).count() > 1 ] logger.info('Found {} target users.'.format(len(targets))) return targets
def update_comments_viewed_timestamp(): users = User.find( Q('comments_viewed_timestamp', 'ne', None) | Q('comments_viewed_timestamp', 'ne', {})) for user in users: if user.comments_viewed_timestamp: for node in user.comments_viewed_timestamp: user.comments_viewed_timestamp[node] = { 'node': user.comments_viewed_timestamp[node] } user.save() logger.info('Migrated timestamp for user {0}'.format(user._id))
def test_all_users_have_wiki_osfstorage_enabled(self): all_user_count = User.find().count() results = AddonSnapshot().get_events() osfstorage_res = [ res for res in results if res['provider']['name'] == 'osfstorage' ][0] wiki_res = [ res for res in results if res['provider']['name'] == 'osfstorage' ][0] assert_equal(osfstorage_res['users']['enabled'], all_user_count) assert_equal(wiki_res['users']['enabled'], all_user_count)
def ensure_external_identity_uniqueness(provider, identity, user=None): from framework.auth.core import User # avoid circular import users_with_identity = User.find(Q('external_identity.{}.{}'.format(provider, identity), 'ne', None)) for existing_user in users_with_identity: if user and user._id == existing_user._id: continue if existing_user.external_identity[provider][identity] == 'VERIFIED': if user and user.external_identity.get(provider, {}).get(identity, {}): user.external_identity[provider].pop(identity) if user.external_identity[provider] == {}: user.external_identity.pop(provider) user.save() # Note: This won't work in v2 because it rolls back transactions when status >= 400 raise ValidationError('Another user has already claimed this external identity') existing_user.external_identity[provider].pop(identity) if existing_user.external_identity[provider] == {}: existing_user.external_identity.pop(provider) existing_user.save() return
def test_user_with_claim_url_registers_new_account(self, mock_session): # Assume that the unregistered user data is already stored in the session mock_session.data = { 'unreg_user': { 'uid': self.user._primary_key, 'pid': self.project._primary_key, 'token': self.user.get_unclaimed_record( self.project._primary_key)['token'] } } res2 = self.app.get('/account/') # Fills in Register form form = res2.forms['registerForm'] form['register-fullname'] = 'tester' form['register-username'] = '******' form['register-username2'] = '*****@*****.**' form['register-password'] = '******' form['register-password2'] = 'testing' res3 = form.submit() assert_in('Registration successful.', res3.body) assert_in('Successfully claimed contributor', res3.body) u = User.find(Q('username', 'eq', '*****@*****.**'))[0] key = ApiKeyFactory() u.api_keys.append(key) u.save() u.auth = ('test', key._primary_key) self.app.get( u.get_confirmation_url('*****@*****.**')).follow(auth=u.auth) # Confirms their email address self.project.reload() self.user.reload() u.reload() assert_not_in(self.user._primary_key, self.project.contributors) assert_equal(2, len(self.project.contributors)) # user is now a contributor to self.project assert_in(u._primary_key, self.project.contributors)
def ensure_external_identity_uniqueness(provider, identity, user=None): from framework.auth.core import User # avoid circular import users_with_identity = User.find( Q('external_identity.{}.{}'.format(provider, identity), 'ne', None)) for existing_user in users_with_identity: if user and user._id == existing_user._id: continue if existing_user.external_identity[provider][identity] == 'VERIFIED': if user and user.external_identity.get(provider, {}).get( identity, {}): user.external_identity[provider].pop(identity) if user.external_identity[provider] == {}: user.external_identity.pop(provider) user.save( ) # Note: This won't work in v2 because it rolls back transactions when status >= 400 raise ValidationError( 'Another user has already claimed this external identity') existing_user.external_identity[provider].pop(identity) if existing_user.external_identity[provider] == {}: existing_user.external_identity.pop(provider) existing_user.save() return
def update_comments_viewed_timestamp(): users = User.find( Q('comments_viewed_timestamp', 'ne', None) & Q('comments_viewed_timestamp', 'ne', {})) for user in users: if user.comments_viewed_timestamp: timestamps = {} for node_id in user.comments_viewed_timestamp: node_timestamps = user.comments_viewed_timestamp[node_id] # node timestamp if node_timestamps.get('node', None): timestamps[node_id] = node_timestamps['node'] # file timestamps file_timestamps = node_timestamps.get('files', None) if file_timestamps: for file_id in file_timestamps: timestamps[file_id] = file_timestamps[file_id] user.comments_viewed_timestamp = timestamps user.save() logger.info('Migrated timestamp for user {0}'.format(user._id))
def test_user_with_claim_url_registers_new_account(self, mock_session): # Assume that the unregistered user data is already stored in the session mock_session.data = { 'unreg_user': { 'uid': self.user._primary_key, 'pid': self.project._primary_key, 'token': self.user.get_unclaimed_record( self.project._primary_key)['token'] } } res2 = self.app.get('/account/') # Fills in Register form form = res2.forms['registerForm'] form['register-fullname'] = 'tester' form['register-username'] = '******' form['register-username2'] = '*****@*****.**' form['register-password'] = '******' form['register-password2'] = 'testing' res3 = form.submit() assert_in('Registration successful.', res3.body) assert_in('Successfully claimed contributor', res3.body) u = User.find(Q('username', 'eq', '*****@*****.**'))[0] key = ApiKeyFactory() u.api_keys.append(key) u.save() u.auth = ('test', key._primary_key) self.app.get(u.get_confirmation_url('*****@*****.**')).follow(auth=u.auth) # Confirms their email address self.project.reload() self.user.reload() u.reload() assert_not_in(self.user._primary_key, self.project.contributors) assert_equal(2, len(self.project.contributors)) # user is now a contributor to self.project assert_in(u._primary_key, self.project.contributors)
def get_users(): return User.find(Q('piwik_token', 'eq', None))
def get_queryset(self): return User.find(Q('_id', 'eq', self.context['request'].user._id))
def get_users(): """Get all users who will be subscribed to the OSF General mailing list.""" # Exclude unconfirmed and unregistered users # NOTE: Unclaimed and unconfirmed users have is_registered=False return User.find(Q('is_registered', 'eq', True))
def get_targets(): """Return a QuerySet containing confirmed Users who have unclaimed records.""" return User.find(QUERY)
def get_targets(): logger.info('Acquiring targets...') targets = [u for u in User.find() if Node.find(Q('is_bookmark_collection', 'eq', True) & Q('is_deleted', 'eq', False) & Q('creator', 'eq', u._id)).count() > 1] logger.info('Found {} target users.'.format(len(targets))) return targets
def get_queryset(self): return User.find(Q("_id", "eq", self.context["request"].user._id))