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).encode('utf-8'))
            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")
Exemple #2
0
    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).encode('utf-8'))
            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()

        
        unicodeKanId = self._strip( data['kan_id'] ).encode('utf-8')

        self.stdout.write('Importing from %s record %s ... ' % (
            filename, unicodeKanId ))

        print unicodeKanId
        kan_formatted = self._get_can_number_and_chars(unicodeKanId)
        kanId = kan_formatted[0]
        kanIdChar= kan_formatted[1]
        print kanId, kanIdChar
        if self._exists(data,kanId):
            return None

        record = VotingRecord(
            kan_id=kanId,
            kan_id_chars=kanIdChar,
            scrape_date=self._strip(data['scrape_date']),
            name=self._strip(data['name']),
            date=self._strip(data['date'][0:10]),
            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