def remove_profile_images(profile_image_names): """ Physically remove the image files specified in `profile_image_names` """ storage = get_profile_image_storage() for name in profile_image_names.values(): storage.delete(name)
def create_profile_images(image_file, profile_image_names): """ Generates a set of image files based on image_file and stores them according to the sizes and filenames specified in `profile_image_names`. Arguments: image_file (file): The uploaded image file to be cropped and scaled to use as a profile image. The image is cropped to the largest possible square, and centered on this image. profile_image_names (dict): A dictionary that maps image sizes to file names. The image size is an integer representing one side of the equilateral image to be created. Returns: None """ storage = get_profile_image_storage() original = Image.open(image_file) image = _set_color_mode_to_rgb(original) image = _crop_image_to_square(image) for size, name in profile_image_names.items(): scaled = _scale_image(image, size) exif = _get_corrected_exif(scaled, original) with closing(_create_image_file(scaled, exif)) as scaled_image_file: storage.save(name, scaled_image_file)
def create_profile_images(image_file, profile_image_names): """ Generates a set of image files based on image_file and stores them according to the sizes and filenames specified in `profile_image_names`. """ image_obj = Image.open(image_file) # first center-crop the image if needed (but no scaling yet). width, height = image_obj.size if width != height: side = width if width < height else height image_obj = image_obj.crop(((width - side) / 2, (height - side) / 2, (width + side) / 2, (height + side) / 2)) storage = get_profile_image_storage() for size, name in profile_image_names.items(): scaled_image_file = _get_scaled_image_file(image_obj, size) # Store the file. try: image_path = "/edx/var/edxapp/media/profile-images/%s" % name if os.path.exists(image_path): os.remove(image_path) storage.save(name, scaled_image_file) finally: scaled_image_file.close()
def get_profile_image_urls_by_username(username, profile_image_uploaded_at): """ Return a dict {size:url} for each profile image for a given user. Arguments: username username of user for whom we are getting urls. profile_image_uploaded_at datetime when profile image uploaded Returns: dictionary of {size_display_name: url} for each image. """ if profile_image_uploaded_at: urls = _get_profile_image_urls( _make_profile_image_name(username), get_profile_image_storage(), version=profile_image_uploaded_at.strftime("%s"), ) else: urls = _get_default_profile_image_urls() urls = {size_display_name: prefix_with_lms_base(url) for size_display_name, url in urls.items()} data = {'has_image': True if profile_image_uploaded_at else False} data.update({ '{image_key_prefix}_{size}'.format(image_key_prefix=PROFILE_IMAGE_KEY_PREFIX, size=size_display_name): url for size_display_name, url in urls.items() }) return data
def setUp(self): super(ProfileImageEndpointMixin, self).setUp() self.user = UserFactory.create(password=TEST_PASSWORD) # Ensure that parental controls don't apply to this user self.user.profile.year_of_birth = 1980 self.user.profile.save() self.url = reverse(self._view_name, kwargs={'username': self.user.username}) self.client.login(username=self.user.username, password=TEST_PASSWORD) self.storage = get_profile_image_storage() self.table = 'auth_userprofile' # this assertion is made here as a sanity check because all tests # assume user.profile.has_profile_image is False by default self.assertFalse(self.user.profile.has_profile_image) # Reset the mock event tracker so that we're not considering the # initial profile creation events. self.reset_tracker()
def create_profile_images(image_file, profile_image_names): """ Generates a set of image files based on image_file and stores them according to the sizes and filenames specified in `profile_image_names`. """ image_obj = Image.open(image_file) # first center-crop the image if needed (but no scaling yet). width, height = image_obj.size if width != height: side = width if width < height else height image_obj = image_obj.crop(((width - side) / 2, (height - side) / 2, (width + side) / 2, (height + side) / 2)) storage = get_profile_image_storage() for size, name in profile_image_names.items(): scaled_image_file = _get_scaled_image_file(image_obj, size) # Store the file. try: storage.save(name, scaled_image_file) finally: scaled_image_file.close()