Beispiel #1
0
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
            )
Beispiel #2
0
 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)
Beispiel #3
0
 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)
Beispiel #4
0
    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(raw_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))
Beispiel #5
0
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
            )
Beispiel #6
0
def create_or_set_user_attribute_created_on_site(user, site):
    """
    Create or Set UserAttribute indicating the microsite site the user account was created on.
    User maybe created on 'courses.edx.org', or a white-label site
    """
    if site:
        UserAttribute.set_user_attribute(user, 'created_on_site', site.domain)
Beispiel #7
0
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)
Beispiel #8
0
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)
Beispiel #9
0
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)
Beispiel #10
0
def create_or_set_user_attribute_created_on_site(user, site):
    """
    Create or Set UserAttribute indicating the microsite 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 _create_users(cls, site_conf):
     # Create some test users
     for i in range(1, 11):
         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)
Beispiel #12
0
 def _create_users(cls, site_conf):
     # 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)
Beispiel #13
0
 def test_unicode(self):
     UserAttribute.set_user_attribute(self.user, self.name, self.value)
     for field in (self.name, self.value, self.user.username):
         self.assertIn(field, unicode(UserAttribute.objects.get(user=self.user)))
Beispiel #14
0
 def test_unicode(self):
     UserAttribute.set_user_attribute(self.user, self.name, self.value)
     for field in (self.name, self.value, self.user.username):
         self.assertIn(field, six.text_type(UserAttribute.objects.get(user=self.user)))