#NEWLINE# def main( ) : #NEWLINE# #INDENT# parser = argparse . ArgumentParser ( description = 'Import respondents into CheckUp.' , epilog = 'Importer written by Matt Clark an Adam Playford.' ) #NEWLINE# parser . add_argument ( 'file' , help = 'The CSV file to be imported.' ) #NEWLINE# parser . add_argument ( 'survey_slug' , help = 'The slug of the survey to import data to.' ) #NEWLINE# args = parser . parse_args ( ) #NEWLINE# try : #NEWLINE# #INDENT# survey = Survey . objects . get ( home_slug = args . survey_slug ) #NEWLINE# #UNINDENT# except Survey . DoesNotExist : #NEWLINE# #INDENT# print ( ( 'Could not find a Survey with slug "%s"' % args . survey_slug ) ) #NEWLINE# return #NEWLINE# #UNINDENT# with open ( args . file , 'rU' ) as csvfile : #NEWLINE# #INDENT# respondents = csv . DictReader ( csvfile ) #NEWLINE# for row in respondents : #NEWLINE# #INDENT# ( group , group_created ) = Group . objects . get_or_create ( name = row [ 'group' ] ) #NEWLINE# try : #NEWLINE# #INDENT# respondent = Respondent . objects . get ( sheet_id = int ( row [ 'sheet_id' ] ) ) #NEWLINE# #UNINDENT# except Respondent . DoesNotExist : #NEWLINE# #INDENT# respondent = Respondent ( sheet_id = int ( row [ 'sheet_id' ] ) ) #NEWLINE# #UNINDENT# cols = [ 'party' , 'district' , 'gender' , 'first_name' , 'last_name' , 'website' , 'office_phone' , 'email' , 'twitter' , 'address' , 'address2' , 'city' , 'state' , 'zip' , 'headshot' ] #NEWLINE# try : #NEWLINE# #INDENT# title = Title . objects . get ( long = row [ 'long' ] ) #NEWLINE# ( title . short , title . order ) = ( row [ 'short' ] , row [ 'order' ] ) #NEWLINE# title . save ( ) #NEWLINE# #UNINDENT# except Title . DoesNotExist : #NEWLINE# #INDENT# title = Title . objects . create ( long = row [ 'long' ] , short = row [ 'short' ] , order = col [ 'order' ] ) #NEWLINE# #UNINDENT# except Title . MultipleObjectsReturned : #NEWLINE# #INDENT# Title . objects . filter ( long = row [ 'long' ] ) . delete ( ) #NEWLINE# title = Title . objects . create ( long = row [ 'long' ] , short = row [ 'short' ] , order = row [ 'order' ] ) #NEWLINE# #UNINDENT# for col in cols : #NEWLINE# #INDENT# setattr ( respondent , col , row [ col ] ) #NEWLINE# #UNINDENT# ( respondent . group , respondent . title ) = ( group , title ) #NEWLINE# respondent . save ( ) #NEWLINE# assignment_cols = [ 'form_chatter' , 'display_chatter' ] #NEWLINE# try : #NEWLINE# #INDENT# reporter = Reporter . objects . get ( user__last_name = row [ 'reporter' ] ) #NEWLINE# #UNINDENT# except Reporter . DoesNotExist : #NEWLINE# #INDENT# print ( ( 'Error! Reporter with last name "%s" not in database.' % row [ 'reporter' ] ) ) #NEWLINE# return #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# assignment = respondent . assignments . get ( survey = survey ) #NEWLINE# #UNINDENT# except Assignment . DoesNotExist : #NEWLINE# #INDENT# assignment = Assignment ( survey = survey , respondent = respondent ) #NEWLINE# #UNINDENT# assignment . questions = QuestionGroup . objects . get ( name = row [ 'question_group' ] ) #NEWLINE# assignment . reporter = reporter #NEWLINE# for col in assignment_cols : #NEWLINE# #INDENT# setattr ( assignment , col , row [ col ] ) #NEWLINE# #UNINDENT# assignment . save ( )