Skip to content

Commit

Permalink
Remove dupes in areas of operations, connects #774
Browse files Browse the repository at this point in the history
  • Loading branch information
hancush committed Sep 1, 2021
1 parent 4e10855 commit eb3aeec
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions association/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from complex_fields.base_models import BaseModel
from organization.models import Organization
from location.models import Location
from sfm_pc.models import GetComplexFieldNameMixin
from sfm_pc.models import GetComplexFieldNameMixin, SuperlativeDateMixin
from source.mixins import SourcesMixin


class Association(models.Model, BaseModel, SourcesMixin, GetComplexFieldNameMixin):
class Association(models.Model, BaseModel, SourcesMixin, SuperlativeDateMixin, GetComplexFieldNameMixin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.startdate = ComplexFieldContainer(self, AssociationStartDate)
Expand Down
4 changes: 2 additions & 2 deletions emplacement/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from complex_fields.base_models import BaseModel
from organization.models import Organization
from location.models import Location
from sfm_pc.models import GetComplexFieldNameMixin
from sfm_pc.models import GetComplexFieldNameMixin, SuperlativeDateMixin
from source.mixins import SourcesMixin


class Emplacement(models.Model, BaseModel, SourcesMixin, GetComplexFieldNameMixin):
class Emplacement(models.Model, BaseModel, SourcesMixin, SuperlativeDateMixin, GetComplexFieldNameMixin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.startdate = ComplexFieldContainer(self, EmplacementStartDate)
Expand Down
15 changes: 7 additions & 8 deletions organization/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import reversion

from django.db import models
from django.db.models import Min, Max
from django.db.models.functions import Coalesce
from django.utils.translation import ugettext as _
from django.conf import settings
Expand Down Expand Up @@ -85,10 +86,9 @@ def associations(self):
with nulls last.
'''
assocs = self.associationorganization_set\
.annotate(lcd=Coalesce('object_ref__associationstartdate__value',
'object_ref__associationenddate__value',
models.Value('1000-0-0')))\
.order_by('-lcd')
.annotate(min_start_date=Min('object_ref__associationstartdate__value'),
max_end_date=Max('object_ref__associationenddate__value'))\
.order_by(Coalesce('min_start_date', 'max_end_date').desc(nulls_last=True))

return assocs

Expand All @@ -101,10 +101,9 @@ def emplacements(self):
with nulls last.
'''
empls = self.emplacementorganization_set\
.annotate(lcd=Coalesce('object_ref__emplacementstartdate__value',
'object_ref__emplacementenddate__value',
models.Value('1000-0-0')))\
.order_by('-lcd')
.annotate(min_start_date=Min('object_ref__emplacementstartdate__value'),
max_end_date=Max('object_ref__emplacementenddate__value'))\
.order_by(Coalesce('min_start_date', 'max_end_date').desc(nulls_last=True))

return empls

Expand Down
7 changes: 2 additions & 5 deletions organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,9 @@ def get_context_data(self, **kwargs):
)

associations = context['organization'].associations
context['associations'] = [ass.object_ref for ass in associations]
context['associations'] = [a.object_ref for a in associations]

area_ids = [
association.object_ref.area.get_value().value.id
for association in associations
]
area_ids = associations.values_list('object_ref__associationarea__value')

context['areas'] = serialize(
'geojson',
Expand Down
13 changes: 13 additions & 0 deletions sfm_pc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ def get_spreadsheet_source_field_name(cls, field_name):
# If no source field name is specified, the default is usually
# the spreadsheet field name with ":source" appended on
return cls.get_spreadsheet_field_name(field_name) + ':source'


class SuperlativeDateMixin:

@property
def first_startdate(self):
return self.startdate.field_model.objects.filter(object_ref=self)\
.order_by('value').first()

@property
def last_enddate(self):
return self.enddate.field_model.objects.filter(object_ref=self)\
.order_by('-value').first()
16 changes: 8 additions & 8 deletions templates/organization/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ <h3 id="areas-of-operation"><i class="fa fa-fw fa-globe"></i>
{% endif %}
</td>
<td class="cited">
{% datetype association.startdate.get_value 'start' %}
{% cite association.startdate.get_value %}
{% datetype association.first_startdate 'start' %}
{% cite association.first_startdate %}
</td>
<td class="cited">
{% datetype association.enddate.get_value 'end' %}
{% cite association.enddate.get_value %}
{% datetype association.last_enddate 'end' %}
{% cite association.last_enddate %}
</td>
</td>
{% endfor %}
Expand Down Expand Up @@ -190,12 +190,12 @@ <h3 id="sites">
{% cite emplacement.site.get_value %}
</td>
<td class="cited">
{% datetype emplacement.startdate.get_value 'start' %}
{% cite emplacement.startdate.get_value %}
{% datetype emplacement.first_startdate 'start' %}
{% cite emplacement.first_startdate %}
</td>
<td class="cited">
{% datetype emplacement.enddate.get_value 'end' %}
{% cite emplacement.enddate.get_value %}
{% datetype emplacement.last_enddate 'end' %}
{% cite emplacement.last_enddate %}
</td>
</td>
{% endfor %}
Expand Down

0 comments on commit eb3aeec

Please sign in to comment.