-
Notifications
You must be signed in to change notification settings - Fork 16
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
OCD model migration #240
OCD model migration #240
Conversation
@hancush this is ready for me to indoctrinate you. |
@fgregg making good progress here. can you say more about:
? |
re: django-councilmatic/councilmatic_core/models.py Lines 711 to 721 in 8d64575
we already use this property to display agenda items. should be able to continue using with minor changes, e.g., i think it's |
if you add ordering to the model class, it will, by default return querysets in that order. This is just an opportunity to clean up some code, and not required for this migration. https://docs.djangoproject.com/en/2.2/ref/models/options/#ordering |
Change logDescriptiontl;dr - Remove the need for extensive and error-prone ETL by basing the Councilmatic app off Open Civic Data models, i.e., a jurisdiction-specific database fed by the scrapers, rather than maintaining a remote database containing a set of similar, Councilmatic-specific models. There is a Python package containing Django versions of the OCD models. The relevant ones live in Further reading:
ApproachExtend Open Civic Data modelsSubclassingLeverage multi-table inheritance to add additional fields to OCD models. The primary use case in the refactor is adding slugs to first-class models – Person, Event, Bill, and Organization - and adding a headshot to Person. Since the scrapers create OCD objects, we added signals to each of the subclassed models to create the related Councilmatic model on save. Read more on Django signals. » We have also added a management command to import headshot files. ProxyingWe make extensive use of proxy models to add custom managers and additional properties and methods to classes for use in Councilmatic code. This is an issue because the proxied models return OCD objects, not Councilmatic objects. To get around that, we used the hacky but effective To customize the model of related objects, proxy a model like A brief summary of alternative approaches and why we ultimately did not choose to use them can be found here. Related changes
Upgrade to Django 2
Repair and extend the tests
Remaining changes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall looks great! there are some thing would be nice to change. I can't mark this as a request change.
councilmatic_core/feeds.py
Outdated
@@ -120,7 +120,7 @@ def description(self, obj): | |||
return "Recent sponsored bills from " + obj.name + "." | |||
|
|||
def items(self, person): | |||
sponsored_bills = [s.bill for s in person.primary_sponsorships.order_by('-_bill__last_action_date')[:10]] | |||
sponsored_bills = sorted((s.bill for s in person.primary_sponsorships), key=lambda x: x.last_action_date)[:10] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be ideal to push this sorting to the query.
last_date = timezone.now() - timedelta(36500) | ||
|
||
if last_date: | ||
last_date = last_date - last_date.utcoffset() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's going on here? a comment on the method would be good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll be honest with you, i have no idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that an easier question to answer is: how should the index handle bills without a last action date?
it seems like this is placing them way, way in the past. is this for the legislative session filter? if so, then it would have the effect of not returning bills with this funny date for any of the current legislative sessions. i think that we could achieve this by simply returning None
if there is no last_action_date
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that this has to do with https://github.com/datamade/chi-councilmatic/blob/master/chicago/search_indexes.py#L20-L26
but we should just handle that kind of changes in that code, and we override this method anyway. I think we can drop this method altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done! rebuilt the chicago index to make sure it didn't break anything, and it looks ok to me!
|
||
def convert_document_to_plaintext(self): | ||
import textract |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this import here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so you can run the tests w/o needing to install textract, which is a pita. https://textract.readthedocs.io/en/stable/installation.html / #193 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a comment to that effect.
from django.db import connection | ||
|
||
|
||
class Command(BaseCommand): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
|
||
bill = ProxyForeignKey(Bill, related_name='sponsorships', on_delete=models.CASCADE) | ||
person = ProxyForeignKey(Person, null=True, on_delete=models.SET_NULL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An organization can be a sponsor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approve
This PR changes django-councilmatic to use opencivicdata models.
In concert with datamade/chi-councilmatic#252, these code changes can largely reproduce the Chicago Councilmatic site.
TODO:
add order_by to meta class on EventAgendaItem https://github.com/opencivicdata/python-opencivicdata/blob/master/opencivicdata/legislative/models/event.pyfigure out how we want to handle updating the index