def test_habitat_names(self): for value_str, friendly_text, expected_name in habitat_data: cv = models.CharacterValue() cv.value_str = value_str cv.friendly_text = friendly_text cvlist = [cv] namelist = habitat_names(cvlist) self.assertEqual(expected_name, namelist[0])
def _save_length(request, character, taxon, minmax): tcvs = list( models.TaxonCharacterValue.objects.filter( character_value__character=character, taxon=taxon).select_related('character_value')) if tcvs: tcv = tcvs[0] value = tcv.character_value old_values = [value.value_min, value.value_max] is_value_shared = len(value.taxon_character_values.all()) # Delete any null/null lengths if minmax[0] is None and minmax[1] is None: tcv.delete() tcv.character_value.delete() return old_values if is_value_shared: tcv.delete() else: value.value_min = minmax[0] value.value_max = minmax[1] value.save() return else: old_values = [None, None] value = models.CharacterValue( character=character, value_min=minmax[0], value_max=minmax[1], ) value.save() models.TaxonCharacterValue(character_value=value, taxon=taxon).save() return old_values
def test_CharacterValue_value_with_string(self): cv = models.CharacterValue() cv.value_str = 'test value' self.assertEqual(cv.value, 'test value')
def test_CharacterValue_value_with_range(self): cv = models.CharacterValue() cv.value_min = 1 cv.value_max = 9 self.assertEqual(cv.value, (1, 9))
def test_CharacterValue_value_with_float(self): cv = models.CharacterValue() cv.value_flt = 1.9 self.assertEqual(cv.value, 1.9)
def _setup_sample_data(load_images=False): pilegroup1 = models.PileGroup(name='pilegroup1') pilegroup1.save() pilegroup2 = models.PileGroup(name='pilegroup2') pilegroup2.save() pile1 = models.Pile(name='pile1') pile1.pilegroup = pilegroup1 pile1.save() pile2 = models.Pile(name='pile2') pile2.pilegroup = pilegroup2 pile2.save() famfoo, created = models.Family.objects.get_or_create(name='Fooaceae') fambaz, created = models.Family.objects.get_or_create(name='Bazaceae') genfoo, created = models.Genus.objects.get_or_create(name='Fooium', family=famfoo) genbaz, created = models.Genus.objects.get_or_create(name='Bazia', family=fambaz) foo = models.Taxon(family=famfoo, genus=genfoo, scientific_name='Fooium fooia') foo.save() bar = models.Taxon(family=famfoo, genus=genfoo, scientific_name='Fooium barula') bar.save() abc = models.Taxon(family=fambaz, genus=genbaz, scientific_name='Bazia americana') abc.save() # Since loading the images slows things down, only load them if asked to. if load_images: image_type, c = models.ImageType.objects.get_or_create(name='taxon') content_type, c = ContentType.objects.get_or_create(model='', app_label='core') # Create one image. im1 = models.ContentImage(alt='im1 alt', rank=1, creator='photographer A', image_type=image_type, content_type=content_type, object_id=bar.id) filename = 'huperzia-appressa-ha-flastname-1.jpg' f = open('%s/%s' % (_testdata_dir(), filename), 'r') image_file = File(f) im1.image.save(filename, image_file) im1.save() f.close() # Create another image. im2 = models.ContentImage(alt='im2 alt', rank=2, creator='photographer B', image_type=image_type, content_type=content_type, object_id=bar.id) filename = 'huperzia-appressa-ha-flastname-1.jpg' f = open('%s/%s' % (_testdata_dir(), filename), 'r') image_file = File(f) im2.image.save(filename, image_file) im2.save() f.close() # Add the images to the taxon. bar.images = [im1, im2] bar.save() pile1.species.add(foo) pile1.species.add(bar) pile1.species.add(abc) cg1 = models.CharacterGroup(name='cg1', id='1') cg1.save() c1 = models.Character(short_name='c1', name='Character 1', character_group=cg1) c1.save() c2 = models.Character(short_name='c2', name='Character 2', character_group=cg1) c2.save() c3 = models.Character(short_name='c3', name='Character 3', character_group=cg1, value_type='LENGTH') c3.save() c4 = models.Character(short_name='c4', name='Character 4', character_group=cg1) c4.save() char_habitat = models.Character(short_name='habitat', name='Habitat', character_group=cg1) char_habitat.save() cv1_1 = models.CharacterValue(value_str='cv1_1', character=c1) cv1_1.save() cv1_2 = models.CharacterValue(value_str='cv1_2', character=c1) cv1_2.save() cv2 = models.CharacterValue(value_str='cv2', character=c2) cv2.save() cv3 = models.CharacterValue(value_min=5, value_max=11, character=c3) cv3.save() cv_habitat1 = models.CharacterValue(value_str='forests', character=char_habitat) cv_habitat1.save() cv_habitat2 = models.CharacterValue(value_str='edges of forests', character=char_habitat) cv_habitat2.save() pile1.characters.add(c1) pile1.characters.add(c2) pile1.characters.add(c3) pile1.characters.add(char_habitat) pile1.save() models.TaxonCharacterValue(taxon=foo, character_value=cv1_1).save() models.TaxonCharacterValue(taxon=bar, character_value=cv1_2).save() models.TaxonCharacterValue(taxon=bar, character_value=cv2).save() models.TaxonCharacterValue(taxon=bar, character_value=cv_habitat1).save() models.TaxonCharacterValue(taxon=bar, character_value=cv_habitat2).save() # Create a couple of default filters here, making sure not to create # one for *every* character, so as to exercise some code in the # handlers that deals with a default filter not existing for a character. df1 = models.DefaultFilter(pile=pile1, character=c1, order=1) df1.save() df2 = models.DefaultFilter(pile=pile1, character=c2, order=2) df2.save() ppc1 = models.PlantPreviewCharacter(pile=pile1, character=c1, order=1) ppc1.save() ppc2 = models.PlantPreviewCharacter(pile=pile1, character=c2, order=2) ppc2.save() pile1.plant_preview_characters.members = [ppc1, ppc2] pile1.save()