Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JusticeV452 committed May 14, 2024
2 parents b15bfe6 + 1d93229 commit af9e074
Show file tree
Hide file tree
Showing 35 changed files with 24,101 additions and 71 deletions.
98 changes: 98 additions & 0 deletions backend/app/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

from .models import (
LSElection,
LoknitiResponders,
LoknitiResponses,
LoknitiCodebook,
TCPDElection,
SeatShare,
CampaignFinance,
)

from .serializers import (
LSElectionSerializaer,
LoknitiRespondersSerializer,
LoknitiResponsesSerializer,
LoknitiCodebookSerializer,
TCPDElectionSerializer,
SeatShareSerializer,
CampaignFinanceSerializer,
Expand Down Expand Up @@ -181,3 +187,95 @@ def campaign_finance_donor_subset(request, donor_name):
campaign_finances = CampaignFinance.objects.filter(donor_name=donor_name)
serializer = CampaignFinanceSerializer(campaign_finances, many=True)
return Response(serializer.data)


@api_view(['GET'])
def get_lokniti_codebook(request):
"""
API endpoint to get codebook
"""
responses = LoknitiCodebook.objects.all()
serializer = LoknitiCodebookSerializer(responses, many=True)
return Response(serializer.data)


@api_view(['GET'])
def get_lokniti_responders(request):
"""
API endpoint to get responders
"""
responses = LoknitiResponders.objects.filter(PC_id=1, PS_id=1)
serializer = LoknitiRespondersSerializer(responses, many=True)
return Response(serializer.data)


@api_view(['GET'])
def get_responders_by_constituency(request, election_year, state_name, pc_id):
"""
API endpoint to get responders by constituency
"""
state_name = state_name.replace("_", " ")
if election_year == 2009:
state_name = state_name.upper()
responses = LoknitiResponders.objects.filter(
election_year=election_year, state_name__icontains=state_name, PC_id=pc_id)
serializer = LoknitiRespondersSerializer(responses, many=True)
return Response(serializer.data)


@api_view(['GET'])
def get_lokniti_responses_by_question_and_year(request, election_year, question_var):
"""
API endpoint to get all responses to a question in a specific
election year
"""
responses = LoknitiResponses.objects.filter(
responder__election_year=election_year, question_var=question_var)
serializer = LoknitiResponsesSerializer(responses, many=True)
return Response(serializer.data)


@api_view(['GET'])
# pylint: disable=too-many-arguments
def get_lokniti_responses_by_constituency(
request, election_year, state_name, PC_id, question_election_year, question_var_orig):
"""
API endpoint to get all responses to a question in a specific
election year and constituency based on a question variable name
from a specific year
"""

question_text = LoknitiCodebook.objects.get(
question_var=question_var_orig, election_year=question_election_year).question_text

question_var = LoknitiCodebook.objects.get(
question_text=question_text, election_year=election_year).question_var

responses = LoknitiResponses.objects.filter(
responder__election_year=election_year, question_var=question_var,
responder__state_name__icontains=state_name, responder__PC_id=PC_id
)
serializer = LoknitiResponsesSerializer(responses, many=True)

return Response(serializer.data)


@api_view(['GET'])
def get_caste_data(request, election_year=2004):
"""
API endpoint to get data and colors for competitiveness map
"""
colors_path = os.path.join(
settings.GEOJSON_DIR, "casteColors.json")
with open(colors_path, encoding='utf-8') as f:
colors_json = json.load(f)

data_path = os.path.join(
settings.GEOJSON_DIR, "casteData.json")
with open(data_path, encoding='utf-8') as f:
data_json = json.load(f)

return Response({
"colors": colors_json[str(election_year)],
"data": data_json[str(election_year)]
})
14 changes: 14 additions & 0 deletions backend/app/data/database_update_config/lokniticodebook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"model_name": "LoknitiCodebook",
"attr_to_column": {
"election_year": "year",
"question_var": "var_name",
"question_text": "question_text"
},
"file_names": [
"lokniti_data/Lokniti_Data_Codebook_2004.csv",
"lokniti_data/Lokniti_Data_Codebook_2009.csv",
"lokniti_data/Lokniti_Data_Codebook_2014.csv",
"lokniti_data/Lokniti_Data_Codebook_2019.csv"
]
}
11 changes: 11 additions & 0 deletions backend/app/data/database_update_config/loknitiresponders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"config_type": "management_command",
"command_name": "load_responders",
"model_names": ["LoknitiResponders", "LoknitiResponses"],
"file_names": [
"lokniti_data/NES_2004_Data_file.csv",
"lokniti_data/NES_2009_Data_file.csv",
"lokniti_data/NES_2014_Data_file.csv",
"lokniti_data/NES_2019_Data_file.csv"
]
}
Loading

0 comments on commit af9e074

Please sign in to comment.