def _setup_amendments (self):
        """Establish amendment relationships between voting records.

        Only after all records with their string representation of the
        relationship have been imported, we can setup their relationships
        on a database level.
        """
        for record in VotingRecord.objects.all():
            if not record.number: # skip if record has no bill number
                continue

            self.stdout.write('Setup amendments %s: ' % (record.number))
            for amend in record.amendments.all():
                if not amend.number: # don't add amendments with no number
                    continue

                rec = VotingRecord.objects.filter(number=amend.number)
                if len(rec) == 0:
                    rec = VotingRecord(number=amend.number)
                    rec.save()
                else:
                    rec = rec[0] # ignore multiple occurrences

                record.amended_by.add(rec) # won't add dupes
                self.stdout.write('%s ' % amend.number)

            record.save()
            self.stdout.write("done!\n")
    def _handle_record (self, filename):
        """Handle a single voting record.

        @param filename: filename of voting record
        @type filename: str
        @return: created voting record object
        @rtype: VotingRecord
        """
        fh = open(filename, 'r')
        data = json.loads(fh.read())
        fh.close()

        self.stdout.write('Importing from %s record %s ... ' % (
            filename, data['kan_id']))
        if self._exists(data):
            return None

        record = VotingRecord(
            kan_id=self._strip(data['kan_id']),
            scrape_date=self._strip(data['scrape_date']),
            name=self._strip(data['name']),
            date=self._strip(data['date']),
            url=self._strip(data['url']),
            number=self._strip(data['number']))
        record.save()

        if data['result']:
            self._save_results(record, data['result'])

        if data['amendments']:
            self._save_amendments(record, data['amendments'])

        self.stdout.write("success!\n")
        return record