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

further minimizing django councilmatic #396

Open
fgregg opened this issue Jan 6, 2024 · 4 comments
Open

further minimizing django councilmatic #396

fgregg opened this issue Jan 6, 2024 · 4 comments

Comments

@fgregg
Copy link
Member

fgregg commented Jan 6, 2024

it’s been productive to move the views and templates and such out of django councilmatic.

another good step would be to move all possible methods from the django councilmatic models to the chi-councilmatic proxy models

@fgregg
Copy link
Member Author

fgregg commented Jan 8, 2024

this is not actually a good approach since we have some things set up in django_councilmatic to follow relations and get the right things back.

i think i'd like to experiment with a different approach where we extend opencivicdata models to include a slug, and then we basically replicate the model definitions from opencivicdata into chi-councilmatic, but use managed=False.

this will require us to keep these model definitions in sync, but the opencivicdata models are very slow moving.

@fgregg
Copy link
Member Author

fgregg commented Jan 8, 2024

i wonder if an approach like this could work.

  1. do not have opencividata be an installed app
  2. in models.py for chicago, do something like
import opencividata.core

class Person(opencivicdata.core.models.Person):
    class Meta:
        managed = False
        db_table = "opencivicdata_person"

to cut down on the amount of duplication.

@fgregg
Copy link
Member Author

fgregg commented Jan 8, 2024

i think that will work if we monkeypatch the opencivicdata.core.models as abstract.

@fgregg
Copy link
Member Author

fgregg commented Jan 8, 2024

this is working beautifully:

from contextlib import contextmanager

@contextmanager
def abstract(base_model):

    previous_abstract_state = base_model._meta.abstract
    base_model._meta.abstract = True
    try:
        yield base_model
    finally:
        base_model._meta.abstract = previous_abstract_state


with abstract(opencivicdata.core.models.Person) as AbstractPerson:
    class TestPerson(AbstractPerson):
        class Meta:
            managed = False
            db_table = "opencivicdata_person"
    

        hello = 'there'
>>> from chicago.models import TestPerson
>>> TestPerson.objects.first()
<TestPerson: Martin, Matthew J.>
>>> TestPerson.objects.first().id
'ocd-person/02abf3c1-c846-4249-b086-54f1707f5a16'
>>> TestPerson.objects.first().family_name
'Martin'
>>> TestPerson.objects.first().hello
'there'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants