def home_view(request): """View for the home page of the Go Botany site.""" partner = which_partner(request) # Get home page images for the partner home_page_images = HomePageImage.objects.filter(partner_site=partner) # Get or generate today's Plant of the Day, if appropriate. plant_of_the_day = PlantOfTheDay.get_by_date.for_day( date.today(), partner.short_name) plant_of_the_day_taxon = None if plant_of_the_day: # Get the Taxon record of the Plant of the Day. try: plant_of_the_day_taxon = Taxon.objects.get( scientific_name=plant_of_the_day.scientific_name) except ObjectDoesNotExist: pass plant_of_the_day_image = None species_images = botany.species_images(plant_of_the_day_taxon) if species_images: plant_of_the_day_image = botany.species_images( plant_of_the_day_taxon)[0] return render_per_partner( 'home.html', { 'home_page_images': home_page_images, 'plant_of_the_day': plant_of_the_day_taxon, 'plant_of_the_day_image': plant_of_the_day_image, }, request)
def home_view(request): """View for the home page of the Go Botany site.""" partner = which_partner(request) # Get home page images for the partner home_page_images = HomePageImage.objects.filter(partner_site=partner) # Get or generate today's Plant of the Day, if appropriate. plant_of_the_day = PlantOfTheDay.get_by_date.for_day( date.today(), partner.short_name) plant_of_the_day_taxon = None if plant_of_the_day: # Get the Taxon record of the Plant of the Day. try: plant_of_the_day_taxon = Taxon.objects.get( scientific_name=plant_of_the_day.scientific_name) except ObjectDoesNotExist: pass plant_of_the_day_image = None species_images = botany.species_images(plant_of_the_day_taxon) if species_images: plant_of_the_day_image = botany.species_images( plant_of_the_day_taxon)[0] return render_to_response_per_partner('home.html', { 'home_page_images': home_page_images, 'plant_of_the_day': plant_of_the_day_taxon, 'plant_of_the_day_image': plant_of_the_day_image, }, request)
def home_view(request): """View for the home page of the Go Botany site.""" home_page_images = HomePageImage.objects.all() # Get or generate today's Plant of the Day. partner = which_partner(request) plant_of_the_day = PlantOfTheDay.get_by_date.for_day(date.today(), partner.short_name) # Get the Taxon record of the Plant of the Day. plant_of_the_day_taxon = None try: plant_of_the_day_taxon = Taxon.objects.get(scientific_name=plant_of_the_day.scientific_name) except ObjectDoesNotExist: pass plant_of_the_day_image = None species_images = botany.species_images(plant_of_the_day_taxon) if species_images: plant_of_the_day_image = botany.species_images(plant_of_the_day_taxon)[0] return render_to_response( "gobotany/home.html", { "home_page_images": home_page_images, "plant_of_the_day": plant_of_the_day_taxon, "plant_of_the_day_image": plant_of_the_day_image, }, context_instance=RequestContext(request), )
def _simple_taxon(taxon, pile_slug=None, include_default_image=False, include_factoid=False): genus_name, epithet = taxon.scientific_name.lower().split(None, 1) url = reverse('taxa-species', args=(genus_name, epithet)) if pile_slug: url += '?' + urlencode({'pile': pile_slug}) common_name = '' common_names = taxon.common_names.all() if common_names: common_name = common_names[0].common_name json = { 'id': taxon.id, 'scientific_name': taxon.scientific_name, 'common_name': common_name, 'genus': taxon.scientific_name.split()[0], # faster than .genus.name 'family': taxon.family.name, 'taxonomic_authority': taxon.taxonomic_authority, 'url': url, 'images': [_taxon_image(i) for i in botany.species_images(taxon)], } if include_default_image: json['default_image'] = _taxon_image(taxon.get_default_image()) if include_factoid: json['factoid'] = taxon.factoid return json
def family_view(request, family_slug): family_name = family_slug.capitalize() family = get_object_or_404(Family, name=family_name) # If it is decided that common names will not be required, change the # default below to None so the template will omit the name if missing. DEFAULT_COMMON_NAME = 'common name here' common_name = family.common_name or DEFAULT_COMMON_NAME family_drawings = (family.images.filter( image_type__name='example drawing')) if not family_drawings: # No example drawings for this family were specified. Including # drawings here was planned early on but not finished for the # initial release. In the meantime, the first two species # images from the family are shown. species = family.taxa.all() for s in species: species_images = botany.species_images(s) if len(species_images) > 1: family_drawings = species_images[0:2] break family_drawings = _images_with_copyright_holders(family_drawings) pile = family.taxa.all()[0].piles.all()[0] pilegroup = pile.pilegroup return render_per_partner('family.html', { 'family': family, 'common_name': common_name, 'family_drawings': family_drawings, 'pilegroup': pilegroup, 'pile': pile, }, request)
def genus_view(request, genus_slug): genus_name = genus_slug.capitalize() genus = get_object_or_404(Genus, name=genus_name) # If it is decided that common names will not be required, change the # default below to None so the template will omit the name if missing. DEFAULT_COMMON_NAME = 'common name here' common_name = genus.common_name or DEFAULT_COMMON_NAME genus_drawings = genus.images.filter(image_type__name='example drawing') if not genus_drawings: # No example drawings for this genus were specified. Including # drawings here was planned early on but not finished for the # initial release. In the meantime, the first two species # images from the genus are shown. species = genus.taxa.all() for s in species: species_images = botany.species_images(s) if len(species_images) > 1: genus_drawings = species_images[0:2] break genus_drawings = _images_with_copyright_holders(genus_drawings) pile = genus.taxa.all()[0].piles.all()[0] pilegroup = pile.pilegroup return render_to_response('gobotany/genus.html', { 'genus': genus, 'common_name': common_name, 'genus_drawings': genus_drawings, 'pilegroup': pilegroup, 'pile': pile, }, context_instance=RequestContext(request))
def genus_view(request, genus_slug): genus_name = genus_slug.capitalize() genus = get_object_or_404(Genus, name=genus_name) # If it is decided that common names will not be required, change the # default below to None so the template will omit the name if missing. DEFAULT_COMMON_NAME = 'common name here' common_name = genus.common_name or DEFAULT_COMMON_NAME genus_drawings = genus.images.filter(image_type__name='example drawing') if not genus_drawings: # No example drawings for this genus were specified. Including # drawings here was planned early on but not finished for the # initial release. In the meantime, the first two species # images from the genus are shown. species = genus.taxa.all() for s in species: species_images = botany.species_images(s) if len(species_images) > 1: genus_drawings = species_images[0:2] break genus_drawings = _images_with_copyright_holders(genus_drawings) pile = genus.taxa.all()[0].piles.all()[0] pilegroup = pile.pilegroup return render_per_partner( 'genus.html', { 'genus': genus, 'common_name': common_name, 'genus_drawings': genus_drawings, 'pilegroup': pilegroup, 'pile': pile, }, request)
def genus_view(request, genus_slug): genus_name = genus_slug.capitalize() genus = get_object_or_404(Genus, name=genus_name) # If it is decided that common names will not be required, change the # default below to None so the template will omit the name if missing. DEFAULT_COMMON_NAME = 'common name here' common_name = genus.common_name or DEFAULT_COMMON_NAME genus_drawings = genus.images.filter(image_type__name='example drawing') if not genus_drawings: # No example drawings for this genus were specified. # Use 2 images from the genus's species instead. species = genus.taxa.all() genus_drawings = [] for s in species: species_images = botany.species_images(s, image_types='flowers,inflorescences') if len(species_images): genus_drawings.append(species_images[0]) if len(genus_drawings) == 2: break genus_drawings = _images_with_copyright_holders(genus_drawings) pile = genus.taxa.all()[0].piles.all()[0] pilegroup = pile.pilegroup return render_to_response('gobotany/genus.html', { 'genus': genus, 'common_name': common_name, 'genus_drawings': genus_drawings, 'pilegroup': pilegroup, 'pile': pile, }, context_instance=RequestContext(request))
def _get_plants_of_the_day(max_number_plants, partner_name): """Get recent plants of the day, generating some if needed.""" # Generate today's Plant of the Day if it hasn't been already. # If there is a gap of any recent days where no Plant of the Day # record exists yet, generate records for those days too. for i in range(max_number_plants): day = date.today() - timedelta(days=i) plant_of_the_day = PlantOfTheDay.objects.filter(last_seen=day, partner_short_name=partner_name, include=True) if len(plant_of_the_day) > 0: # Found the latest recent Plant of the Day record within # the date range used for the feed, so no more records # need to be generated. break else: # No Plant of the Day exists for that day, so generate one. plant_of_the_day = PlantOfTheDay.get_by_date.for_day(day, partner_name) # Now that any new records that were needed have been generated, plants = [] plant_records = ( PlantOfTheDay.objects.filter(include=True, last_seen__isnull=False, partner_short_name=partner_name).order_by( "-last_seen" ) )[:max_number_plants] for plant_record in plant_records: plant = {} plant["scientific_name"] = plant_record.scientific_name # Use the last_updated field because it is a date/time field, # rather than last_seen which is just a date field, because the # time is needed for stamping each post. However, the actual # query above must use last_seen to get the correct plant records. post_datetime = datetime.combine(plant_record.last_seen, plant_record.last_updated.time()) time_zone_offset = "-00:00" # unknown local time zone offset plant["post_datetime"] = "".join([post_datetime.isoformat(), time_zone_offset]) # Get the Taxon record for this Plant of the Day and collect # information to be used by the template. try: taxon = Taxon.objects.get(scientific_name=plant_record.scientific_name) plant["common_names"] = taxon.common_names.all() plant["facts"] = taxon.factoid plant["url"] = reverse("taxa-species", args=[taxon.genus.slug, taxon.epithet]) image = None species_images = botany.species_images(taxon) if len(species_images) > 0: image = species_images[0] plant["image"] = image plants.append(plant) except ObjectDoesNotExist: pass return plants
def read(self, request): kwargs = {} for k, v in request.GET.items(): kwargs[str(k)] = v try: images = botany.species_images(**kwargs) except models.Taxon.DoesNotExist: return rc.NOT_FOUND return [_taxon_image(image) for image in images]
def home_view(request): """View for the home page of the Go Botany site.""" try: intro = FlatPage.objects.get(url='/home/').content except ObjectDoesNotExist: intro = None # Get or generate today's Plant of the Day, if appropriate. partner = which_partner(request) plant_of_the_day = PlantOfTheDay.get_by_date.for_day( date.today(), partner.short_name) plant_of_the_day_taxon = None if plant_of_the_day: # Get the Taxon record of the Plant of the Day. try: plant_of_the_day_taxon = GoOrchidTaxon.objects.get( scientific_name=plant_of_the_day.scientific_name) except ObjectDoesNotExist: pass plant_of_the_day_image = None if hasattr(plant_of_the_day_taxon, 'taxon_ptr'): species_images = botany.species_images( plant_of_the_day_taxon.taxon_ptr, image_types='flowers,inflorescences') if len(species_images) == 0: species_images = botany.species_images( plant_of_the_day_taxon, image_types='flowers,inflorescences') if species_images: plant_of_the_day_image = species_images[0] return render_to_response('gobotany/home.html', { 'intro': intro, 'plant_of_the_day': plant_of_the_day_taxon, 'plant_of_the_day_image': plant_of_the_day_image, }, context_instance=RequestContext(request))
def taxon_image(request): kwargs = {} items = request.GET.items() if items: for k, v in items: kwargs[str(k)] = v try: images = botany.species_images(**kwargs) except models.Taxon.DoesNotExist: return rc.NOT_FOUND else: return rc.BAD_REQUEST return jsonify([_taxon_image(image) for image in images])
def _simple_taxon(taxon): res = {} first_common_name = ''; common_names = taxon.common_names.all() if (common_names): first_common_name = common_names[0].common_name res['scientific_name'] = taxon.scientific_name res['common_name'] = first_common_name res['genus'] = taxon.scientific_name.split()[0] # faster than .genus.name res['family'] = taxon.family.name res['id'] = taxon.id res['taxonomic_authority'] = taxon.taxonomic_authority res['default_image'] = _taxon_image(taxon.get_default_image()) res['images'] = [_taxon_image(i) for i in botany.species_images(taxon)] res['factoid'] = taxon.factoid return res
def family_view(request, family_slug): family_name = family_slug.capitalize() family = get_object_or_404(Family, name=family_name) # If it is decided that common names will not be required, change the # default below to None so the template will omit the name if missing. DEFAULT_COMMON_NAME = 'common name here' common_name = family.common_name or DEFAULT_COMMON_NAME family_drawings = (family.images.filter( image_type__name='example drawing')) if not family_drawings: # No example drawings for this family were specified. # Use 2 images from the family's species instead. species = family.taxa.all() family_drawings = [] for s in species: species_images = botany.species_images( s, image_types='flowers,inflorescences') if len(species_images): family_drawings.append(species_images[0]) if len(family_drawings) == 2: break family_drawings = _images_with_copyright_holders(family_drawings) pile = family.taxa.all()[0].piles.all()[0] pilegroup = pile.pilegroup return render_to_response('gobotany/family.html', { 'family': family, 'common_name': common_name, 'family_drawings': family_drawings, 'pilegroup': pilegroup, 'pile': pile, }, context_instance=RequestContext(request))
def species_view(request, genus_slug, epithet): COMPACT_MULTIVALUE_CHARACTERS = [ 'Habitat', 'New England state', 'Specific Habitat' ] genus_name = genus_slug.capitalize() scientific_name = '%s %s' % (genus_name, epithet) taxon = get_object_or_404(GoOrchidTaxon, scientific_name=scientific_name) scientific_name_short = '%s. %s' % (scientific_name[0], epithet) pile_slug = request.GET.get('pile') if pile_slug: pile = get_object_or_404(Pile, slug=pile_slug) else: # Randomly grab the first pile from the species pile = taxon.piles.order_by('id')[0] pilegroup = pile.pilegroup partner = which_partner(request) partner_species = None if partner: rows = PartnerSpecies.objects.filter(species=taxon, partner=partner).all() if rows: partner_species = rows[0] species_in_simple_key = (partner_species and partner_species.simple_key) key = request.GET.get('key') if not key: if species_in_simple_key: key = 'simple' else: key = 'full' if hasattr(taxon, 'taxon_ptr'): species_images = botany.species_images(taxon.taxon_ptr) if len(species_images) == 0: species_images = botany.species_images(taxon) images = _images_with_copyright_holders(species_images) # Get the set of preview characteristics. plant_preview_characters = { ppc.character_id: ppc.order for ppc in PlantPreviewCharacter.objects.filter(pile=pile, partner_site=partner) } # Select ALL character values for this taxon. character_values = list( taxon.character_values.select_related('character', 'character__character_group')) # Throw away values for characters that are not part of this pile. pile_ids = (None, pile.id) # characters like 'habitat' have pile_id None character_values = [ v for v in character_values if v.character.pile_id in pile_ids ] # Create a tree of character groups, characters, and values. get_group_name = lambda v: v.character.character_group.name get_character_name = lambda v: v.character.friendly_name character_values.sort(key=get_character_name) character_values.sort(key=get_group_name) all_characteristics = [] for group_name, seq1 in groupby(character_values, get_group_name): characters = [] for character_name, seq2 in groupby(seq1, get_character_name): seq2 = list(seq2) character = seq2[0].character # arbitrary; all look the same characters.append({ 'group': character.character_group.name, 'name': character.friendly_name, 'values': sorted((_format_character_value(v) for v in seq2), cmp=_compare_character_values), 'in_preview': character.id in plant_preview_characters, 'preview_order': plant_preview_characters.get(character.id, -1), }) all_characteristics.append({ 'name': group_name, 'characters': characters }) # Pick out the few preview characters for separate display. preview_characters = sorted( (character for group in all_characteristics for character in group['characters'] if character['in_preview']), key=itemgetter('preview_order')) native_to_north_america = _native_to_north_america_status(taxon) return render_to_response( 'gobotany/species.html', { 'pilegroup': pilegroup, 'pile': pile, 'scientific_name': scientific_name, 'scientific_name_short': scientific_name_short, 'taxon': taxon, 'key': key, 'species_in_simple_key': species_in_simple_key, 'common_names': taxon.common_names.all(), # view uses this 3 times 'images': images, 'partner_heading': partner_species.species_page_heading if partner_species else None, 'partner_blurb': partner_species.species_page_blurb if partner_species else None, 'ready_for_display': taxon.ready_for_display, 'compact_multivalue_characters': COMPACT_MULTIVALUE_CHARACTERS, 'brief_characteristics': preview_characters, 'all_characteristics': all_characteristics, 'epithet': epithet, 'native_to_north_america': native_to_north_america }, context_instance=RequestContext(request))
def species_view(request, genus_slug, epithet): COMPACT_MULTIVALUE_CHARACTERS = ['Habitat', 'New England state', 'Specific Habitat'] genus_name = genus_slug.capitalize() scientific_name = '%s %s' % (genus_name, epithet) taxon = get_object_or_404(GoOrchidTaxon, scientific_name=scientific_name) scientific_name_short = '%s. %s' % (scientific_name[0], epithet) pile_slug = request.GET.get('pile') if pile_slug: pile = get_object_or_404(Pile, slug=pile_slug) else: # Randomly grab the first pile from the species pile = taxon.piles.order_by('id')[0] pilegroup = pile.pilegroup partner = which_partner(request) partner_species = None if partner: rows = PartnerSpecies.objects.filter( species=taxon, partner=partner).all() if rows: partner_species = rows[0] species_in_simple_key = (partner_species and partner_species.simple_key) key = request.GET.get('key') if not key: if species_in_simple_key: key = 'simple' else: key = 'full' if hasattr(taxon, 'taxon_ptr'): species_images = botany.species_images(taxon.taxon_ptr) if len(species_images) == 0: species_images = botany.species_images(taxon) images = _images_with_copyright_holders(species_images) # Get the set of preview characteristics. plant_preview_characters = { ppc.character_id: ppc.order for ppc in PlantPreviewCharacter.objects.filter(pile=pile, partner_site=partner) } # Select ALL character values for this taxon. character_values = list(taxon.character_values.select_related( 'character', 'character__character_group')) # Throw away values for characters that are not part of this pile. pile_ids = (None, pile.id) # characters like 'habitat' have pile_id None character_values = [v for v in character_values if v.character.pile_id in pile_ids] # Create a tree of character groups, characters, and values. get_group_name = lambda v: v.character.character_group.name get_character_name = lambda v: v.character.friendly_name character_values.sort(key=get_character_name) character_values.sort(key=get_group_name) all_characteristics = [] for group_name, seq1 in groupby(character_values, get_group_name): characters = [] for character_name, seq2 in groupby(seq1, get_character_name): seq2 = list(seq2) character = seq2[0].character # arbitrary; all look the same characters.append({ 'group': character.character_group.name, 'name': character.friendly_name, 'values': sorted((_format_character_value(v) for v in seq2), cmp=_compare_character_values), 'in_preview': character.id in plant_preview_characters, 'preview_order': plant_preview_characters.get(character.id, -1), }) all_characteristics.append({ 'name': group_name, 'characters': characters }) # Pick out the few preview characters for separate display. preview_characters = sorted(( character for group in all_characteristics for character in group['characters'] if character['in_preview'] ), key=itemgetter('preview_order')) native_to_north_america = _native_to_north_america_status(taxon) return render_to_response('gobotany/species.html', { 'pilegroup': pilegroup, 'pile': pile, 'scientific_name': scientific_name, 'scientific_name_short': scientific_name_short, 'taxon': taxon, 'key': key, 'species_in_simple_key': species_in_simple_key, 'common_names': taxon.common_names.all(), # view uses this 3 times 'images': images, 'partner_heading': partner_species.species_page_heading if partner_species else None, 'partner_blurb': partner_species.species_page_blurb if partner_species else None, 'ready_for_display': taxon.ready_for_display, 'compact_multivalue_characters': COMPACT_MULTIVALUE_CHARACTERS, 'brief_characteristics': preview_characters, 'all_characteristics': all_characteristics, 'epithet': epithet, 'native_to_north_america': native_to_north_america }, context_instance=RequestContext(request))
def species_view(request, genus_slug, epithet): COMPACT_MULTIVALUE_CHARACTERS = ['Habitat', 'New England state', 'Specific Habitat'] genus_name = genus_slug.capitalize() scientific_name = '%s %s' % (genus_name, epithet) taxon = get_object_or_404(Taxon, scientific_name=scientific_name) scientific_name_short = '%s. %s' % (scientific_name[0], epithet) pile_slug = request.GET.get('pile') if pile_slug: pile = get_object_or_404(Pile, slug=pile_slug) else: # Randomly grab the first pile from the species pile = taxon.piles.order_by('id')[0] pilegroup = pile.pilegroup partner = which_partner(request) partner_species = None if partner: rows = PartnerSpecies.objects.filter( species=taxon, partner=partner).all() if rows: partner_species = rows[0] dkey_pages = dkey_models.Page.objects.filter(title=scientific_name) dkey_page = dkey_pages[0] if dkey_pages else None dkey_hybrids = (dkey_models.Hybrid.objects .filter(Q(scientific_name1=scientific_name) | Q(scientific_name2=scientific_name)) .order_by('number1', 'number2')) species_in_simple_key = (partner_species and partner_species.simple_key) key = request.GET.get('key') if not key: if species_in_simple_key: key = 'simple' else: key = 'full' species_images = botany.species_images(taxon) images = _images_with_copyright_holders(species_images) # Get the set of preview characteristics. plant_preview_characters = { ppc.character_id: ppc.order for ppc in PlantPreviewCharacter.objects.filter(pile=pile, partner_site=partner) } # Select ALL character values for this taxon. character_values = list(taxon.character_values.select_related( 'character', 'character__character_group')) # Throw away values for characters that are not part of this pile. pile_ids = (None, pile.id) # characters like 'habitat' have pile_id None # TODO: eventually remove this temporary code after the Remaining # Non-Monocots pile character value data is split and assigned. if pile.name.find('Remaining Non-Monocots') > -1: try: # Include the big pile which has the character value data. big_pile = Pile.objects.get(name='Remaining Non-Monocots') pile_ids = pile_ids + (big_pile.id,) except models.Pile.DoesNotExist: pass # (end of temporary code) character_values = [ v for v in character_values if v.character.pile_id in pile_ids ] # Create a tree of character groups, characters, and values. get_group_name = lambda v: v.character.character_group.name get_character_name = lambda v: v.character.friendly_name character_values.sort(key=get_character_name) character_values.sort(key=get_group_name) all_characteristics = [] for group_name, seq1 in groupby(character_values, get_group_name): characters = [] for character_name, seq2 in groupby(seq1, get_character_name): seq2 = list(seq2) character = seq2[0].character # arbitrary; all look the same characters.append({ 'group': character.character_group.name, 'name': character.friendly_name, 'values': sorted(_format_character_value(v) for v in seq2), 'in_preview': character.id in plant_preview_characters, 'preview_order': plant_preview_characters.get(character.id, -1), }) all_characteristics.append({ 'name': group_name, 'characters': characters }) # Pick out the few preview characters for separate display. preview_characters = sorted(( character for group in all_characteristics for character in group['characters'] if character['in_preview'] ), key=itemgetter('preview_order')) native_to_north_america = _native_to_north_america_status(taxon) return render_to_response('gobotany/species.html', { 'pilegroup': pilegroup, 'pile': pile, 'scientific_name': scientific_name, 'scientific_name_short': scientific_name_short, 'taxon': taxon, 'key': key, 'species_in_simple_key': species_in_simple_key, 'common_names': taxon.common_names.all(), # view uses this 3 times 'conservation_status_rows': ( 'endangered', 'threatened', 'special concern', 'historic', 'rare', 'extirpated', 'invasive', 'prohibited', ), 'dkey_hybrids': dkey_hybrids, 'dkey_page': dkey_page, 'images': images, 'partner_heading': partner_species.species_page_heading if partner_species else None, 'partner_blurb': partner_species.species_page_blurb if partner_species else None, 'compact_multivalue_characters': COMPACT_MULTIVALUE_CHARACTERS, 'brief_characteristics': preview_characters, 'all_characteristics': all_characteristics, 'epithet': epithet, 'native_to_north_america': native_to_north_america }, context_instance=RequestContext(request))
def _get_plants_of_the_day(max_number_plants, partner_name): """Get recent plants of the day, generating some if needed.""" # Generate today's Plant of the Day if it hasn't been already. # If there is a gap of any recent days where no Plant of the Day # record exists yet, generate records for those days too. for i in range(max_number_plants): day = date.today() - timedelta(days=i) plant_of_the_day = PlantOfTheDay.objects.filter( last_seen=day, partner_short_name=partner_name, include=True) if len(plant_of_the_day) > 0: # Found the latest recent Plant of the Day record within # the date range used for the feed, so no more records # need to be generated. break else: # No Plant of the Day exists for that day, so generate one. plant_of_the_day = PlantOfTheDay.get_by_date.for_day( day, partner_name) # Now that any new records that were needed have been generated, plants = [] plant_records = (PlantOfTheDay.objects.filter( include=True, last_seen__isnull=False, partner_short_name=partner_name) .order_by('-last_seen'))[:max_number_plants] for plant_record in plant_records: plant = {} plant['scientific_name'] = plant_record.scientific_name # Use the last_updated field because it is a date/time field, # rather than last_seen which is just a date field, because the # time is needed for stamping each post. However, the actual # query above must use last_seen to get the correct plant records. post_datetime = datetime.combine(plant_record.last_seen, plant_record.last_updated.time()) time_zone_offset = '-00:00' # unknown local time zone offset plant['post_datetime'] = ''.join([post_datetime.isoformat(), time_zone_offset]) # Get the Taxon record for this Plant of the Day and collect # information to be used by the template. try: taxon = Taxon.objects.get( scientific_name=plant_record.scientific_name) plant['common_names'] = taxon.common_names.all() plant['facts'] = taxon.factoid plant['url'] = reverse('taxa-species', args=[taxon.genus.slug, taxon.epithet]) image = None species_images = botany.species_images(taxon) if len(species_images) > 0: image = species_images[0] plant['image'] = image plants.append(plant) except ObjectDoesNotExist: pass return plants
def species_view(request, genus_slug, epithet): COMPACT_MULTIVALUE_CHARACTERS = [ 'Habitat', 'New England state', 'Specific Habitat' ] genus_name = genus_slug.capitalize() scientific_name = '%s %s' % (genus_name, epithet) taxon = get_object_or_404(Taxon, scientific_name=scientific_name) scientific_name_short = '%s. %s' % (scientific_name[0], epithet) pile_slug = request.GET.get('pile') if pile_slug: pile = get_object_or_404(Pile, slug=pile_slug) else: # Randomly grab the first pile from the species pile = taxon.piles.order_by('id')[0] pilegroup = pile.pilegroup partner = which_partner(request) partner_species = None if partner: rows = PartnerSpecies.objects.filter(species=taxon, partner=partner).all() if rows: partner_species = rows[0] dkey_pages = dkey_models.Page.objects.filter(title=scientific_name) dkey_page = dkey_pages[0] if dkey_pages else None dkey_hybrids = (dkey_models.Hybrid.objects.filter( Q(scientific_name1=scientific_name) | Q(scientific_name2=scientific_name)).order_by('number1', 'number2')) species_in_simple_key = (partner_species and partner_species.simple_key) key = request.GET.get('key') if not key: if species_in_simple_key: key = 'simple' else: key = 'full' species_images = botany.species_images(taxon) images = _images_with_copyright_holders(species_images) # Get the set of preview characteristics. plant_preview_characters = { ppc.character_id: ppc.order for ppc in PlantPreviewCharacter.objects.filter(pile=pile, partner_site=partner) } # If no preview characteristics are defined for a partner, use the # ones defined for Go Botany. if len(plant_preview_characters) == 0: gobotany = PartnerSite.objects.get(short_name='gobotany') plant_preview_characters = { ppc.character_id: ppc.order for ppc in PlantPreviewCharacter.objects.filter( pile=pile, partner_site=gobotany) } # Select ALL character values for this taxon. character_values = list( taxon.character_values.select_related('character', 'character__character_group')) # Throw away values for characters that are not part of this pile. pile_ids = (None, pile.id) # characters like 'habitat' have pile_id None # TODO: eventually remove this temporary code after the Remaining # Non-Monocots pile character value data is split and assigned. if pile.name.find('Remaining Non-Monocots') > -1: try: # Include the big pile which has the character value data. big_pile = Pile.objects.get(name='Remaining Non-Monocots') pile_ids = pile_ids + (big_pile.id, ) except models.Pile.DoesNotExist: pass # (end of temporary code) character_values = [ v for v in character_values if v.character.pile_id in pile_ids ] # Create a tree of character groups, characters, and values. get_group_name = lambda v: v.character.character_group.name get_character_name = lambda v: v.character.friendly_name character_values.sort(key=get_character_name) character_values.sort(key=get_group_name) all_characteristics = [] for group_name, seq1 in groupby(character_values, get_group_name): characters = [] for character_name, seq2 in groupby(seq1, get_character_name): seq2 = list(seq2) character = seq2[0].character # arbitrary; all look the same characters.append({ 'group': character.character_group.name, 'name': character.friendly_name, 'values': sorted(_format_character_value(v) for v in seq2), 'in_preview': character.id in plant_preview_characters, 'preview_order': plant_preview_characters.get(character.id, -1), }) all_characteristics.append({ 'name': group_name, 'characters': characters }) # Pick out the few preview characters for separate display. preview_characters = sorted( (character for group in all_characteristics for character in group['characters'] if character['in_preview']), key=itemgetter('preview_order')) native_to_north_america = _native_to_north_america_status(taxon) statuses = taxon.conservation_statuses.values_list( 'variety_subspecies_hybrid', 'region', 's_rank', 'endangerment_code') statuses_state_names = [{ 'variety_subspecies_hybrid': variety_subspecies_hybrid, 'state': settings.STATE_NAMES[region.lower()], 's_rank': s_rank, 'endangerment_code': endangerment_code } for (variety_subspecies_hybrid, region, s_rank, endangerment_code) in statuses] conservation_statuses = sorted( statuses_state_names, key=lambda k: (k['variety_subspecies_hybrid'], k['state'])) return render_per_partner( 'species.html', { 'pilegroup': pilegroup, 'pile': pile, 'scientific_name': scientific_name, 'scientific_name_short': scientific_name_short, 'taxon': taxon, 'key': key, 'species_in_simple_key': species_in_simple_key, 'common_names': taxon.common_names.all(), # view uses this 3 times 'dkey_hybrids': dkey_hybrids, 'dkey_page': dkey_page, 'images': images, 'partner_heading': partner_species.species_page_heading if partner_species else None, 'partner_blurb': partner_species.species_page_blurb if partner_species else None, 'compact_multivalue_characters': COMPACT_MULTIVALUE_CHARACTERS, 'brief_characteristics': preview_characters, 'all_characteristics': all_characteristics, 'epithet': epithet, 'native_to_north_america': native_to_north_america, 'conservation_statuses': conservation_statuses, 'prior_signup_detected': prior_signup_detected(request), }, request)