Skip to content

Commit

Permalink
Show tags in edit taxon form (#4058)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput committed Jul 11, 2024
1 parent 34f2ccc commit 1460b3b
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 48 deletions.
12 changes: 7 additions & 5 deletions bims/scripts/occurrences_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,14 @@ def process_data(self, row):
optional_data['abundance_type'] = abundance_type

# -- Record type
record_type = DataCSVUpload.row_value(row, RECORD_TYPE)
if record_type:
record_type_str = DataCSVUpload.row_value(row, RECORD_TYPE)
if record_type_str:
record_type = RecordType.objects.filter(
name__iexact=record_type
name__iexact=record_type_str
).first()
if not record_type:
record_type = RecordType.objects.create(
name=record_type
name=record_type_str
)
else:
record_type = None
Expand Down Expand Up @@ -872,7 +872,9 @@ def process_data(self, row):
'taxonomy': taxonomy,
'collector_user': collector,
'validated': True,
'accuracy_of_identification': certainty_of_identification,
'accuracy_of_identification': (
certainty_of_identification if certainty_of_identification else 100
),
'date_accuracy': date_accuracy.lower() if date_accuracy else '',
'data_type': data_type,
'identified_by': identified_by if identified_by else ''
Expand Down
54 changes: 30 additions & 24 deletions bims/signals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
SourceReference, SourceReferenceDatabase, SourceReferenceBibliography,
SourceReferenceDocument
)
from easyaudit.signals.model_signals import (
post_delete,
post_save,
pre_save,
m2m_changed
)
from django.conf import settings
try:
from easyaudit.signals.model_signals import (
post_delete,
post_save,
pre_save,
m2m_changed
)
except RuntimeError as e:
pass


def disconnect_bims_signals():
Expand Down Expand Up @@ -64,15 +68,16 @@ def disconnect_bims_signals():
location_context_post_save_handler,
sender=LocationContextFilterGroupOrder
)
signals.post_save.disconnect(
post_delete,
dispatch_uid='easy_audit_signals_post_delete')
signals.post_save.disconnect(
post_save, dispatch_uid='easy_audit_signals_post_save')
signals.pre_save.disconnect(
pre_save, dispatch_uid='easy_audit_signals_pre_save')
signals.m2m_changed.disconnect(
m2m_changed, dispatch_uid='easy_audit_signals_m2m_changed')
if 'easyaudit' in settings.INSTALLED_APPS:
signals.post_save.disconnect(
post_delete,
dispatch_uid='easy_audit_signals_post_delete')
signals.post_save.disconnect(
post_save, dispatch_uid='easy_audit_signals_post_save')
signals.pre_save.disconnect(
pre_save, dispatch_uid='easy_audit_signals_pre_save')
signals.m2m_changed.disconnect(
m2m_changed, dispatch_uid='easy_audit_signals_m2m_changed')

def connect_bims_signals():
signals.post_save.connect(
Expand Down Expand Up @@ -111,12 +116,13 @@ def connect_bims_signals():
location_context_post_save_handler,
sender=LocationContextFilterGroupOrder
)
signals.post_save.connect(
post_delete,
dispatch_uid='easy_audit_signals_post_delete')
signals.post_save.connect(
post_save, dispatch_uid='easy_audit_signals_post_save')
signals.pre_save.connect(
pre_save, dispatch_uid='easy_audit_signals_pre_save')
signals.m2m_changed.connect(
m2m_changed, dispatch_uid='easy_audit_signals_m2m_changed')
if 'easyaudit' in settings.INSTALLED_APPS:
signals.post_save.connect(
post_delete,
dispatch_uid='easy_audit_signals_post_delete')
signals.post_save.connect(
post_save, dispatch_uid='easy_audit_signals_post_save')
signals.pre_save.connect(
pre_save, dispatch_uid='easy_audit_signals_pre_save')
signals.m2m_changed.connect(
m2m_changed, dispatch_uid='easy_audit_signals_m2m_changed')
12 changes: 12 additions & 0 deletions bims/static/js/taxon/edit_taxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,17 @@ document.addEventListener('DOMContentLoaded', function() {
});
}

if (tagList.length > 0) {
const tagAutoComplete = $('#taxa-tag-auto-complete');
tagAutoComplete.empty().val(null).trigger('change');
const tagIds = [];
tagList.forEach(tag => {
let newOption = new Option(tag, tag, false, false);
tagAutoComplete.append(newOption);
tagIds.push(tag);
});
tagAutoComplete.val(tagIds).trigger('change');
}

});

9 changes: 9 additions & 0 deletions bims/templates/edit_taxon.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ <h2>
{% endblock %}

{% block foot %}

<script>
let tagList = [];
{% if object.tags.all.count > 0 %}
{% for tag in object.tags.all %}
tagList.push('{{ tag }}');
{% endfor %}
{% endif %}
</script>

<!-- Plugin JavaScript -->
<script src="{% static "js/libs/jquery/jquery-3.3.1.min.js" %}"></script>
Expand Down
1 change: 0 additions & 1 deletion bims/templatetags/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def is_debug():
@register.simple_tag
def is_fada_site():
project_name = get_cache('project_name', '')
print(project_name)
if not project_name:
project_name = preferences.SiteSetting.project_name
set_cache('project_name', project_name)
Expand Down
20 changes: 18 additions & 2 deletions bims/tests/model_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Meta:
model = Taxonomy
django_get_or_create = ('id',)

