def handle(self, *args, **options): #args = '<filename>' help = 'Bills places from csv file to "Bills" table' print args[0] if len(args) <= 0: raise CommandError('Please specify file name') if not os.path.exists(args[0]): raise CommandError("File %s doesn't exist" % args[0]) #with codecs.open(args[0], 'r', 'cp1255 ') as f: with open(args[0], 'r') as f: r = csv.DictReader(f) for d in r: bill = Bill() for header, conv_func in BILL_FIELDS: value = d[header] if conv_func: value = conv_func(value) setattr(bill, header, value) try: bill.full_clean() except ValidationError as e: print "error in bill" continue if not Bill.objects.filter(name=bill.name).count() == 0: print "Bill already exists!" else: bill.save() print 'Done importing bills csv!'
def handle(self, *args, **options): help = 'Bills places from google spreadsheet to "Bills" table' response = urllib.urlopen(URL); data = json.loads(response.read()) bills = data['feed']['entry'] for e in bills: bill = Bill() for header, conv_func in BILL_FIELDS: value = e['gsx$' + header.replace("_","")]['$t'] if conv_func: value = conv_func(value) setattr(bill, header, value) try: bill.full_clean() except ValidationError as e: print "error in bill" continue if not Bill.objects.filter(name=bill.name).count() == 0: print "Bill already exists!" else: if bill.passed is not None: bill.save() print "Bill saved!" else: print "Bill has no vote" print 'Done importing bills from google spreadsheet!'