opencivicdata / pupa

framework for scraping legislative/government data
BSD 3-Clause "New" or "Revised" License
85 stars 42 forks source link

Add support for post-save signals that depend on related objects #329

Closed hancush closed 4 years ago

hancush commented 4 years ago

Description

This PR adds a line to save new objects after related objects are created. This allows use of Django's post_save signal to perform operations that depend on related objects.

Take our use case as an example: We want to use some custom logic to determine a bill's last action date. This logic depends on two types of related objects, bill actions and event related entities. The change in this PR allows us to query related objects after Event and Bill save in order to determine the appropriate last action date.

As ever, thanks for your work!

jamesturk commented 4 years ago

I'm curious if it'd make sense to just fire the signal artificially in this case & skip the database write. From a quick glance it looks like that should be safe here, any thoughts on pros/cons?

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.003%) to 94.772% when pulling d2b768f254749a9b8c1ecfc2487fef6eb39beb1e on hancush:feature/hec/post-save-signal-support into 195a68cc2dd74de79365b352a9a3c00866c3395a on opencivicdata:master.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.007%) to 94.775% when pulling 86006cf67546b0c9864425abc2e6efb130a5402e on hancush:feature/hec/post-save-signal-support into 195a68cc2dd74de79365b352a9a3c00866c3395a on opencivicdata:master.

hancush commented 4 years ago

@jamesturk That's a great point. The only con I can think of is that artificially firing post_save would only support post-save signals that expect related objects, but I can't imagine you'd want to write a pre-save signal that expects data to already exist. Let me give it a shot.

hancush commented 4 years ago

@jamesturk Firing the signal directly seems to work as expected! Revised per your suggestion in 86006cf.