id = factory.Sequence(lambda n: n)
id = factory.Sequence(lambda n: n + 1)
iucn_status = factory.SubFactory(IUCNStatusF)
scientific_name = factory.Sequence(lambda n: u'Scientific name %s' % n)
canonical_name = factory.Sequence(lambda n: u'Canonical name %s' % n)
Expand Down Expand Up @@ -300,16 +300,32 @@ def taxonomies(self, create, extracted, **kwargs):
)


class TaxonomyUpdateProposalF(TaxonomyF):
class TaxonomyUpdateProposalF(factory.django.DjangoModelFactory):
"""
Taxonomy update proposal
"""
class Meta:
model = TaxonomyUpdateProposal
django_get_or_create = ('id',)

id = factory.Sequence(lambda n: n + 1)
iucn_status = factory.SubFactory(IUCNStatusF)
scientific_name = factory.Sequence(lambda n: u'Scientific name %s' % n)
canonical_name = factory.Sequence(lambda n: u'Canonical name %s' % n)
taxon_group = factory.SubFactory(TaxonGroupF)
original_taxonomy = factory.SubFactory(TaxonomyF)

@factory.post_generation
def vernacular_names(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return

if extracted:
# A list of groups were passed in, use them
for vernacular_name in extracted:
self.vernacular_names.add(vernacular_name)


@factory.django.mute_signals(signals.post_save)
class BiologicalCollectionRecordF(factory.django.DjangoModelFactory):
Expand Down
2 changes: 1 addition & 1 deletion bims/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ def test_profile_update_request(self):
self.assertEqual(response.status_code, 302)
updated_user = get_user_model().objects.get(id=user.id)
self.assertEqual(updated_user.first_name, post_dict['first-name'])
self.assertEqual(updated_user.bims_profile.role, post_dict['role'])
self.assertEqual(updated_user.bims_profile.role.name, post_dict['role'])
15 changes: 4 additions & 11 deletions bims/tests/test_remove_occurrences_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ def test_remove_data(self):
site_setting.save()

vernacular_name_1 = VernacularNameF.create()
taxonomy_1 = TaxonomyF.create(
id=1,
vernacular_names=[vernacular_name_1])
taxonomy_2 = TaxonomyF.create(id=2)
taxonomy_3 = TaxonomyF.create(id=3)
taxonomy_1 = TaxonomyF.create(vernacular_names=[vernacular_name_1])
taxonomy_2 = TaxonomyF.create()
taxonomy_3 = TaxonomyF.create()
taxa_ids = [taxonomy_1.id, taxonomy_2.id, taxonomy_3.id]
view = RemoveOccurrencesApiView.as_view()

taxon_group_1 = TaxonGroupF.create(
Expand Down Expand Up @@ -94,17 +93,11 @@ def test_remove_data(self):
response = view(request)
content = json.loads(response.content)

self.assertEqual(content['Collections deleted'], 2)
self.assertEqual(content['Taxa deleted'], 3)
self.assertEqual(content['Survey deleted'], 2)
self.assertFalse(
BiologicalCollectionRecord.objects.filter(
module_group_id=taxon_group_1.id
).exists()
)
self.assertFalse(
Taxonomy.objects.filter(id__in=[1, 2]).exists()
)
self.assertFalse(
Survey.objects.filter(id__in=survey_ids).count() > 0
)
Expand Down
2 changes: 0 additions & 2 deletions bims/tests/test_review_taxon_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ def test_expert_approve_taxon(self):
).first()
taxon = Taxonomy.objects.filter(
canonical_name='New Taxon1'
).exclude(
id=proposal.id
).first()
self.assertIsNotNone(
taxon
Expand Down
2 changes: 1 addition & 1 deletion bims/tests/test_source_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ def test_generate_source_reference_filter(self, mock_get_current, mock_set, mock
f'source_reference_filter_{self.tenant}',
[
{'id': self.references[0].id, 'reference': f'- | {self.entry.publication_date.year} | Test', 'type': 'Peer-reviewed scientific article'},
{'id': self.references[1].id, 'reference': f' | 2024 | {self.document.source}', 'type': 'Published report or thesis'}]
{'id': self.references[1].id, 'reference': f'- | 2024 | {self.document.source}', 'type': 'Published report or thesis'}]
, timeout=None)
11 changes: 10 additions & 1 deletion bims/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.http import HttpResponseRedirect, Http404

from bims.models import Profile
from bims.models.profile import Role
from bims.models.survey import Survey
from bims.models.location_site import LocationSite
from bims.models.biological_collection_record import BiologicalCollectionRecord
Expand Down Expand Up @@ -33,7 +34,15 @@ def post(self, request, *args, **kwargs):
if not Profile.objects.filter(user=profile).exists():
Profile.objects.create(user=profile)

profile.bims_profile.role = self.request.POST.get('role', '')
role = self.request.POST.get('role', '')
if role:
role_obj, created = Role.objects.get_or_create(
name=role
)
if created:
role_obj.display_name = role.replace('_', ' ').title()
role_obj.save()
profile.bims_profile.role = role_obj
profile.bims_profile.save()
profile.save()

Expand Down

0 comments on commit 1460b3b

Please sign in to comment.