Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate last action date on OCD Bill and Event save #264

Merged
merged 2 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions councilmatic_core/haystack_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ def prepare_ocr_full_text(self, obj):

def get_updated_field(self):
return 'updated_at'

def prepare_last_action_date(self, obj):
# Solr seems to be fussy about the time format, and we do not need the time, just the date stamp.
# https://lucene.apache.org/solr/guide/7_5/working-with-dates.html#date-formatting
if obj.last_action_date:
return obj.last_action_date.date()
18 changes: 18 additions & 0 deletions councilmatic_core/migrations/0051_bill_last_action_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.9 on 2020-01-30 19:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('councilmatic_core', '0050_remove_billdocument'),
]

operations = [
migrations.AddField(
model_name='bill',
name='last_action_date',
field=models.DateTimeField(blank=True, null=True),
),
]
27 changes: 22 additions & 5 deletions councilmatic_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ class Bill(opencivicdata.legislative.models.Bill):

slug = models.SlugField(unique=True)
restrict_view = models.BooleanField(default=False)
last_action_date = models.DateTimeField(blank=True, null=True)

def delete(self, **kwargs):
kwargs['keep_parents'] = kwargs.get('keep_parents', True)
Expand Down Expand Up @@ -460,11 +461,6 @@ def current_action(self):
"""
return self.actions.last()

@property
def last_action_date(self):
if self.current_action:
return self.current_action.date_dt

@property
def first_action(self):
"""
Expand Down Expand Up @@ -597,6 +593,27 @@ def unique_related_upcoming_events(self):
agenda_item__event__start_date__gte=timezone.now()).all()]
return list(set(events))

def get_last_action_date(self):
'''
Return the date of the most recent action. If there is no action,
return the date of the most recent past event for which the bill
appears on the agenda. Otherwise, return None.
'''
current_action = self.current_action

if current_action:
return current_action.date_dt

try:
last_agenda = Event.objects.filter(start_time__lte=timezone.now(),
agenda__related_entities__bill=self)\
.latest('start_time')
except Event.DoesNotExist:
return None

else:
return last_agenda.start_time


class BillSponsorship(opencivicdata.legislative.models.BillSponsorship):
class Meta:
Expand Down
14 changes: 13 additions & 1 deletion councilmatic_core/signals/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Person as OCDPerson,
Post as OCDPost)
from opencivicdata.legislative.models import (Event as OCDEvent,
Bill as OCDBill)
Bill as OCDBill,
EventRelatedEntity as OCDEventRelatedEntity)

from councilmatic_core.models import (Organization as CouncilmaticOrganization,
Person as CouncilmaticPerson,
Expand Down Expand Up @@ -38,6 +39,7 @@ def create_councilmatic_person(sender, instance, created, **kwargs):
# just update the child table, not the parent table
cp.save_base(raw=True)


@receiver(post_save, sender=OCDEvent)
def create_councilmatic_event(sender, instance, created, **kwargs):
if created:
Expand All @@ -50,6 +52,12 @@ def create_councilmatic_event(sender, instance, created, **kwargs):
# just update the child table, not the parent table
ce.save_base(raw=True)

for entity in EventRelatedEntity.objects.filter(agenda_item__event=instance):
cb = entity.bill.councilmatic_bill
cb.last_action_date = cb.get_last_action_date()
cb.save()


@receiver(post_save, sender=OCDBill)
def create_councilmatic_bill(sender, instance, created, **kwargs):
if created:
Expand All @@ -64,6 +72,10 @@ def create_councilmatic_bill(sender, instance, created, **kwargs):
else:
cb = instance.councilmatic_bill

cb.last_action_date = cb.get_last_action_date()
cb.save()


@receiver(post_save, sender=OCDPost)
def create_councilmatic_post(sender, instance, created, **kwargs):
if created:
Expand Down
4 changes: 0 additions & 4 deletions councilmatic_core/templates/partials/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<p class="small text-muted condensed">
<i class="fa fa-fw fa-calendar-o"></i> {{result.last_action_date|date:'n/d/Y'}} - {{result.object.current_action.description | remove_action_subj }}
</p>
{% elif result.object.get_last_action_date %}
<p class="small text-muted condensed">
<i class="fa fa-fw fa-calendar-o"></i> {{result.object.get_last_action_date|date:'n/d/Y'}} - {{result.object.current_action.description | remove_action_subj }}
</p>
{% endif %}

{% if result.object.primary_sponsor %}
Expand Down