def update_from_knesset_data(self): from knesset_data.html_scrapers.votes import HtmlVote resolve_vote_types = { 'voted for': u'for', 'voted against': u'against', 'abstain': u'abstain', 'did not vote': u'no-vote', } html_votes = HtmlVote.get_from_vote_id(self.src_id).member_votes for vote_type in ['for', 'against', 'abstain']: expected_member_ids = [int(member_id) for member_id, member_vote_type in html_votes if resolve_vote_types[member_vote_type] == vote_type] actual_member_ids = [int(member_id) for member_id in self.actions.filter(type=vote_type).values_list('member_id', flat=True)] if len(expected_member_ids) > len(actual_member_ids): missing_member_ids = [member_id for member_id in expected_member_ids if member_id not in actual_member_ids] for member_id in missing_member_ids: logger.info('fixing for member id %s' % member_id) vote_action, created = VoteAction.objects.get_or_create(member=Member.objects.get(pk=member_id), vote=self, defaults={'type': vote_type}) if created: vote_action.save() elif len(expected_member_ids) != len(actual_member_ids): raise Exception( 'strange mismatch in members, actual has more members then expected, this is unexpected')
def update_from_knesset_data(self): from knesset_data.html_scrapers.votes import HtmlVote resolve_vote_types = { 'voted for': u'for', 'voted against': u'against', 'abstain': u'abstain', 'did not vote': u'no-vote', } html_votes = HtmlVote.get_from_vote_id(self.src_id).member_votes for vote_type in ['for', 'against', 'abstain']: expected_member_ids = [ int(member_id) for member_id, member_vote_type in html_votes if resolve_vote_types[member_vote_type] == vote_type ] actual_member_ids = [ int(member_id) for member_id in self.actions.filter( type=vote_type).values_list('member_id', flat=True) ] if len(expected_member_ids) > len(actual_member_ids): missing_member_ids = [ member_id for member_id in expected_member_ids if member_id not in actual_member_ids ] for member_id in missing_member_ids: logger.info('fixing for member id %s' % member_id) vote_action, created = VoteAction.objects.get_or_create( member=Member.objects.get(pk=member_id), vote=self, defaults={'type': vote_type}) if created: vote_action.save() elif len(expected_member_ids) != len(actual_member_ids): raise Exception( 'strange mismatch in members, actual has more members then expected, this is unexpected' )
def test(self): html_vote = HtmlVote.get_from_vote_id(24084) self.assertListEqual( html_vote.member_votes, [ ("863", "voted for"), ("879", "voted for"), ("953", "voted for"), ("914", "voted for"), ("918", "voted for"), ("950", "voted for"), ("922", "voted for"), ("924", "voted for"), ("932", "voted for"), ("944", "voted for"), ("230", "voted for"), ("862", "voted for"), ("865", "voted for"), ("881", "voted for"), ("818", "voted for"), ("901", "voted for"), ("216", "voted for"), ("876", "voted for"), ("915", "voted for"), ("854", "voted for"), ("941", "voted for"), ("871", "voted for"), ], )
def _add_vote_actions(self, dataservice_vote, oknesset_vote): for member_id, vote_result_code in HtmlVote.get_from_vote_id(dataservice_vote.id).member_votes: member_qs = Member.objects.filter(pk=member_id) if member_qs.exists(): member = member_qs.first() vote_type = self._resolve_vote_type(vote_result_code) vote_action, created = VoteAction.objects.get_or_create(vote=oknesset_vote, member=member, defaults={'type': vote_type, 'party': member.current_party}) if created: vote_action.save() else: raise VoteScraperException('vote %s: could not find member id %s' % (dataservice_vote.id, member_id))
def _add_vote_actions(self, dataservice_vote, oknesset_vote): for member_id, vote_result_code in HtmlVote.get_from_vote_id( dataservice_vote.id).member_votes: member_qs = Member.objects.filter(pk=member_id) if member_qs.exists(): member = member_qs.first() vote_type = self._resolve_vote_type(vote_result_code) vote_action, created = VoteAction.objects.get_or_create( vote=oknesset_vote, member=member, defaults={ 'type': vote_type, 'party': member.current_party }) if created: vote_action.save() else: raise VoteScraperException( 'vote %s: could not find member id %s' % (dataservice_vote.id, member_id))
def test(self): html_vote = HtmlVote.get_from_vote_id(24084) self.assertListEqual(html_vote.member_votes, [('863', 'voted for'), ('879', 'voted for'), ('953', 'voted for'), ('914', 'voted for'), ('918', 'voted for'), ('950', 'voted for'), ('922', 'voted for'), ('924', 'voted for'), ('932', 'voted for'), ('944', 'voted for'), ('230', 'voted for'), ('862', 'voted for'), ('865', 'voted for'), ('881', 'voted for'), ('818', 'voted for'), ('901', 'voted for'), ('216', 'voted for'), ('876', 'voted for'), ('915', 'voted for'), ('854', 'voted for'), ('941', 'voted for'), ('871', 'voted for')])