Beispiel #1
0
def taxonomy_pre_save_handler(sender, instance, **kwargs):
    """Get iucn status before save."""
    if instance.rank == TaxonomicRank.CLASS.name:
        generate_permission(instance.scientific_name)

    if instance.is_species and not instance.iucn_status:
        iucn_status = get_iucn_status(species_name=instance.scientific_name)
        if iucn_status:
            instance.iucn_status = iucn_status
Beispiel #2
0
def taxon_pre_save_handler(sender, instance, **kwargs):
    """Get iucn status before save."""
    if instance.taxon_class:
        generate_permission(instance.taxon_class)

    if instance.common_name and not instance.iucn_status:
        iucn_status = get_iucn_status(species_name=instance.common_name)
        if iucn_status:
            instance.iucn_status = iucn_status
Beispiel #3
0
    def handle(self, *args, **options):
        # Get collection without taxa record
        taxon_id = options.get('taxon_id')
        if taxon_id:
            taxa = Taxonomy.objects.filter(pk=taxon_id)
        else:
            taxa = Taxonomy.objects.all()

        models.signals.post_save.disconnect(
            collection_post_save_update_cluster, )
        models.signals.post_save.disconnect(collection_post_save_handler, )
        for taxon in taxa:
            print('Update taxon : %s' % taxon.common_name)
            if taxon.species:
                iucn_status = get_iucn_status(species_name=taxon.species,
                                              only_returns_json=True)
            else:
                iucn_status = get_iucn_status(species_name=taxon.common_name,
                                              only_returns_json=True)
            if not iucn_status:
                # let's try fetch with gbif id
                iucn_status = get_iucn_status(taxon_id=taxon.gbif_id,
                                              only_returns_json=True)

            if 'result' in iucn_status:
                iucn, status = IUCNStatus.objects.get_or_create(
                    category=iucn_status['result'][0]['category'])
                taxon.iucn_status = iucn
                taxon.iucn_redlist_id = iucn_status['result'][0]['taxonid']
                taxon.iucn_data = json.dumps(iucn_status['result'][0],
                                             indent=4)
                taxon.save()

            print(iucn_status)

        models.signals.post_save.connect(collection_post_save_update_cluster, )
        models.signals.post_save.connect(collection_post_save_handler, )