Skip to content

Commit

Permalink
Update edit form, fix taxon group experts (#4196)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput authored Aug 28, 2024
1 parent 0a814ec commit 727c046
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 31 deletions.
8 changes: 7 additions & 1 deletion bims/api_views/taxon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rest_framework.pagination import PageNumberPagination
from taggit.models import Tag

from bims.api_views.taxon_update import is_expert
from bims.models.taxonomy import Taxonomy, TaxonTag, CustomTaggedTaxonomy
from bims.serializers.taxon_detail_serializer import TaxonDetailSerializer
from bims.serializers.taxon_serializer import TaxonSerializer
Expand Down Expand Up @@ -655,12 +656,17 @@ def get(self, request, *args):
page = self.paginate_queryset(taxon_list)
validated = ast.literal_eval(request.GET.get('validated', 'True'))
if page is not None:
taxon_group_id = request.GET.get('taxonGroup', None)
serializer = self.get_paginated_response(
TaxonSerializer(page, many=True, context={
'taxon_group_id': request.GET.get('taxonGroup', None),
'taxon_group_id': taxon_group_id,
'user': request.user.id,
'validated': validated
}).data)
serializer.data['is_expert'] = is_expert(
self.request.user,
TaxonGroup.objects.get(id=taxon_group_id)
) if taxon_group_id else False
else:
serializer = TaxonSerializer(
taxon_list,
Expand Down
17 changes: 14 additions & 3 deletions bims/api_views/taxon_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ def create_taxon_proposal(
'Taxonomic Comments',
'Conservation Comments',
'Biogeographic Comments',
'Environmental Comments'
'Environmental Comments',
'Taxonomic References',
'Conservation References',
'Biogeographic References',
'Environmental References'
]
for additional_key in additional_data_to_check:
if data.get(additional_key):
additional_data[additional_key] = data.get(additional_key)

canonical_name = data.get('canonical_name', taxon.canonical_name)
if taxon.is_species:
if taxon.genus_name not in canonical_name:
canonical_name = taxon.genus_name + ' ' + canonical_name

proposal, created = TaxonomyUpdateProposal.objects.get_or_create(
original_taxonomy=taxon,
taxon_group=taxon_group,
Expand All @@ -57,15 +66,15 @@ def create_taxon_proposal(
'author': data.get('author', taxon.author),
'rank': data.get('rank', taxon.rank),
'scientific_name': data.get('scientific_name', taxon.scientific_name),
'canonical_name': data.get('canonical_name', taxon.canonical_name),
'canonical_name': canonical_name,
'origin': data.get('origin', taxon.origin),
'iucn_status': iucn_status,
'endemism': endemism,
'taxon_group_under_review': taxon_group,
'taxonomic_status': taxonomic_status,
'accepted_taxonomy': accepted_taxonomy,
'parent': data.get('parent', taxon.parent),
'hierarchical_data': taxon.hierarchical_data,
'hierarchical_data': {},
'gbif_data': taxon.gbif_data,
'collector_user': creator,
'additional_data': additional_data
Expand Down Expand Up @@ -114,6 +123,8 @@ def update_taxon_proposal(
def is_expert(user, taxon_group):
if user.is_superuser:
return True
if not taxon_group:
return False
return taxon_group.experts.filter(
id=user.id
).exists()
Expand Down
16 changes: 14 additions & 2 deletions bims/models/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ def sub_tribe_name(self):
def variety_name(self):
self.get_taxon_rank_name(TaxonomicRank.VARIETY.name)

@property
def taxon_name(self):
if self.is_species and self.genus_name:
return self.canonical_name.split(self.genus_name)[-1].strip()
return self.canonical_name

@property
def is_species(self):
return (
Expand Down Expand Up @@ -483,18 +489,24 @@ def save(self, *args, **kwargs):
if self.additional_data and 'fetch_gbif' in self.additional_data:
update_taxon_with_gbif = True
del self.additional_data['fetch_gbif']
species_name = ''
if not self.hierarchical_data or 'species_name' not in self.hierarchical_data:
species_name = self.species_name
genus_name = self.genus_name
if genus_name in species_name and genus_name:
species_name = species_name.split(genus_name)[-1].strip()
if not self.hierarchical_data:
self.hierarchical_data = {
'family_name': self.get_taxon_rank_name(TaxonomicRank.FAMILY.name),
'genus_name': self.get_taxon_rank_name(TaxonomicRank.GENUS.name),
'species_name': self.get_taxon_rank_name(TaxonomicRank.SPECIES.name),
'species_name': species_name
}
elif 'family_name' not in self.hierarchical_data:
self.hierarchical_data['family_name'] = self.get_taxon_rank_name(TaxonomicRank.FAMILY.name)
elif 'genus_name' not in self.hierarchical_data:
self.hierarchical_data['genus_name'] = self.get_taxon_rank_name(TaxonomicRank.GENUS.name)
elif 'species_name' not in self.hierarchical_data:
self.hierarchical_data['species_name'] = self.get_taxon_rank_name(TaxonomicRank.SPECIES.name)
self.hierarchical_data['species_name'] = species_name

super(Taxonomy, self).save(*args, **kwargs)

Expand Down
1 change: 1 addition & 0 deletions bims/models/taxonomy_update_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def approve(self, reviewer: settings.AUTH_USER_MODEL):
setattr(
self.original_taxonomy,
field, getattr(self, field))
self.original_taxonomy.hierarchical_data = {}
self.original_taxonomy.save()
self.status = 'approved'
self.save()
Expand Down
7 changes: 6 additions & 1 deletion bims/static/js/taxa_management/taxa_management.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ export const taxaManagement = (() => {
success: function (response) {
loading.hide();
taxaData = response.results;
$('#add-taxon-btn').hide();

if (response['is_expert']) {
$('#add-taxon-btn').show();
}

$.each(response.results, function (index, data) {
let name = data.canonical_name || data.scientific_name;
let taxonomicStatusHTML = (data.taxonomic_status && data.taxonomic_status.toLowerCase() === 'synonym') ?
Expand All @@ -366,7 +372,6 @@ export const taxaManagement = (() => {
let validatedHTML = !data.validated ? '<span class="badge badge-secondary">Unvalidated</span>' : '';

data.nameHTML = name + '<br/>' + gbifHTML + iucnHTML + validatedHTML + `<input type="hidden" class="proposal-id" value="${data.proposal_id}" />`;

if (data['can_edit']) {
let $rowAction = $('.row-action').clone(true, true).removeClass('row-action');
if (!data['validated']) {
Expand Down
57 changes: 53 additions & 4 deletions bims/templates/edit_taxon.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ <h2>
<div class="col-sm-10">
<input type="text" name="canonical_name" id="canonical_name"
class="form-control form-control-sm"
placeholder="" value="{{ object.canonical_name }}" >
placeholder=""
value="{{ object.taxon_name }}" >
</div>
</div>

Expand Down Expand Up @@ -218,13 +219,13 @@ <h2>

<div class="form-group row">
<label for="rank" class="col-sm-2 col-form-label col-form-label">
Conservation Comments
Taxonomic References
</label>
<div class="col-sm-10">
<input type="text" name="additional_data__Conservation_Comments" id="additional_data__Conservation_Comments"
<input type="text" name="additional_data__Taxonomic_References" id="additional_data__Taxonomic_References"
class="form-control form-control-sm"
placeholder=""
value="{% with object.additional_data as data %}{% if data|value_by_key:'Conservation Comments' %}{{ data|value_by_key:'Conservation Comments' }}{% elif data|value_by_key:'Conservation comments' %}{{ data|value_by_key:'Conservation comments' }}{% else %}{% endif %}{% endwith %}" >
value="{% with object.additional_data as data %}{% if data|value_by_key:'Taxonomic References' %}{{ data|value_by_key:'Taxonomic References' }}{% endif %}{% endwith %}" >
</div>
</div>

Expand All @@ -239,6 +240,18 @@ <h2>
</div>
</div>

<div class="form-group row">
<label for="rank" class="col-sm-2 col-form-label col-form-label">
Biogeographic References
</label>
<div class="col-sm-10">
<input type="text" name="additional_data__Biogeographic_References" id="additional_data__Biogeographic_References"
class="form-control form-control-sm"
placeholder=""
value="{% with object.additional_data as data %}{% if data|value_by_key:'Biogeographic References' %}{{ data|value_by_key:'Biogeographic References' }}{% endif %}{% endwith %}" >
</div>
</div>

<div class="form-group row">
<label for="rank" class="col-sm-2 col-form-label col-form-label">
Environmental Comments
Expand All @@ -250,6 +263,42 @@ <h2>
</div>
</div>

<div class="form-group row">
<label for="rank" class="col-sm-2 col-form-label col-form-label">
Environmental References
</label>
<div class="col-sm-10">
<input type="text" name="additional_data__Environmental_References" id="additional_data__Environmental_References"
class="form-control form-control-sm"
placeholder=""
value="{% with object.additional_data as data %}{% if data|value_by_key:'Environmental References' %}{{ data|value_by_key:'Environmental References' }}{% endif %}{% endwith %}" >
</div>
</div>

<div class="form-group row">
<label for="rank" class="col-sm-2 col-form-label col-form-label">
Conservation Comments
</label>
<div class="col-sm-10">
<input type="text" name="additional_data__Conservation_Comments" id="additional_data__Conservation_Comments"
class="form-control form-control-sm"
placeholder=""
value="{% with object.additional_data as data %}{% if data|value_by_key:'Conservation Comments' %}{{ data|value_by_key:'Conservation Comments' }}{% elif data|value_by_key:'Conservation comments' %}{{ data|value_by_key:'Conservation comments' }}{% else %}{% endif %}{% endwith %}" >
</div>
</div>

<div class="form-group row">
<label for="rank" class="col-sm-2 col-form-label col-form-label">
Conservation References
</label>
<div class="col-sm-10">
<input type="text" name="additional_data__Conservation_References" id="additional_data__Conservation_References"
class="form-control form-control-sm"
placeholder=""
value="{% with object.additional_data as data %}{% if data|value_by_key:'Conservation References' %}{{ data|value_by_key:'Conservation References' }}{% endif %}{% endwith %}" >
</div>
</div>

<div class="form-group submit-holder">
{% if update %}
{% if allow_to_edit %}
Expand Down
21 changes: 3 additions & 18 deletions bims/templates/taxa_management.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ <h2>Taxon Management</h2>
</div>
<!-- Download button -->
<div class="float-right download-button-container"
style="margin-top: -47px; display: none">
{% if perms.bims.add_taxonomy or is_expert %}
style="margin-top: -47px;">
<button id="add-taxon-btn"
class="btn btn-outline-success" data-toggle="modal"
style="display: none"
data-target="#addNewTaxonModal">Add a taxon
</button>
<div class="btn-group">
Expand All @@ -289,7 +289,6 @@ <h2>Taxon Management</h2>
<a class="dropdown-item disabled" href="#" id="download-pdf">PDF Report</a>
</div>
</div>
{% endif %}
</div>
<div class="taxa-table">
<table id="taxaTable" class="table stripe">
Expand All @@ -306,18 +305,9 @@ <h2>Taxon Management</h2>
<th scope="col">Biographic distributions</th>
<th scope="col">Accepted Taxon</th>
<th scope="col">Rank</th>
{# <th scope="col">Origin#}
{# </th>#}
{# <th scope="col">Endemism#}
{# </th>#}
{# <th scope="col">Records#}
{# </th>#}

<th scope="col">
{% if fada_site %}Environmental Information{% else %}Tags{% endif %}</th>
{% if perms.bims.change_taxonomy or is_expert %}
<th scope="col"></th>
{% endif %}
<th scope="col"></th>
</tr>
</thead>
<tbody id="taxa-list"></tbody>
Expand All @@ -340,7 +330,6 @@ <h2>Taxon Management</h2>
</div>
</div>

{% if perms.bims.change_taxonomy or is_expert %}
{% with preferences.SiteSetting.project_name as project_name %}
<div class="row-action" style="display: none">
<div class="btn-validated-container">
Expand All @@ -360,9 +349,6 @@ <h2>Taxon Management</h2>
<i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit in Admin
</a>
{% endif %}
{# <button class="dropdown-item add-tag" title="Manage tags">#}
{# <i class="fa fa-tags" aria-hidden="true"></i> Manage tags#}
{# </button>#}
</div>
</div>
</div>
Expand All @@ -386,7 +372,6 @@ <h2>Taxon Management</h2>
</div>
</div>
{% endwith %}
{% endif %}

</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions bims/templatetags/jsonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def get_html_for_radio_group_headings(column_count):

@register.filter
def value_by_key(d, key):
if not d:
return ''
if key in d:
return d[key]
return ''
Expand Down
12 changes: 10 additions & 2 deletions bims/views/edit_taxon_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ def form_valid(self, form):

data['Taxonomic Comments'] = (
self.request.POST.get('additional_data__Taxonomic_Comments', '')).strip()
data['Conservation Comments'] = (
self.request.POST.get('additional_data__Conservation_Comments', '')).strip()
data['Taxonomic References'] = (
self.request.POST.get('additional_data__Taxonomic_References', '')).strip()
data['Biogeographic Comments'] = (
self.request.POST.get('additional_data__Biogeographic_Comments', '')).strip()
data['Biogeographic References'] = (
self.request.POST.get('additional_data__Biogeographic_References', '')).strip()
data['Environmental Comments'] = (
self.request.POST.get('additional_data__Environmental_Comments', '')).strip()
data['Environmental References'] = (
self.request.POST.get('additional_data__Environmental_References', '')).strip()
data['Conservation Comments'] = (
self.request.POST.get('additional_data__Conservation_Comments', '')).strip()
data['Conservation References'] = (
self.request.POST.get('additional_data__Conservation_References', '')).strip()

new_proposal = False

Expand Down

0 comments on commit 727c046

Please sign in to comment.