def test_command_by_activation_keys(self): """ Test population of created_on_site attribute by activation keys. """ call_command( "populate_created_on_site_user_attribute", "--activation-keys", self.activation_keys, "--site-domain", self.site.domain ) for register_user in self.registered_users: self.assertEqual(UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE), self.site.domain) # Populate 'created_on_site' attribute with different site domain call_command( "populate_created_on_site_user_attribute", "--activation-keys", self.activation_keys, "--site-domain", self.site_other.domain ) for register_user in self.registered_users: # 'created_on_site' attribute already exists. Attribute's value will not change self.assertNotEqual( UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE), self.site_other.domain )
def _record_utm_registration_attribution(request, user): """ Attribute this user's registration to the latest UTM referrer, if applicable. """ utm_cookie_name = RegistrationCookieConfiguration.current().utm_cookie_name utm_cookie = request.COOKIES.get(utm_cookie_name) if user and utm_cookie: utm = json.loads(utm_cookie) for utm_parameter_name in REGISTRATION_UTM_PARAMETERS: utm_parameter = utm.get(utm_parameter_name) if utm_parameter: UserAttribute.set_user_attribute( user, REGISTRATION_UTM_PARAMETERS.get(utm_parameter_name), utm_parameter ) created_at_unixtime = utm.get('created_at') if created_at_unixtime: # We divide by 1000 here because the javascript timestamp generated is in milliseconds not seconds. # PYTHON: time.time() => 1475590280.823698 # JS: new Date().getTime() => 1475590280823 created_at_datetime = datetime.datetime.fromtimestamp(int(created_at_unixtime) / float(1000), tz=UTC) UserAttribute.set_user_attribute( user, REGISTRATION_UTM_CREATED_AT, created_at_datetime )
def _record_affiliate_registration_attribution(request, user): """ Attribute this user's registration to the referring affiliate, if applicable. """ affiliate_id = request.COOKIES.get(settings.AFFILIATE_COOKIE_NAME) if user and affiliate_id: UserAttribute.set_user_attribute(user, REGISTRATION_AFFILIATE_ID, affiliate_id)
def create_or_set_user_attribute_created_on_site(user, site): """ Create or Set UserAttribute indicating the site the user account was created on. User maybe created on 'courses.edx.org', or a white-label site. Due to the very high traffic on this table we now ignore the default site (eg. 'courses.edx.org') and code which comsumes this attribute should assume a 'created_on_site' which doesn't exist belongs to the default site. """ if site and site.id != settings.SITE_ID: UserAttribute.set_user_attribute(user, 'created_on_site', site.domain)
def _get_batched_users(self, site_domain, users_queryset, offset, users_query_batch_size): """ Args: site_domain: site where we need unsynced users users_queryset: users_queryset to slice users_query_batch_size: slice size Returns: site users """ self.stdout.write( u'Fetching Users for site {site} from {start} to {end}'.format( site=site_domain, start=offset, end=offset + users_query_batch_size)) users = users_queryset.select_related( 'profile')[offset:offset + users_query_batch_size] site_users = [ user for user in users if UserAttribute.get_user_attribute( user, 'created_on_site') == site_domain ] self.stdout.write( u'\tSite Users={count}'.format(count=len(site_users))) return site_users
def test_with_invalid_site_domain(self, populate): """ Test management command with invalid site domain. """ fake_site_domain = 'fake-site-domain' with mock.patch('six.moves.input', return_value=populate): call_command("populate_created_on_site_user_attribute", "--users", self.user_ids, "--site-domain", fake_site_domain) for user in self.users: if populate == 'y': assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) == fake_site_domain else: assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) is None
def test_get_set_attribute(self): assert UserAttribute.get_user_attribute(self.user, self.name) is None UserAttribute.set_user_attribute(self.user, self.name, self.value) assert UserAttribute.get_user_attribute(self.user, self.name) == self.value new_value = 'new_value' UserAttribute.set_user_attribute(self.user, self.name, new_value) assert UserAttribute.get_user_attribute(self.user, self.name) == new_value
def test_get_set_attribute(self): self.assertIsNone(UserAttribute.get_user_attribute(self.user, self.name)) UserAttribute.set_user_attribute(self.user, self.name, self.value) self.assertEqual(UserAttribute.get_user_attribute(self.user, self.name), self.value) new_value = 'new_value' UserAttribute.set_user_attribute(self.user, self.name, new_value) self.assertEqual(UserAttribute.get_user_attribute(self.user, self.name), new_value)
def test_command_by_user_ids(self): """ Test population of created_on_site attribute by user ids. """ call_command("populate_created_on_site_user_attribute", "--users", self.user_ids, "--site-domain", self.site.domain) for user in self.users: assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) == self.site.domain # Populate 'created_on_site' attribute with different site domain call_command("populate_created_on_site_user_attribute", "--users", self.user_ids, "--site-domain", self.site_other.domain) for user in self.users: # 'created_on_site' attribute already exists. Attribute's value will not change assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) != self.site_other.domain
def test_command_with_invalid_arguments(self): """ Test management command with invalid user ids and activation keys. """ user = self.users[0] call_command( "populate_created_on_site_user_attribute", "--users", '9{id}'.format(id=user.id), # invalid id "--site-domain", self.site.domain ) self.assertIsNone(UserAttribute.get_user_attribute(user, CREATED_ON_SITE)) register_user = self.registered_users[0] call_command( "populate_created_on_site_user_attribute", "--activation-keys", "invalid-{key}".format(key=register_user.activation_key), # invalid key "--site-domain", self.site.domain ) self.assertIsNone(UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE))
def _create_users(cls, site_conf): # lint-amnesty, pylint: disable=missing-function-docstring # Create some test users for i in range(1, 20): profile_meta = { "first_name": "First Name{0}".format(i), "last_name": "Last Name{0}".format(i), "company": "Company{0}".format(i), "title": "Title{0}".format(i), "state": "State{0}".format(i), "country": "US", } loe = UserProfile.LEVEL_OF_EDUCATION_CHOICES[0][0] date_joined = timezone.now() - timedelta(i) user = UserFactory(date_joined=date_joined) user_profile = user.profile user_profile.level_of_education = loe user_profile.meta = json.dumps(profile_meta) user_profile.save() # pylint: disable=no-member UserAttribute.set_user_attribute(user, 'created_on_site', site_conf.site.domain) cls.users.append(user)
def test_command_with_invalid_arguments(self): """ Test management command with invalid user ids and activation keys. """ user = self.users[0] call_command( "populate_created_on_site_user_attribute", "--users", f'9{user.id}', # invalid id "--site-domain", self.site.domain) assert UserAttribute.get_user_attribute(user, CREATED_ON_SITE) is None register_user = self.registered_users[0] call_command( "populate_created_on_site_user_attribute", "--activation-keys", f"invalid-{register_user.activation_key}", # invalid key "--site-domain", self.site.domain) assert UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE) is None
def handle(self, *args, **options): site_domain = options['site_domain'] user_ids = options['users'].split(',') if options['users'] else [] activation_keys = options['activation_keys'].split( ',') if options['activation_keys'] else [] if not user_ids and not activation_keys: raise CommandError('You must provide user ids or activation keys.') try: Site.objects.get(domain__exact=site_domain) except Site.DoesNotExist: question = "The site you specified is not configured as a Site in the system. " \ "Are you sure you want to continue? (y/n):" if str(six.moves.input(question)).lower().strip()[0] != 'y': return for user_id in user_ids: try: user = User.objects.get(id=user_id) if UserAttribute.get_user_attribute(user, CREATED_ON_SITE): self.stdout.write( "created_on_site attribute already exists for user id: {id}" .format(id=user_id)) else: UserAttribute.set_user_attribute(user, CREATED_ON_SITE, site_domain) except User.DoesNotExist: self.stdout.write( "This user id [{id}] does not exist in the system.".format( id=user_id)) for key in activation_keys: try: user = Registration.objects.get(activation_key=key).user if UserAttribute.get_user_attribute(user, CREATED_ON_SITE): self.stdout.write( "created_on_site attribute already exists for user id: {id}" .format(id=user.id)) else: UserAttribute.set_user_attribute(user, CREATED_ON_SITE, site_domain) except Registration.DoesNotExist: self.stdout.write( "This activation key [{key}] does not exist in the system." .format(key=key))
def _record_marketing_emails_opt_in_attribute(opt_in, user): """ Attribute this user's registration based on form data """ if settings.MARKETING_EMAILS_OPT_IN and user and opt_in: UserAttribute.set_user_attribute(user, MARKETING_EMAILS_OPT_IN, opt_in)
def test_unicode(self): UserAttribute.set_user_attribute(self.user, self.name, self.value) for field in (self.name, self.value, self.user.username): assert field in str(UserAttribute.objects.get(user=self.user))
def fetch_from_created_on_site_prop(user, domain): """ Fetch option. """ if not domain: return False return UserAttribute.get_user_attribute(user, 'created_on_site') == domain