def runTest(self): # Create an image with metadata. file_descriptor, file_path = tempfile.mkstemp(suffix='.jpg') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'JPEG') metadata = pyexiv2.Image(file_path) metadata.readMetadata() metadata['Exif.Image.ImageDescription'] = "Test file" metadata['Exif.Image.Artist'] = "Adam" metadata['Iptc.Application2.CountryName'] = "USA" metadata['Iptc.Application2.ProvinceState'] = "Virginia" metadata['Iptc.Application2.City'] = "Blacksburg" metadata['Iptc.Application2.SubLocation'] = "Dreamland" try: metadata['Exif.Photo.DateTimeOriginal'] = datetime.datetime( 2007, 9, 28, 3, 0) except TypeError: pass metadata['Iptc.Application2.Keywords'] = ['test', 'photo'] metadata.writeMetadata() # Create a Photo object. user = User.objects.create(username="******") album = Album.objects.create(owner=user, name="Test") photo = Photo() photo.owner = user image = open(file_path) photo.image = ImageFile(image) photo.album = album photo.is_jpeg = True photo.save() image.close() os.remove(file_path) # Synchronize. performed_sync = photo.sync_metadata_from_file() self.assertTrue(performed_sync) # Metadata should exist in the database. self.assertEqual(photo.description, 'Test file') self.assertEqual(photo.artist, 'Adam') self.assertEqual(photo.country, 'USA') self.assertEqual(photo.province_state, 'Virginia') self.assertEqual(photo.city, 'Blacksburg') self.assertEqual(photo.location, 'Dreamland') self.assertEqual(str(photo.time_created), '2007-09-28 03:00:00') self.assertEqual(photo.keyword_list, [u'test', u'photo']) # Attempt to synchronize again... self.assertFalse(photo.sync_metadata_from_file()) # Nothing should have changed. self.assertEqual(photo.description, 'Test file') self.assertEqual(photo.artist, 'Adam') self.assertEqual(photo.country, 'USA') self.assertEqual(photo.province_state, 'Virginia') self.assertEqual(photo.city, 'Blacksburg') self.assertEqual(photo.location, 'Dreamland') self.assertEqual(str(photo.time_created), '2007-09-28 03:00:00') self.assertEqual(photo.keyword_list, [u'test', u'photo']) # Synchronize only one field. metadata = pyexiv2.Image(photo.image.path) metadata.readMetadata() metadata['Exif.Image.ImageDescription'] = "Image for testing" metadata.writeMetadata() performed_sync = photo.sync_metadata_from_file() self.assertTrue(performed_sync) self.assertEqual(photo.description, 'Image for testing') # Disable synchronization. photo.metadata_sync_enabled = False photo.description = "Image for testing" photo.save() self.assertFalse(photo.sync_metadata_from_file()) # Sometimes metadata can't be read from the image. file_descriptor, file_path = tempfile.mkstemp(suffix='.pcx') os.close(file_descriptor) Image.new('RGB', (1, 1)).save(file_path, 'PCX') photo = Photo() photo.owner = user image = open(file_path) photo.image = ImageFile(image) photo.album = album photo.save() image.close() os.remove(file_path) self.assertFalse(photo.sync_metadata_from_file()) self.assertFalse(photo.metadata_sync_enabled) User.objects.all().delete() Album.objects.all().delete() PhotoTag.objects.all().delete() Photo.objects.all().delete()