Commit 02437f32 authored by luxcem's avatar luxcem

adds copying information

parent 0f1fa231
This diff is collapsed.
# This file is part of django-representatives.
#
# django-representatives is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# django-representatives is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
from django.contrib import admin
......@@ -27,7 +46,7 @@ class PhoneInline(admin.TabularInline):
class MandateInline(admin.StackedInline):
model = Mandate
extra = 0
class RepresentativeAdmin(admin.ModelAdmin):
list_display = ('full_name', 'gender', 'birth_place')
......@@ -45,14 +64,10 @@ class RepresentativeAdmin(admin.ModelAdmin):
class MandateAdmin(admin.ModelAdmin):
list_display = ('representative', 'group', 'role', 'constituency', 'begin_date', 'end_date')
search_fields = ('representative', 'group', 'constituency')
# list_filter = ('role',)
admin.site.register(Representative, RepresentativeAdmin)
admin.site.register(Country)
admin.site.register(Mandate, MandateAdmin)
admin.site.register(Group)
admin.site.register(Constituency)
# This file is part of django-representatives.
#
# django-representatives is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# django-representatives is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
import sys
from django.db import transaction
from datetime import datetime
from representatives.models import Representative, Country, Group, Constituency
PERSONAL_FIELDS = ("first_name", "last_name", "full_name", "birth_place", "cv", "photo")
GENDER_DICT = dict(Representative.GENDER)
def export_a_representative(representative):
reps = {"id": representative.remote_id}
reps["personal"] = {field: getattr(representative, field) for field in PERSONAL_FIELDS}
reps["personal"]["gender"] = GENDER_DICT[representative.gender]
reps["personal"]["birth_date"] = representative.birth_date.isoformat().split('T')[0] if representative.birth_date else None
reps["contact"] = {}
reps["contact"]["emails"] = [{
"email": email.email,
"type": email.kind
} for email in representative.email_set.all()]
reps["contact"]["websites"] = [{
"website": website.url,
"type": website.kind
} for website in representative.website_set.all()]
reps["contact"]["phones"] = [{
"phone": phone.number,
"type": phone.kind, "address":
phone.address_id,
"id": phone.id
} for phone in representative.phone_set.all()]
reps["contact"]["address"] = [{
"id": address.id,
"country": {"name": address.country.name, "code": address.country.code},
"city": address.city,
"street": address.street,
"number": address.number,
"postcode": address.postcode,
"floor": address.floor,
"office_number": address.office_number,
"type": address.kind,
"geo": None,
"phones": [phone.id for phone in address.phone_set.all()],
} for address in representative.address_set.all()]
reps["mandates"] = [{
"name": mandate.group.name,
"type": mandate.group.kind,
"short_id": mandate.group.abbreviation,
"url_official": mandate.url,
"constituency": mandate.constituency.name,
"role": mandate.role,
"begin_date": mandate.begin_date.isoformat().split('T')[0] if mandate.begin_date else None,
"end_date": mandate.end_date.isoformat().split('T')[0] if mandate.end_date else None,
# "current": mandate.active,
} for mandate in representative.mandate_set.all()]
return reps
def export_all_representatives():
return [export_a_representative(representative) for representative in Representative.objects.all()]
def export_active_representatives():
return [export_a_representative(representative) for representative in Representative.objects.filter(active=True)]
def import_representatives_from_format(data, verbose=False):
reverted_gender_dict = {x[1]: x[0] for x in Representative.GENDER}
a = 0
end = len(data)
with transaction.atomic():
for reps in data:
a += 1
if verbose:
sys.stdout.write("%s/%s\r" % (a, end))
sys.stdout.flush()
remote_id = reps["id"]
representative = Representative.objects.filter(remote_id=remote_id)
if representative:
representative = representative[0]
else:
representative = Representative()
representative.remote_id = remote_id
representative.first_name = reps["personal"]["first_name"]
representative.last_name = reps["personal"]["last_name"]
representative.full_name = reps["personal"]["full_name"]
representative.birth_place = reps["personal"]["birth_place"]
representative.cv = reps["personal"]["cv"]
representative.photo = reps["personal"]["photo"]
if reps["personal"]["birth_date"]:
representative.birth_date = datetime.strptime(reps["personal"]["birth_date"], "%Y-%m-%d")
else:
representative.birth_date = None
representative.gender = reverted_gender_dict[reps["personal"]["gender"]]
representative.save()
representative.email_set.all().delete()
for email in reps["contact"]["emails"]:
representative.email_set.create(
email=email["email"],
kind=email["type"],
)
representative.website_set.all().delete()
for website in reps["contact"]["websites"]:
representative.website_set.create(
url=website["website"],
kind=website["type"],
)
addresses = {}
representative.address_set.all().delete()
for address in reps["contact"]["address"]:
country = Country.objects.filter(code=address["country"]["code"])
if not country:
country = Country.objects.create(
name=address["country"]["name"],
code=address["country"]["code"]
)
else:
country = country[0]
address_in_db = representative.address_set.create(
city=address["city"],
street=address["street"],
number=address["number"],
postcode=address["postcode"],
floor=address["floor"],
office_number=address["office_number"],
kind=address["type"],
country=country
)
for phone in address["phones"]:
addresses[phone] = address_in_db
representative.phone_set.all().delete()
for phone in reps["contact"]["phones"]:
representative.phone_set.create(
number=phone["phone"],
kind=phone["type"],
address=addresses[phone["id"]]
)
representative.mandate_set.all().delete()
for mandate in reps["mandates"]:
constituency, created = Constituency.objects.get_or_create(
name=mandate['constituency']
)
group, created = Group.objects.get_or_create(
name=mandate['name'],
abbreviation=mandate['short_id'],
kind=mandate['type']
)
representative.mandate_set.create(
group=group,
constituency=constituency,
url=mandate["url_official"],
role=mandate["role"],
begin_date=mandate["begin_date"],
end_date=mandate["end_date"],
# active=mandate["current"],
)
representative.save()
if verbose:
sys.stdout.write("\n")
# This file is part of django-representatives.
#
# django-representatives is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# django-representatives is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
import json
from django.core.management.base import BaseCommand
from representatives.utils import export_all_representatives
class Command(BaseCommand):
args = '<poll_id poll_id ...>'
help = 'Closes the specified poll for voting'
def handle(self, *args, **options):
print json.dumps(export_all_representatives(), indent=4)
# This file is part of django-representatives.
#
# django-representatives is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# django-representatives is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
import json
import urllib2
# from urllib2 import urlopen
from urllib2 import urlopen
from django.core.management.base import BaseCommand
from django.conf import settings
......@@ -21,5 +41,5 @@ class Command(BaseCommand):
'http://compotista.mm.staz.be')
url = compotista_server + "/latest/"
import_representatives_from_format(
json.load(urllib2.urlopen(url)),
json.load(urlopen(url)),
verbose=verbose)
# This file is part of django-representatives.
#
# django-representatives is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# django-representatives is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
import sys
import json
from django.core.management.base import BaseCommand
from representatives.utils import import_representatives_from_format
......
# This file is part of django-representatives.
#
# django-representatives is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# django-representatives is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
from django.db import models
......@@ -35,7 +54,7 @@ class Representative(models.Model):
def gender_as_str(self):
genders = {0: 'N/A', 1: 'F', 2: 'M'}
return genders[self.gender]
def __unicode__(self):
return self.full_name
......@@ -115,4 +134,3 @@ class Mandate(models.Model):
begin_date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)
url = models.URLField()
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
# This file is part of django-representives.
#
# compotista is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# compotista is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with Foobar.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Laurent Peuch <cortex@worlddomination.be>
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
# coding: utf-8
import sys
from django.db import transaction
......@@ -66,11 +87,9 @@ def export_a_representative(representative):
def export_all_representatives():
return [export_a_representative(representative) for representative in Representative.objects.all()]
def export_active_representatives():
return [export_a_representative(representative) for representative in Representative.objects.filter(active=True)]
def import_representatives_from_format(data, verbose=False):
reverted_gender_dict = {x[1]: x[0] for x in Representative.GENDER}
a = 0
......
# Create your views here.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment