def test_exists_after_creation(self): """ Test that image creation and upload to S3 works. """ images = Image.objects.all() self.assertTrue(len(images) == 0) i = Image(description = "cute dog!", owner=self.account) i.tip = self.tip i.image_large.name = "dog_shelf.jpeg" input_file_path = os.path.join(self.test_image_dir, "dog_shelf.jpeg") with open(input_file_path, "rb") as f_in: content = ImageFile(f_in) default_storage.save(i.image_large.name, content) i.save() # -------------------------------------------------------------------- # Verify that the Django ORM representation exists. # -------------------------------------------------------------------- images = Image.objects.all() self.assertTrue(len(images) == 1) image = images[0] self.assertEqual(i.image_large.name, "dog_shelf.jpeg") # -------------------------------------------------------------------- # -------------------------------------------------------------------- # Verify that an HTTP GET of the image URL works. # -------------------------------------------------------------------- r = requests.get(i.get_absolute_url()) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers.get("Content-Type", None), "image/jpeg")
def test_image_add(self): tip = Tip(title = "Peel Pub", location = "Peel Street, near McGill campus", guide = self.guide) tip.save() self.assertEqual(len(tip.image_set.all()), 0) i = Image(description = "cute dog!", owner=self.account) i.tip = tip i.save() self.assertEqual(len(tip.image_set.all()), 1)
def _test_guide_image(self, delete_image = None, delete_tip = None): guide = Guide(title = "What to do in Montreal!", location = "Montreal, Canada", owner = self.account, is_draft = True) guide.save() tip = Tip(title = "this is a tip title", location = "this is a tip location", description = "this is a tip description", guide = guide) tip.save() image = Image(owner = guide.owner, tip = tip, is_uploaded = False) image.save() self.assertEqual(guide.image, None) image.is_uploaded = True image.save() self.assertEqual(guide.image.pk, image.pk) if delete_image: image.delete() elif delete_tip: tip.delete() self.assertEqual(guide.image, None)
def test_final_sizes(self, temp_image, original_size, thumbnail_size): image = Image(original_image=temp_image) image.save(original_size=original_size, thumbnail_size=thumbnail_size) assert (image.preview_image.width, image.preview_image.height) == thumbnail_size assert (image.original_image.width, image.original_image.height) <= original_size
def get_absolute_url(self): from apps.images.models import Image return Image.get_url(self.image.url)
def _migrate_chemical_analyses(self): print("Migrating chemical analyses...") User = get_user_model() all_references = [] old_chemical_analyses = queryset_iterator( LegacyChemicalAnalyses .objects .all() .select_related('subsample', 'reference', 'user') ) ChemicalAnalysis.objects.all().delete() for record in old_chemical_analyses: print("Migrating chemical analyses: #{}".format(record.pk)) if record.large_rock == 'Y': mineral = Mineral.objects.get(name='Bulk Rock') else: try: mineral = Mineral.objects.get(name=record.mineral.name) except AttributeError: mineral = None if record.reference: all_references.append(record.reference.name) subsample = Subsample.objects.get( name=record.subsample.name, sample=Sample.objects.get( pk=SampleMapping.objects.get( old_sample_id=record.subsample.sample.pk ).new_sample_id) ) try: reference=record.reference.name except AttributeError: reference = None try: reference_image = Image.get(subsample=subsample) except Image.DoesNotExist: reference_image = None chem_analysis = ChemicalAnalysis.objects.create( subsample=subsample, public_data=True if record.public_data == 'Y' else False, reference_x=record.reference_x, reference_y=record.reference_y, stage_x = record.stage_x, stage_y = record.stage_y, analysis_method = record.analysis_method, where_done = record.where_done, analyst = record.analyst, analysis_date = record.analysis_date, date_precision = record.date_precision, description = record.description, total = record.total, spot_id = record.spot_id, mineral=mineral, owner=User.objects.get(email=record.user.email), reference=reference, reference_image=reference_image ) legacy_cae = LegacyChemicalAnalysisElements.objects.filter( chemical_analysis=record ).select_related('element') for cae in legacy_cae: ChemicalAnalysisElement.objects.create( chemical_analysis=chem_analysis, element=Element.objects.get(name=cae.element.name), amount=cae.amount, precision = cae.precision, precision_type = cae.precision_type, measurement_unit = cae.measurement_unit, min_amount = cae.min_amount, max_amount = cae.max_amount, ) legacy_cao = LegacyChemicalAnalysisOxides.objects.filter( chemical_analysis=record ).select_related('oxide') for cao in legacy_cao: ChemicalAnalysisOxide.objects.create( chemical_analysis=chem_analysis, oxide=Oxide.objects.get(species=cao.oxide.species), amount=cao.amount, precision = cao.precision, precision_type = cao.precision_type, measurement_unit = cao.measurement_unit, min_amount = cao.min_amount, max_amount = cao.max_amount, ) if all_references: for ref in all_references: Reference.objects.get_or_create(name=ref)