generated from dhmit/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated load_responders to work with load_responses
- Also changed load_responders to check if responder is a duplicate before saving -
- Loading branch information
1 parent
9c23d67
commit 28e50bd
Showing
3 changed files
with
112 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Django management command helper load_responders | ||
Updates local db with values from base csv dataset | ||
""" | ||
|
||
from app.models import LoknitiResponses, LoknitiCodebook | ||
from django.db.models import Q | ||
|
||
|
||
def get_question_vars(year): | ||
""" | ||
Returns a dict mapping question variables for the given year to their codebook entry | ||
""" | ||
codebook_entries = LoknitiCodebook.objects.filter( | ||
Q(election_year=year) & Q(question_var__startswith='q')) | ||
question_vars = {question.question_var: question for question in codebook_entries} | ||
return question_vars | ||
|
||
|
||
def load_responses(responder, row): | ||
""" | ||
Load Responses given Responder instance and Lokniti NES csv row | ||
""" | ||
|
||
# get survey questions | ||
question_vars = get_question_vars(responder.election_year) | ||
|
||
for q_var, codebook_entry in question_vars.items(): | ||
response = LoknitiResponses( | ||
respondent_no=responder.respondent_no, | ||
election_year=responder.election_year, | ||
question_var=q_var, | ||
response=getattr(row, q_var), | ||
responder=responder, | ||
entry=codebook_entry | ||
) | ||
response.save() |
This file was deleted.
Oops, something went wrong.