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

Use the ORM to get most recent affiliation and his assets for each politician #140

Open
giovanisleite opened this issue Aug 29, 2020 · 0 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@giovanisleite
Copy link

giovanisleite commented Aug 29, 2020

Solve these TODO#load_affiliations and TODO#load_assets

def politicians_from_affiliation():
# TODO use the ORM (get most recent affiliation for each `voter_id`)
sql = """
SELECT core_affiliation.*
FROM core_affiliation
INNER JOIN (
SELECT voter_id, MAX(started_in) AS started_in
FROM core_affiliation
GROUP BY voter_id
) AS most_recent
ON most_recent.voter_id = core_affiliation.voter_id
WHERE status = 'R';
"""
yield from (
Politician(current_affiliation=affiliation)
for affiliation in Affiliation.objects.raw(sql).iterator()
)

@staticmethod
def assets_per_politician_per_year():
# TODO use the ORM?
Row = namedtuple("Row", ("politician_id", "year", "value"))
sql = """
SELECT
core_candidate.politician_id,
core_candidate.year,
SUM(core_asset.value) AS total
FROM core_asset
INNER JOIN core_candidate
ON core_candidate.id = core_asset.candidate_id
WHERE core_candidate.politician_id IS NOT NULL
GROUP BY core_candidate.politician_id, core_candidate.year
ORDER BY core_candidate.year DESC
"""

Is it possible to use the ORM in these cases? Will the change turn it more readable?

@giovanisleite giovanisleite changed the title Use the ORM to get most recent affiliation for each politician Use the ORM to get most recent affiliation and his assets for each politician Aug 29, 2020
@ogecece ogecece added help wanted Extra attention is needed question Further information is requested labels Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants