def load_demos(): """Initializes the demo explorations.""" for index, exploration in enumerate(feconf.DEMO_EXPLORATIONS): if len(exploration) == 3: (exp_filename, title, category) = exploration image_filename = None elif len(exploration) == 4: (exp_filename, title, category, image_filename) = exploration else: raise Exception('Invalid demo exploration: %s' % exploration) image_id = None if image_filename: image_filepath = os.path.join( feconf.SAMPLE_IMAGES_DIR, image_filename) image_id = Image.create(utils.get_file_contents( image_filepath, raw_bytes=True)) yaml_content = utils.get_sample_exploration_yaml(exp_filename) exploration_id = create_from_yaml( yaml_content, None, title, category, exploration_id=str(index), image_id=image_id) exploration = Exploration.get(exploration_id) exploration.is_public = True exploration.put()
def load_demo_explorations(cls): """Initializes the demo explorations.""" for index, exploration in enumerate(feconf.DEMO_EXPLORATIONS): assert len(exploration) in [3, 4], ( 'Invalid format for demo exploration: %s' % exploration) yaml_filename = '%s.yaml' % exploration[0] yaml_file = utils.get_file_contents( os.path.join(feconf.SAMPLE_EXPLORATIONS_DIR, yaml_filename)) title = exploration[1] category = exploration[2] image_filename = exploration[3] if len(exploration) == 4 else None image_id = None if image_filename: with open(os.path.join( feconf.SAMPLE_IMAGES_DIR, image_filename)) as f: raw_image = f.read() image_id = Image.create(raw_image) exploration = cls.create_from_yaml( yaml_file=yaml_file, user=None, title=title, category=category, exploration_id=str(index), image_id=image_id) exploration.is_public = True exploration.put()
def post(self): """Saves an image uploaded by a content creator.""" raw = self.request.get('image') if raw: image_id = Image.create(raw) self.render_json({'image_id': image_id}) else: raise self.InvalidInputException('No image supplied')
def get(self, image_id): """Returns an image. Args: image_id: string representing the image id. """ try: image = Image.get(image_id) # TODO(sll): Support other image types. self.response.headers['Content-Type'] = str('image/%s' % image.format) self.response.write(image.raw) except: raise self.PageNotFoundException
def test_image_class(self): """Test the Image class.""" # An Image must have the 'raw' property set. image = Image(id='The hash id') with self.assertRaises(Image.ModelValidationError): image.put() # TODO: image format validation. # The 'raw' property must be a valid image. with self.assertRaises(Image.ModelValidationError): image.raw = 'The image' image.put() # Set the 'raw' property to be a valid image, then do a put(). with open('oppia/tests/data/img.png') as f: image.raw = File(f) image_file_content = image.raw.read() image.put() # Retrieve the image. retrieved_image = Image.objects.get(id='The hash id') # Read its content retrieved_content = retrieved_image.raw.read() self.assertEqual(retrieved_content, image_file_content)