def download_profile_image(user=None, *args, **kwargs): is_azuread_oauth2 = kwargs['backend'] and kwargs[ 'backend'].name == "azuread-oauth2" and True or False if user and not user.profile.profile_image_uploaded_at and is_azuread_oauth2: try: username = user.username req = urllib2.Request( 'https://graph.microsoft.com/v1.0/me/photo/$value') req.add_header( 'Authorization', 'Bearer {0}'.format(kwargs['response']['access_token'])) resp = urllib2.urlopen(req) content = resp.read() fp = BytesIO() fp.write(content) image_file = files.File(fp) profile_image_names = get_profile_image_names(user.username) create_profile_images(image_file, profile_image_names) user.profile.profile_image_uploaded_at = now() user.save() log.info(LOG_MESSAGE_CREATE, { 'image_names': profile_image_names.values(), 'user_id': user.id }) except Exception as e: log.exception( 'Error when downloading user image from Microsoft API', exc_info=True)
def create_profile_image(self, user, storage): """ Creates profile image for user and checks that created image exists in storage """ with make_image_file() as image_file: create_profile_images(image_file, get_profile_image_names(user.username)) self.check_images(user, storage) set_has_profile_image(user.username, True, self.TEST_PROFILE_IMAGE_UPLOADED_AT)
def create_profile_image(self, user, storage): """ Creates profile image for user and checks that created image exists in storage """ with make_image_file() as image_file: create_profile_images(image_file, get_profile_image_names(user.username)) self.check_images(user, storage) set_has_profile_image(user.username, True, self.TEST_PROFILE_IMAGE_UPLOADED_AT)
def test_get_user_info_cookie_data(self): with make_image_file() as image_file: create_profile_images(image_file, get_profile_image_names(self.user.username)) self.user.profile.profile_image_uploaded_at = date.today() self.user.profile.save() actual = cookies_api._get_user_info_cookie_data( self.request, self.user) # pylint: disable=protected-access expected = { 'version': settings.EDXMKTG_USER_INFO_COOKIE_VERSION, 'username': self.user.username, 'header_urls': self._get_expected_header_urls(), 'user_image_urls': self._get_expected_image_urls(), } self.assertDictEqual(actual, expected)
def download_profile_image(auth_entry, strategy, details, user=None, *args, **kwargs): if user and not user.profile.profile_image_uploaded_at: try: username = user.username req = urllib2.Request('https://graph.microsoft.com/v1.0/me/photo/$value') req.add_header('Authorization', 'Bearer {0}'.format(kwargs['response']['access_token'])) resp = urllib2.urlopen(req) content = resp.read() fp = BytesIO() fp.write(content) image_file = files.File(fp) profile_image_names = get_profile_image_names(user.username) create_profile_images(image_file, profile_image_names) set_has_profile_image(username, True, _make_upload_dt()) except Exception as e: log.exception('Error when downloading user image from Microsoft API', exc_info=True)
def handle(self, *args, **options): """ Execute the command. """ username = options['username'] if options[ 'username'] else 'gdpr_test_user' email = options['email'] if options[ 'email'] else '*****@*****.**' course_uuid = options['course'] if options['course'] else uuid4().hex user, __ = User.objects.get_or_create(username=username, email=email) user_info = { 'email': email, 'first_name': "GDPR", 'last_name': "Test", 'is_active': True } for field, value in user_info.items(): setattr(user, field, value) user.set_password('gdpr test password') user.save() # UserProfile profile_image_uploaded_date = datetime(2018, 5, 3, tzinfo=UTC) user_profile, __ = UserProfile.objects.get_or_create(user=user) user_profile_info = { 'name': 'gdpr test name', 'meta': '{}', 'location': 'gdpr test location', 'year_of_birth': 1950, 'gender': 'gdpr test gender', 'mailing_address': 'gdpr test mailing address', 'city': 'Boston', 'country': 'US', 'bio': 'gdpr test bio', 'profile_image_uploaded_at': profile_image_uploaded_date } for field, value in user_profile_info.items(): setattr(user_profile, field, value) user_profile.save() # Profile images with make_image_file() as image_file: create_profile_images(image_file, {10: "ten.jpg"}) # DataSharingConsent enterprise_customer, __ = EnterpriseCustomer.objects.get_or_create( # pylint: disable=no-member name='test gdpr enterprise customer', active=True, branding_configuration=None, catalog=None, enable_audit_enrollment=False, enable_data_sharing_consent=False, enforce_data_sharing_consent='at_enrollment', replace_sensitive_sso_username=True, site_id=1) DataSharingConsent.objects.get_or_create( username=username, enterprise_customer_id=enterprise_customer.uuid) # Sapsf data transmission enterprise_customer_user, __ = EnterpriseCustomerUser.objects.get_or_create( user_id=user.id, enterprise_customer_id=enterprise_customer.uuid) audit, __ = EnterpriseCourseEnrollment.objects.get_or_create( enterprise_customer_user=enterprise_customer_user) SapSuccessFactorsLearnerDataTransmissionAudit.objects.get_or_create( enterprise_course_enrollment_id=audit.id, completed_timestamp=10) # PendingEnterpriseCustomerUser PendingEnterpriseCustomerUser.objects.get_or_create( user_email=user.email, enterprise_customer_id=enterprise_customer.uuid) # EntitlementSupportDetail course_entitlement, __ = CourseEntitlement.objects.get_or_create( user_id=user.id, course_uuid=course_uuid) CourseEntitlementSupportDetail.objects.get_or_create( support_user=user, comments='test comments', entitlement_id=course_entitlement.id) # Misc. models that may contain PII of this user SoftwareSecurePhotoVerification.objects.get_or_create( user=user, name='gdpr test', face_image_url='https://fake_image_url.com', photo_id_image_url='gdpr_test', photo_id_key='gdpr_test') PendingEmailChange.objects.get_or_create(user=user, activation_key=uuid4().hex) UserOrgTag.objects.get_or_create(user=user) course_id = CourseKey.from_string("course-v1:edX+DemoX+Demo_Course") # Objects linked to the user via their original email CourseEnrollmentAllowed.objects.get_or_create(email=user.email) course_user_group, __ = CourseUserGroup.objects.get_or_create( name='test course user group', course_id=course_id) UnregisteredLearnerCohortAssignments.objects.get_or_create( email=user.email, course_user_group_id=course_user_group.id) # Enroll the user in a course CourseEnrollment.objects.get_or_create( course_id=course_id, user_id=user.id, )
def handle(self, *args, **options): if not options.get('aws_access_key'): raise CommandError( "migrate_profile_images command requires AWS access key in --aws-access-key" ) if not options.get('aws_access_secret'): raise CommandError( "migrate_profile_images command requires AWS access secret in --aws-access-secret" ) if not options.get('bucket_name'): raise CommandError( "migrate_profile_images command requires name of source s3 bucket in --bucket-name" ) aws_access_key = options.get('aws_access_key') aws_access_secret = options.get('aws_access_secret') bucket_name = options.get('bucket_name') try: log.info("Starting Migration of Profile Images from %s", bucket_name) bucket = Command.get_bucket_connection(aws_access_key, aws_access_secret, bucket_name) for user in User.objects.exclude( profile__avatar_url__isnull=True).exclude( profile__avatar_url__exact=''): try: image_key = ( user.profile.avatar_url.split('/')[-2]) + '/' + ( user.profile.avatar_url.split('/')[-1]) image_url = Command.get_file_url(bucket, image_key) except IndexError: log.info('Unknown avatar url(%s) for %s', user.profile.avatar_url, user.username) continue log.info("Get image_url %s of %s", image_url, user.username) if image_url: with closing(urlopen(image_url)) as fd: image_file = BytesIO(fd.read()) # generate profile pic and thumbnails and store them profile_image_names = get_profile_image_names( user.username) create_profile_images(image_file, profile_image_names) # update the user account to reflect that a profile image is available. uploaded_at = datetime.datetime.utcnow().replace( tzinfo=utc) UserProfile.objects.filter(user=user).update( profile_image_uploaded_at=uploaded_at) log.info( "Profile image updated of %s with upload timestamp %s", user.username, uploaded_at) else: log.info( "Profile image for username %s not found in source bucket", bucket_name) except S3ResponseError as ex: log.info("Unable to connect to bucket %s. %s", bucket_name, ex.message)
def handle(self, *args, **options): """ Execute the command. """ username = options['username'] if options['username'] else 'gdpr_test_user' email = options['email'] if options['email'] else '*****@*****.**' course_uuid = options['course'] if options['course'] else uuid4().hex user, __ = User.objects.get_or_create( username=username, email=email ) user_info = { 'email': email, 'first_name': "GDPR", 'last_name': "Test", 'is_active': True } for field, value in user_info.items(): setattr(user, field, value) user.set_password('gdpr test password') user.save() # UserProfile profile_image_uploaded_date = datetime(2018, 5, 3, tzinfo=UTC) user_profile, __ = UserProfile.objects.get_or_create( user=user ) user_profile_info = { 'name': 'gdpr test name', 'meta': '{}', 'location': 'gdpr test location', 'year_of_birth': 1950, 'gender': 'gdpr test gender', 'mailing_address': 'gdpr test mailing address', 'city': 'Boston', 'country': 'US', 'bio': 'gdpr test bio', 'profile_image_uploaded_at': profile_image_uploaded_date } for field, value in user_profile_info.items(): setattr(user_profile, field, value) user_profile.save() # Profile images with make_image_file() as image_file: create_profile_images( image_file, {10: "ten.jpg"} ) # DataSharingConsent enterprise_customer, __ = EnterpriseCustomer.objects.get_or_create( # pylint: disable=no-member name='test gdpr enterprise customer', active=True, branding_configuration=None, catalog=None, enable_audit_enrollment=False, enable_data_sharing_consent=False, enforce_data_sharing_consent='at_enrollment', replace_sensitive_sso_username=True, site_id=1 ) DataSharingConsent.objects.get_or_create( username=username, enterprise_customer_id=enterprise_customer.uuid ) # Sapsf data transmission enterprise_customer_user, __ = EnterpriseCustomerUser.objects.get_or_create( user_id=user.id, enterprise_customer_id=enterprise_customer.uuid ) audit, __ = EnterpriseCourseEnrollment.objects.get_or_create( enterprise_customer_user=enterprise_customer_user ) SapSuccessFactorsLearnerDataTransmissionAudit.objects.get_or_create( enterprise_course_enrollment_id=audit.id, completed_timestamp=10 ) # PendingEnterpriseCustomerUser PendingEnterpriseCustomerUser.objects.get_or_create( user_email=user.email, enterprise_customer_id=enterprise_customer.uuid ) # EntitlementSupportDetail course_entitlement, __ = CourseEntitlement.objects.get_or_create( user_id=user.id, course_uuid=course_uuid ) CourseEntitlementSupportDetail.objects.get_or_create( support_user=user, comments='test comments', entitlement_id=course_entitlement.id ) # Misc. models that may contain PII of this user SoftwareSecurePhotoVerification.objects.get_or_create( user=user, name='gdpr test', face_image_url='https://fake_image_url.com', photo_id_image_url='gdpr_test', photo_id_key='gdpr_test' ) PendingEmailChange.objects.get_or_create( user=user, activation_key=uuid4().hex ) UserOrgTag.objects.get_or_create( user=user ) course_id = CourseKey.from_string("course-v1:edX+DemoX+Demo_Course") # Objects linked to the user via their original email CourseEnrollmentAllowed.objects.get_or_create( email=user.email ) course_user_group, __ = CourseUserGroup.objects.get_or_create( name='test course user group', course_id=course_id ) UnregisteredLearnerCohortAssignments.objects.get_or_create( email=user.email, course_user_group_id=course_user_group.id ) # Enroll the user in a course CourseEnrollment.objects.get_or_create( course_id=course_id, user_id=user.id, )