def fetch(self, verbose=False, log=None): """ Compares and updates all Committee objects against the data contained within the Sunlight Foundation's Congress API, including subcommittees and memberships to committees. There are three steps to this method: 1) Get or create a Committee object for each congressional committee 2) Clear all existing Committee memberships 3) Iterate over each legislator, fetching and creating all memberships. Each operation is enclosed in a transaction that is only committed if no exceptions are raised. """ for chamber in CHAMBER_CHOICES: if verbose and log: log.write("Fetching %s Committees\n" % chamber[0]) # Get or create a Committee object for each congressional committee for committee in congress.committees(chamber[0]): with transaction.commit_on_success(): committee_obj = self.get_or_create(id=committee["id"])[0] for name, value in committee.iteritems(): setattr(committee_obj, name, value) committee_obj.save() if verbose and log: log.write("- %s" % committee_obj.name) # Create object for each subcommittee of the committee if "subcommittees" in committee: for subcommittee in committee["subcommittees"]: subcommittee = subcommittee["committee"] subcommittee_obj = self.get_or_create(id=subcommittee["id"])[0] for name, value in subcommittee.iteritems(): setattr(subcommittee_obj, name, value) subcommittee_obj.parent = committee_obj subcommittee_obj.save() if verbose and log: log.write(" - %s\n" % subcommittee_obj.name) if verbose and log: log.write("\nClearing committee memberships\n") # Clear all committee memberships with transaction.commit_on_success(): for committee in self.all(): committee.members.clear() committee.save() if verbose and log: log.write("\nCreating committee memberships\n") # Iterate over each legislator, fetching and creating all memberships Legislator = self.model._meta.get_field_by_name("members")[0].rel.to for legislator in Legislator.objects.all(): leg_id = legislator.bioguide_id with transaction.commit_on_success(): for committee in congress.committees_for_legislator(leg_id): committee_obj = self.get(id=committee["id"]) committee_obj.members.add(legislator) committee_obj.save() if verbose and log: log.write("- %s is on the %s\n" % (legislator.fullname, committee_obj.name))