Skip to content

Commit

Permalink
Added search routes to user_friendly, updated routes with limit, offs…
Browse files Browse the repository at this point in the history
…et, order_by (#21)

* Create models for the corresponding section

Create models representing the routes responses for each section.
Added models for new section 'user_friendly'

* Remove redundant classes related to the response

* Remove redundant classes related to the response

* Create an enum file for multiple enum types

* Examples of errors in HTTP responses.

* Update getting data and streaming data

Added methods for getting data for 'user_friendly' section routes:
'get_members()' and 'get_members_info()'. 
Added Helper function for checking if some member Knesset id exists:
'is_mk_individual_exist()'
Added streaming data option for large values of 'limit' and 'offset' when they are used.
Added method for serialize jsons: 
'json_serialize()'

* Update routes info, new routes added

Update routes errors responses and success response. Handle errors with 'HTTPException()' . Added 'user_friendly' section to routes. Simplified response by removing 'success' field, now returning only data.

* Update queries.py

Added new queries for 'get_members()','get_members_presence()' ,'get_members_votes()','get_members_attended_committee_meetings()','get_members_bills()'.
Try query optimization for 'get_members()'.

* Added errors.py to docker

* Fixing PEP 8

* Adding get_member() query for returning single Knesset member user friendly

* - Added get_member()
- Added get_member_by_committee()
- Added get_member_by_faction()
- Added get_member_by_faction_chairperson()
- Added get_member_by_govministries()
- Added get_member_by_name()
- Removing get_members_info()
- Added is_knesset_term_exist()
- Added  is_mk_individual_exist()
- Added named placeholders where needed
- Added docstring to functions
- Added logger.critical() when unknown execption is thrown
- Updated create_query_list() logic, fixing order_by logic
- Added option for disable named cursor in get_db_cursor()

* - Added '/members/{mk_individual_id}' route
- Added '/members/search/by-committee/{committee}' route
- Added '/members/search/by-faction/{faction}' route
- Added '/members/search/by-faction_chairperson/{faction}' route
- Added '/members/search/by-govministries/{govministry}' route
- Added '/members/search/by-name/{name}' route
- Updated these user friendly routes with limit, offset, pagination, streaming :
/members/{mk_individual_id}/presence
/members/{mk_individual_id}/attended_committee_meetings
/members/{mk_individual_id}/votes
/members/{mk_individual_id}/bills
- Order by logic was updated in routes which have limit and offset
- The code is organized better, routes are grouped together per common section

* Fixed field type in 'MkIndividualIDs' model
  • Loading branch information
bobiboMC authored Feb 26, 2024
1 parent 5153b32 commit 200f23a
Show file tree
Hide file tree
Showing 16 changed files with 4,050 additions and 2,841 deletions.
751 changes: 626 additions & 125 deletions api/db.py

Large diffs are not rendered by default.

84 changes: 60 additions & 24 deletions api/queries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def get_member_kns_query(id_field):
return f"""
SELECT
return f"""
SELECT
f."{id_field}",
f."FirstName",
f."LastName",
Expand All @@ -21,7 +21,7 @@ def get_member_kns_query(id_field):
LEFT JOIN members_mk_individual_faction_chairpersons ch
ON ch."mk_individual_id" = f."mk_individual_id"
LEFT JOIN (
SELECT
SELECT
jsonb_array_elements_text(m.member_mk_ids)::integer AS member_mk_id,
array_agg(DISTINCT m.knesset) AS knesset_array
FROM members_faction_memberships m
Expand All @@ -38,9 +38,10 @@ def get_member_kns_query(id_field):
ORDER BY m."faction_name" DESC
LIMIT 1;
"""

def get_minister_query(id_field):
return f"""


def get_minister_query(id_field):
return f"""
SELECT
f."{id_field}",
f."FirstName",
Expand Down Expand Up @@ -93,7 +94,7 @@ def get_minister_query(id_field):


def get_members():
return f"""
return """
SELECT
members_mk_individual.mk_individual_id,
members_mk_individual.mk_individual_first_name,
Expand All @@ -110,12 +111,12 @@ def get_members():
COALESCE(json_agg(DISTINCT migm.*), '[]') AS govministries
FROM
members_mk_individual
LEFT JOIN members_mk_individual_committees mci ON members_mk_individual.mk_individual_id = mci.mk_individual_id AND mci.knesset = %s
LEFT JOIN members_mk_individual_factions mif ON members_mk_individual.mk_individual_id = mif.mk_individual_id AND mif.knesset = %s
LEFT JOIN members_mk_individual_faction_chairpersons mfcp ON members_mk_individual.mk_individual_id = mfcp.mk_individual_id AND mfcp.knesset = %s
LEFT JOIN members_mk_individual_govministries migm ON members_mk_individual.mk_individual_id = migm.mk_individual_id AND migm.knesset = %s
LEFT JOIN members_mk_individual_committees mci ON members_mk_individual.mk_individual_id = mci.mk_individual_id AND mci.knesset = %(knesset_term)s
LEFT JOIN members_mk_individual_factions mif ON members_mk_individual.mk_individual_id = mif.mk_individual_id AND mif.knesset = %(knesset_term)s
LEFT JOIN members_mk_individual_faction_chairpersons mfcp ON members_mk_individual.mk_individual_id = mfcp.mk_individual_id AND mfcp.knesset = %(knesset_term)s
LEFT JOIN members_mk_individual_govministries migm ON members_mk_individual.mk_individual_id = migm.mk_individual_id AND migm.knesset = %(knesset_term)s
WHERE
members_mk_individual."IsCurrent" = %s
members_mk_individual."IsCurrent" = %(is_current)s
GROUP BY
members_mk_individual.mk_individual_id, members_mk_individual.mk_individual_first_name,members_mk_individual.mk_individual_name,members_mk_individual."PersonID",members_mk_individual."GenderID"
,
Expand All @@ -125,27 +126,61 @@ def get_members():
members_mk_individual.altnames;
"""

def get_members_presence(mk_individual_id:int):
return f"""select * from members_presence where

def get_member():
return """
SELECT
members_mk_individual.mk_individual_id,
members_mk_individual.mk_individual_first_name,
members_mk_individual.mk_individual_name,
members_mk_individual."PersonID",
members_mk_individual."GenderID",
members_mk_individual."GenderDesc",
members_mk_individual."IsCurrent",
members_mk_individual.mk_individual_email,
members_mk_individual.altnames,
COALESCE(json_agg(DISTINCT mci.*), '[]') AS committee_positions,
COALESCE(json_agg(DISTINCT mif.*), '[]') AS factions,
COALESCE(json_agg(DISTINCT mfcp.*), '[]') AS faction_chairpersons,
COALESCE(json_agg(DISTINCT migm.*), '[]') AS govministries
FROM
members_mk_individual
LEFT JOIN members_mk_individual_committees mci ON members_mk_individual.mk_individual_id = mci.mk_individual_id AND mci.knesset = %(knesset_term)s
LEFT JOIN members_mk_individual_factions mif ON members_mk_individual.mk_individual_id = mif.mk_individual_id AND mif.knesset = %(knesset_term)s
LEFT JOIN members_mk_individual_faction_chairpersons mfcp ON members_mk_individual.mk_individual_id = mfcp.mk_individual_id AND mfcp.knesset = %(knesset_term)s
LEFT JOIN members_mk_individual_govministries migm ON members_mk_individual.mk_individual_id = migm.mk_individual_id AND migm.knesset = %(knesset_term)s
WHERE
members_mk_individual."mk_individual_id" = %(mk_individual_id)s
GROUP BY
members_mk_individual.mk_individual_id, members_mk_individual.mk_individual_first_name, members_mk_individual.mk_individual_name, members_mk_individual."PersonID", members_mk_individual."GenderID",
members_mk_individual."GenderDesc", members_mk_individual."IsCurrent", members_mk_individual.mk_individual_email, members_mk_individual.altnames;
"""


def get_members_presence(mk_individual_id: int):
return f"""select * from members_presence where
mk_id = {mk_individual_id} order by date desc
"""

def get_members_attended_committee_meetings(mk_individual_id:int):

def get_members_attended_committee_meetings(mk_individual_id: int):
return f"""SELECT
"CommitteeSessionID", "KnessetNum", "TypeID", "TypeDesc",
"CommitteeSessionID", "KnessetNum", "TypeID", "TypeDesc",
"CommitteeID",
"Location", "SessionUrl", "BroadcastUrl",
"StartDate", "FinishDate", "Note",
topics, committee_name, bill_names, bill_types,
topics, committee_name, bill_names, bill_types,
related_to_legislation
FROM
people_committees_meeting_attendees
WHERE
attended_mk_individual_ids @> '[{mk_individual_id}]'
"""

def get_members_votes(mk_individual_id:int):
return f"""SELECT knesset_num, session_id, sess_item_id, sess_item_dscr, vote_item_id, vote_item_dscr, vote_date, vote_time, is_elctrnc_vote, is_accepted, total_for, total_against, total_abstain, 'pro' AS mk_vote

def get_members_votes(mk_individual_id: int):
return f"""SELECT * FROM (
SELECT knesset_num, session_id, sess_item_id, sess_item_dscr, vote_item_id, vote_item_dscr, vote_date, vote_time, is_elctrnc_vote, is_accepted, total_for, total_against, total_abstain, 'pro' AS mk_vote
FROM votes_view_vote_rslts_hdr_approved_extra
WHERE mk_ids_pro @> '[{mk_individual_id}]'
Expand All @@ -159,10 +194,12 @@ def get_members_votes(mk_individual_id:int):
SELECT knesset_num, session_id, sess_item_id, sess_item_dscr, vote_item_id, vote_item_dscr, vote_date, vote_time, is_elctrnc_vote, is_accepted, total_for, total_against, total_abstain, 'abstain' AS mk_vote
FROM votes_view_vote_rslts_hdr_approved_extra
WHERE mk_ids_abstain @> '[{mk_individual_id}]';
"""
WHERE mk_ids_abstain @> '[{mk_individual_id}]'
) a
"""

def get_members_bills(mk_individual_id:int):

def get_members_bills(mk_individual_id: int):
return f"""SELECT
bkb."BillID",
bkb."KnessetNum",
Expand Down Expand Up @@ -192,5 +229,4 @@ def get_members_bills(mk_individual_id:int):
bills_kns_bill bkb ON bkb."BillID" = bkb_init."BillID"
WHERE
mki.mk_individual_id = {mk_individual_id}
"""

"""
Loading

0 comments on commit 200f23a

Please sign in to comment.