def _save_textual(request, character, taxon, vector): values = list(character.character_values.all()) values.sort(key=character_value_key) tcvs = list( models.TaxonCharacterValue.objects.filter(character_value__in=values, taxon=taxon)) tcvmap = {tcv.character_value_id: tcv for tcv in tcvs} old_values = [] for value, digit in zip(values, vector): has_it = value.id in tcvmap needs_it = digit == '1' if has_it: old_values.append(value.value_str) if needs_it and not has_it: models.TaxonCharacterValue(taxon=taxon, character_value=value).save() elif not needs_it and has_it: tcvmap[value.id].delete() return old_values
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 setup_sample_data(self): self.create(models.PileGroup, 'pilegroup1') self.assertEqual(self.pilegroup1.slug, 'pilegroup1') self.create(models.PileGroup, 'pilegroup2', slug='pilegroup2') self.create(models.Pile, 'Carnivores', pilegroup=self.pilegroup1) self.create(models.Pile, 'Pets', pilegroup=self.pilegroup1) self.create(models.Family, 'Canidae') self.create(models.Family, 'Felidae') self.create(models.Family, 'Leporidae') self.create(models.Genus, 'Vulpes', family=self.canidae) self.create(models.Genus, 'Felis', family=self.felidae) self.create(models.Genus, 'Oryctolagus', family=self.leporidae) self.create(models.Taxon, 'fox', family=self.canidae, genus=self.vulpes) self.create(models.Taxon, 'cat', family=self.felidae, genus=self.felis) self.create(models.Taxon, 'rabbit', family=self.leporidae, genus=self.oryctolagus) self.carnivores.species.add(self.fox) self.carnivores.species.add(self.cat) self.pets.species.add(self.cat) self.pets.species.add(self.rabbit) self.create(models.CharacterGroup, 'appearance') self.create(models.CharacterGroup, 'dimensions') self.create(models.Character, 'color', friendly_name='What color?', character_group=self.appearance, value_type=u'TEXT', pile=self.pets) self.create(models.Character, 'cuteness', character_group=self.appearance, value_type=u'TEXT', pile=self.pets) self.create(models.Character, 'length', character_group=self.dimensions, value_type=u'LENGTH', pile=self.pets) def make_cv(name, **kw): self.create(models.CharacterValue, name, **kw) make_cv('red', character=self.color) make_cv('orange', character=self.color) make_cv('gray', character=self.color) make_cv('chartreuse', character=self.color) make_cv('cute', character=self.cuteness) make_cv('size1', character=self.length, value_min=2, value_max=4) make_cv('size2', character=self.length, value_min=3, value_max=4) make_cv('size3', character=self.length, value_min=5, value_max=5) self.create(models.ImageType, 'habit') self.create(models.ImageType, 'stem') self.create(models.ImageType, 'pile image') for taxon, cv in ( (self.fox, self.red), (self.fox, self.size3), (self.cat, self.orange), (self.cat, self.gray), (self.cat, self.cute), (self.cat, self.size2), (self.rabbit, self.gray), (self.rabbit, self.cute), (self.rabbit, self.size1), ): tcv = models.TaxonCharacterValue(taxon=taxon, character_value=cv) tcv.save() self.tcv = tcv # saves the last one, for use by a test below
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()