From 67648936d88e79cccd634be999584137a28359cd Mon Sep 17 00:00:00 2001 From: Henry Lai Date: Tue, 25 Jun 2024 12:47:49 -0600 Subject: [PATCH] add office subtitles --- macros/generate_office_subtitle.sql | 222 ++++++++++++-------- macros/process_mn_sos_candidate_filings.sql | 3 +- 2 files changed, 138 insertions(+), 87 deletions(-) diff --git a/macros/generate_office_subtitle.sql b/macros/generate_office_subtitle.sql index a1f06f9..d72adb9 100644 --- a/macros/generate_office_subtitle.sql +++ b/macros/generate_office_subtitle.sql @@ -1,97 +1,147 @@ {% macro generate_office_subtitle(state, office_name, election_scope, district_type, district, school_district, hospital_district, seat, county, municipality) %} -concat( - CASE WHEN state, - ' - ', - office_name, - ' - ', - CASE WHEN county is not null THEN concat(county,' County - ') END, - CASE WHEN district is not null THEN concat(district,' - ') END, - CASE - WHEN seat is NULL THEN '' - WHEN seat ILIKE 'At Large' THEN concat(seat,' - ') - ELSE concat(seat,' - ') - END, - CASE WHEN is_special_election = TRUE THEN 'Special Election - ' ELSE '' END, - CASE - WHEN race_type = 'primary' THEN concat( - 'Primary - ', party - ) - WHEN race_type = 'general' THEN 'General' - ELSE '' END, - ' - ', - '2024' -- TODO: Update this to be dynamic - ) AS title, - - - - - - - +CASE -- test election_scope + WHEN {{ election_scope }} = 'state' THEN + CASE + WHEN {{ office_name }} = 'U.S. Senate' OR ({{ district }} IS null AND {{ seat }} IS null) THEN + -- use full state name when there is no district or seat, or if U.S. Senate + SELECT name FROM us_states WHERE code ILIKE {{ state }} + -- 'Minnesota' + WHEN {{ seat }} ILIKE '%At Large%' THEN + concat({{ state }}, ' - ', {{ seat }}) + ELSE + concat({{ state }}, ' - Seat ', {{ seat }}) + END + WHEN {{ election_scope }} = 'county' THEN + CASE + WHEN {{ district }} IS null THEN + concat({{ county }}, ' County, ', {{ state }}) + ELSE + concat({{ county }}, ' County, ', {{ state }}, ' - District ', {{ district }}) + END + WHEN {{ election_scope }} = 'city' THEN + CASE -- this list of municipalities have dupes so must add the county + WHEN {{ municipality }} IN ('Beaver Township', 'Becker Township', 'Clover Township', + 'Cornish Township', 'Fairview Township', 'Hillman Township', + 'Lawrence Township', 'Long Lake Township', 'Louisville Township', + 'Moose Lake Township', 'Stokes Township', 'Twin Lakes Township') THEN + concat( + {{ municipality }}, + ' - ', + {{ county }}, + ' County, ', + {{ state }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + ELSE + concat( + {{ municipality }}, + ', ', + {{ state }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + END + WHEN {{ election_scope }} = 'district' THEN + CASE -- test district_types + WHEN {{ district_type }} = 'us_congressional' THEN concat({{ state }}, ' - District ', {{ district }}) + WHEN {{ district_type }} = 'state_house' THEN concat({{ state }}, ' - House District ', {{ district }}) + WHEN {{ district_type }} = 'state_senate' THEN concat({{ state }}, ' - Senate District ', {{ district }}) + WHEN {{ district_type }} = 'county' THEN concat({{ county }}, ' County, ', {{ state }}, ' - District ', {{ district }}) + + WHEN {{ district_type }} = 'city' THEN + CASE -- any numbers mean add 'District' before it, otherwise add whatever is in District (no offices with seats here) + WHEN {{ district }} ~ '^\d+$' THEN concat({{ municipality }}, ', ', {{ state }}, ' - District ', {{ district }}) + ELSE concat({{ municipality }}, ', ', {{ state }}, ' - ', {{ district }}) + END + WHEN {{ district_type }} = 'school' THEN + CASE -- tests if District exists, and then tests if Seat exists for each case + WHEN {{ district }} IS null THEN + -- e.g. "MN - ISD #508 - At Large" + concat( + {{ state }}, + ' - ', + {{ school_district }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + WHEN {{ district }} ~ '^\d+$' THEN + -- e.g. "MN - ISD #709 - District 3" + concat( + {{ state }}, + ' - ', + {{ school_district }}, + ' - District ', + {{ district }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + ELSE + -- e.g. "MN - ISD #2365 - Gibbon District" + concat( + {{ state }}, + ' - ', + {{ school_district }}, + ' - ', + {{ district }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + END + WHEN {{ district_type }} = 'judicial' THEN concat({{ state }}, ' - Seat ', {{ seat }}) -DECLARE @subtitle AS TEXT = '' + WHEN {{ district_type }} = 'hospital' THEN + CASE + WHEN {{ district }} IS null THEN + -- e.g. "Canby Community - At Large" + concat( + {{ hospital_district }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + WHEN {{ district }} ~ '^\d+$' THEN + -- e.g. "Cook County - District 1" + concat( + {{ hospital_district }}, + ' - District ', + {{ district }}, + CASE + WHEN {{ seat }} IS null THEN '' + WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }} + ELSE ' - Seat ' || {{ seat }} + END + ) + END -CASE -- test election_scope - -- state - WHEN {{ election_scope }} = 'state' - THEN - CASE - WHEN {{ district }} is null AND {{ seat }} is null -- ignore for U.S. Senate, seat is used for Class - THEN - concat(@subtitle, 'Minnesota') -- use full state name + WHEN {{ district_type }} = 'soil_and_water' THEN concat({{ county }}, ' County, ', {{ state }}, ' - District ', {{ district }}) + ELSE - CASE - WHEN {{ district }} = number - THEN - concat(@subtitle, ) - -- if (district and seat = null then use full state name - -- else use two letter abbreviation of state - -- if district is just a number add 'District' before - -- if name is State House add 'House District' before - -- if name is State Senate add 'Senate District' before - -- districts and seats - -- hospital district, judges, justices, school board - -- maybe city council - concat({{ state }}, ' - ', {{ district }}, ' ', {{ seat }})) - -- county - WHEN {{ election_scope }} = 'county' - THEN - slugify(concat('mn', ' ', REPLACE({{ office_name }},'&','and'), ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} )) - -- city - WHEN {{ election_scope }} = 'city' - THEN - slugify(concat('mn', ' ', REPLACE({{ office_name }},'&','and'), ' ', REPLACE({{ municipality }},'Township','Twp'), ' ', {{ county }}, ' county ', {{ school_district }}, ' ', {{ district }}, ' ', {{ seat }} )) - -- district - WHEN {{ election_scope }} = 'district' - THEN - CASE -- test district_types - WHEN {{ district_type }} = 'county' - THEN - slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} )) - WHEN {{ district_type }} = 'city' - THEN - slugify(concat('mn', ' ', REPLACE({{ office_name }},'&','and'), ' ', REPLACE({{ municipality }},'Township','Twp'), ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} )) - WHEN {{ district_type }} = 'school' - THEN - slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ school_district }}, ' ', {{ district }}, ' ', {{ seat }} )) - WHEN {{ district_type }} = 'judicial' - THEN - slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ district }}, ' ', {{ seat }} )) - WHEN {{ district_type }} = 'hospital' - THEN - slugify(concat('mn', ' ', {{ office_name }}, ' ', hospital_district, ' ',{{ district }}, ' ', {{ seat }} )) - WHEN {{ district_type }} = 'soil_and_water' - THEN - slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} )) - ELSE -- us_congressional, state_house, state_senate - slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ district }}, ' ', {{ seat }} )) + null END - ELSE -- national - '' - END + ELSE -- national + null + END {% endmacro %} diff --git a/macros/process_mn_sos_candidate_filings.sql b/macros/process_mn_sos_candidate_filings.sql index 4fde748..dccf212 100644 --- a/macros/process_mn_sos_candidate_filings.sql +++ b/macros/process_mn_sos_candidate_filings.sql @@ -100,7 +100,7 @@ WITH transformed_filings AS ( -- get Municipality {{ get_municipality('raw.office_title', 'election_scope', 'district_type') }} AS municipality, - + -- Candidate residence address CASE WHEN raw.residence_street_address IN ('PRIVATE', 'NOT REQUIRED') @@ -211,6 +211,7 @@ SELECT 'g' ) AS phone, {{ generate_office_slug('f.office_name', 'f.election_scope', 'f.district_type', 'f.district', 'f.school_district', 'f.hospital_district', 'f.seat', 'f.county', 'f.municipality') }} AS office_slug, + {{ generate_office_subtitle('f.state_id', 'f.office_name', 'f.election_scope', 'f.district_type', 'f.district', 'f.school_district', 'f.hospital_district', 'f.seat', 'f.county', 'f.municipality') }} AS office_subtitle, {{ get_political_scope('f.office_name', 'f.election_scope', 'f.district_type') }} AS political_scope, residence_street_address, residence_city,