def test_find_party_dash(self): party_expected = PoliticalParty.objects.create( name='Vrijzinnig Democratische Bond', name_short='VDB') party = PoliticalParty.find_party('Vrijzinnig Democratische Bond') self.assertEqual(party, party_expected) party = PoliticalParty.find_party('Vrijzinnig-Democratische Bond') self.assertEqual(party, party_expected)
def test_create_kuzu(self): person_wikidata_id = 'Q616635' # Tunahan Kuzu parliament = Parliament.get_or_create_tweede_kamer() members = openkamer.parliament.create_parliament_member_from_wikidata_id(parliament, person_wikidata_id) self.assertEqual(len(members), 3) party_expected_0 = PoliticalParty.find_party('PvdA') party_expected_1 = PoliticalParty.find_party('GrKÖ') party_expected_2 = PoliticalParty.find_party('DENK') self.assertEqual(members[0].party, party_expected_0) self.assertEqual(members[1].party, party_expected_1) self.assertEqual(members[2].party, party_expected_2)
def test_create_parliament_member_from_wikidata_kuzu(self): person_wikidata_id = 'Q616635' # Tunahan Kuzu parliament = Parliament.get_or_create_tweede_kamer() members = openkamer.parliament.create_parliament_member_from_wikidata_id( parliament, person_wikidata_id) self.assertEqual(len(members), 3) party_expected_0 = PoliticalParty.find_party('PvdA') party_expected_1 = PoliticalParty.find_party('GrKÖ') party_expected_2 = PoliticalParty.find_party('DENK') self.assertEqual(members[0].party, party_expected_0) self.assertEqual(members[1].party, party_expected_1) self.assertEqual(members[2].party, party_expected_2)
def create_parliament_member_from_wikidata_id(parliament, person_wikidata_id): wikidata_item = wikidata.WikidataItem(person_wikidata_id) person = get_or_create_person(person_wikidata_id, wikidata_item=wikidata_item, add_initials=True) logger.info(person) positions = wikidata_item.get_parliament_positions_held() members = [] for position in positions: parliament_member = ParliamentMember.objects.create( person=person, parliament=parliament, joined=position['start_time'], left=position['end_time'] ) logger.info(parliament_member) members.append(parliament_member) if position['part_of_id']: party_item = wikidata.WikidataItem(position['part_of_id']) parties = PoliticalParty.objects.filter(wikidata_id=position['part_of_id']) if parties: party = parties[0] else: party = PoliticalParty.find_party(party_item.get_label(language='nl')) if not party: party = create_party_wikidata(wikidata_id=position['part_of_id']) PartyMember.objects.create(person=person, party=party, joined=position['start_time'], left=position['end_time']) return members
def create_parliament_member_from_wikidata_id(parliament, person_wikidata_id): wikidata_item = wikidata.WikidataItem(person_wikidata_id) person = get_or_create_person(person_wikidata_id, wikidata_item=wikidata_item, add_initials=True) logger.info(person) positions = wikidata_item.get_parliament_positions_held() members = [] for position in positions: parliament_member = ParliamentMember.objects.create( person=person, parliament=parliament, joined=position['start_time'], left=position['end_time']) logger.info(parliament_member) members.append(parliament_member) if position['part_of_id']: party_item = wikidata.WikidataItem(position['part_of_id']) parties = PoliticalParty.objects.filter( wikidata_id=position['part_of_id']) if parties: party = parties[0] else: party = PoliticalParty.find_party( party_item.get_label(language='nl')) if not party: party = create_party_wikidata( wikidata_id=position['part_of_id']) PartyMember.objects.create(person=person, party=party, joined=position['start_time'], left=position['end_time']) return members
def create_parties(): url = 'https://www.tweedekamer.nl/kamerleden/fracties' page = requests.get(url) tree = lxml.html.fromstring(page.content) rows = tree.xpath("//ul[@class='reset grouped-list']/li/a") for row in rows: columns = row.text.split('-') if len(columns) > 1: name = columns[0].strip() name_short = columns[1] name_short = re.sub(r'\(.+?\)', '', name_short).strip() else: name = columns[0] name = re.sub(r'\(.+?\)', '', name).strip() name_short = name # print('name: ' + name) # print('short: ' + name_short) if PoliticalParty.find_party(name): print('WARNING: party already exists!') else: party = PoliticalParty.objects.create(name=name, name_short=name_short) party.update_info('nl', 'nl') party.save() print('created: ' + str(party))
def create_votes_party(voting, votes): logger.info('BEGIN') for vote in votes: party = PoliticalParty.find_party(vote.party_name) if not party: wikidata_id = wikidata.search_political_party_id(vote.party_name, language='nl') name = vote.party_name if wikidata_id: item = wikidata.WikidataItem(wikidata_id) name = item.get_label('nl') if not name: logger.error('vote party has no name') assert False party = PoliticalParty.objects.create(name=name, name_short=vote.party_name, wikidata_id=wikidata_id) party.update_info(language='nl') if not vote.decision: logger.warning('vote has no decision, vote.details: ' + str(vote.details)) VoteParty.objects.create(voting=voting, party=party, party_name=vote.party_name, number_of_seats=vote.number_of_seats, decision=get_decision(vote.decision), details=vote.details, is_mistake=vote.is_mistake) logger.info('END')
def test_create_martin(self): person_wikidata_id = 'Q2801440' # Martin van Rooijen parliament = Parliament.get_or_create_tweede_kamer() members = openkamer.parliament.create_parliament_member_from_wikidata_id(parliament, person_wikidata_id) self.assertEqual(len(members), 1) party_expected = PoliticalParty.find_party('50plus') self.assertEqual(members[0].party, party_expected)
def create_vote_from_html_row(row, bill): ncol = 0 details = '' decision = '' for column in row.iter(): if column.tag == 'td': ncol += 1 if ncol == 1 and column.tag == 'a': party_name = column.text print(party_name) elif ncol == 2: number_of_seats = int(column.text) print(number_of_seats) elif ncol == 3 and column.tag == 'img': decision = Vote.FOR elif ncol == 4 and column.tag == 'img': decision = Vote.AGAINST elif ncol == 5 and column.tag == 'h4': details = column.text print(details) party = PoliticalParty.get_party(party_name) vote = Vote.objects.create(bill=bill, party=party, number_of_seats=number_of_seats, decision=decision, details=details) vote.save() return vote
def create_party(name, name_short): party = PoliticalParty.find_party(name) if party: party.delete() party = PoliticalParty.objects.create(name=name, name_short=name_short) party.update_info(language='nl') return party
def create_party(name, name_short): party = PoliticalParty.find_party(name) if party: party.delete() party = PoliticalParty.objects.create(name=name, name_short=name_short) party.update_info(language='nl') return party
def test_create_parliament_member_from_wikidata_martin(self): person_wikidata_id = 'Q2801440' # Martin van Rooijen parliament = Parliament.get_or_create_tweede_kamer() members = openkamer.parliament.create_parliament_member_from_wikidata_id( parliament, person_wikidata_id) self.assertEqual(len(members), 1) party_expected = PoliticalParty.find_party('50plus') self.assertEqual(members[0].party, party_expected)
def test_create_paul(self): person_wikidata_id = 'Q18169519' # Paul Smeulders Person.objects.create(forename='Paul', surname='Smeulders', wikidata_id=person_wikidata_id) parliament = Parliament.get_or_create_tweede_kamer() members = openkamer.parliament.create_parliament_member_from_wikidata_id( parliament, person_wikidata_id) self.assertEqual(len(members), 1) member = members[0] party_expected = PoliticalParty.find_party('GroenLinks') self.assertEqual(member.party, party_expected) self.assertEqual(member.person.initials, '') member.person.update_info() self.assertEqual(member.person.initials, 'P.H.M.')
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) votes_filter = PartyVotesFilter(self.request.GET, queryset=PartyVoteBehaviour.objects.all()) n_votes_total = 0 stats = [] parties = PoliticalParty.sort_by_current_seats(PoliticalParty.objects.all()) for party in parties: stat = PartyVoteBehaviour.get_stats_party_for_qs(party, votes_filter.qs) stats.append(stat) n_votes_total += stat['n_votes'] context['stats'] = stats context['n_votes'] = n_votes_total context['filter'] = votes_filter context['page_stats_votings_parties'] = True return context
def test_create_paul(self): person_wikidata_id = 'Q18169519' # Paul Smeulders Person.objects.create( forename='Paul', surname='Smeulders', wikidata_id=person_wikidata_id ) parliament = Parliament.get_or_create_tweede_kamer() members = openkamer.parliament.create_parliament_member_from_wikidata_id(parliament, person_wikidata_id) self.assertEqual(len(members), 1) member = members[0] party_expected = PoliticalParty.find_party('GroenLinks') self.assertEqual(member.party, party_expected) self.assertEqual(member.person.initials, '') member.person.update_info() self.assertEqual(member.person.initials, 'P.H.M.')
def create_votes_party(self, voting, stemmingen): logger.info('BEGIN') for stemming in stemmingen: fractie_name = stemming.actor_fractie party = PoliticalParty.find_party(fractie_name) if not party and self.do_create_missing_party: party = self.create_missing_party(stemming) if not stemming.soort: logger.warning('vote has no decision, vote.details: ' + str(stemming.soort)) VoteParty.objects.create( voting=voting, party=party, party_name=fractie_name, number_of_seats=stemming.fractie_size, decision=self.get_decision(stemming.soort), details='', is_mistake=stemming.vergissing if stemming.vergissing is not None else False ) logger.info('END')
def create_votes_party(self, voting, stemmingen): logger.info('BEGIN') for stemming in stemmingen: fractie_name = stemming.actor_fractie if stemming.actor_fractie else stemming.actor_naam party = PoliticalParty.find_party(fractie_name) if not party and self.do_create_missing_party: party = self.create_missing_party(stemming) if not stemming.soort: logger.warning('vote has no decision, vote.details: ' + str(stemming.soort)) VoteParty.objects.create( voting=voting, party=party, party_name=fractie_name, number_of_seats=stemming.fractie_size, decision=self.get_decision(stemming.soort), details='', is_mistake=stemming.vergissing if stemming.vergissing is not None else False ) logger.info('END')
def create_members(): url = 'http://www.tweedekamer.nl/kamerleden/alle_kamerleden' page = requests.get(url) tree = lxml.html.fromstring(page.content) parliament = get_or_create_tweede_kamer() rows = tree.xpath("//tbody/tr") for row in rows: columns = row.xpath("td") if len(columns) == 8: surname = columns[0][0].text.split(',')[0] print(surname) prefix = columns[0][0].text.split('.')[-1].strip() forename = columns[1][0].text if Person.person_exists(forename, surname): person = Person.objects.get(forename=forename, surname=surname) else: person = Person.objects.create( forename=forename, surname=surname, surname_prefix=prefix, ) party_name = columns[2][0].text party = PoliticalParty.get_party(party_name) # residence = columns[3][0].text # age = columns[4][0][0].text # sex = columns[5][0].text # assert age is not None # if sex == 'Man': # sex = Member.MALE # elif sex == 'Vrouw': # sex = Member.FEMALE party_member = PartyMember.objects.create(person=person, party=party) parliament_member = ParliamentMember.objects.create(person=person, parliament=parliament) print("new person: " + str(person)) print("new party member: " + str(party_member)) print("new parliament member: " + str(parliament_member))
def test_find_party(self): name = 'Socialistische Partij' name_short = 'SP' party_expected = PoliticalParty.objects.create(name=name, name_short=name_short) party = PoliticalParty.find_party(name) self.assertEqual(party, party_expected) party = PoliticalParty.find_party(name_short) self.assertEqual(party, party_expected) party = PoliticalParty.find_party('sp') self.assertEqual(party, party_expected) party = PoliticalParty.find_party('SocialIstische parTij') self.assertEqual(party, party_expected) name = 'Group K/Ö' name_short = 'GrKO' party_expected = PoliticalParty.objects.create(name=name, name_short=name_short) party = PoliticalParty.find_party('GrKÖ') self.assertEqual(party, party_expected) party = PoliticalParty.find_party('GrKO') self.assertEqual(party, party_expected)
def test_find_party(self): name = 'Socialistische Partij' name_short = 'SP' party_expected = PoliticalParty.objects.create(name=name, name_short=name_short) party = PoliticalParty.find_party(name) self.assertEqual(party, party_expected) party = PoliticalParty.find_party(name_short) self.assertEqual(party, party_expected) party = PoliticalParty.find_party('sp') self.assertEqual(party, party_expected) party = PoliticalParty.find_party('SocialIstische parTij') self.assertEqual(party, party_expected) name = 'Group K/Ö' name_short = 'GrKO' party_expected = PoliticalParty.objects.create(name=name, name_short=name_short) party = PoliticalParty.find_party('GrKÖ') self.assertEqual(party, party_expected) party = PoliticalParty.find_party('GrKO') self.assertEqual(party, party_expected)
def test_find_party_dash(self): party_expected = PoliticalParty.objects.create(name='Vrijzinnig Democratische Bond', name_short='VDB') party = PoliticalParty.find_party('Vrijzinnig Democratische Bond') self.assertEqual(party, party_expected) party = PoliticalParty.find_party('Vrijzinnig-Democratische Bond') self.assertEqual(party, party_expected)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) parties = PoliticalParty.objects.exclude(slug__exact='') parties = PoliticalParty.sort_by_current_seats(parties) context['parties'] = parties return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) parties = PoliticalParty.objects.all() parties = PoliticalParty.sort_by_current_seats(parties) context['parties'] = parties return context
def set_derived(self): self.party = PoliticalParty.find_party(self.party_name) self.save()
def set_derived(self): self.party = PoliticalParty.find_party(self.party_name) self.save()