Skip to content
Extraits de code Groupes Projets
Valider f014d769 rédigé par luxcem's avatar luxcem
Parcourir les fichiers

uses get_or_creates

parent 3a958099
Branches
Étiquettes
Aucune requête de fusion associée trouvée
......@@ -2,10 +2,8 @@ import sys
from django.db import transaction
from datetime import datetime
from django.core.exceptions import ObjectDoesNotExist
from representatives.models import Representative, Country, Group, Constituency, Mandate
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)
......@@ -155,25 +153,14 @@ def import_representatives_from_format(data, verbose=False):
representative.mandate_set.all().delete()
for mandate in reps["mandates"]:
try:
constituency = Constituency.objects.get(name=mandate['constituency'])
except ObjectDoesNotExist:
constituency = Constituency(name=mandate['constituency'])
constituency.save()
try:
group = Group.objects.get(
name=mandate['name'],
abbreviation=mandate['short_id'],
kind=mandate['type']
)
except ObjectDoesNotExist:
group = Group(
name=mandate['name'],
abbreviation=mandate['short_id'],
kind=mandate['type']
)
group.save()
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,
......@@ -185,22 +172,18 @@ def import_representatives_from_format(data, verbose=False):
active=mandate["current"],
)
# Create a country if not exist
# Create a country if it does not exist
# The representative's country is the one associated
# with the last 'country' mandate
country_mandate = representative.mandate_set.filter(
group__kind='country'
).order_by('-begin_date')[0:1].get()
try:
country = Country.objects.get(
name=country_mandate.group.name,
code=country_mandate.group.abbreviation
)
except ObjectDoesNotExist:
country = Country(
name=country_mandate.group.name,
code=country_mandate.group.abbreviation
)
country.save()
country, created = Country.objects.get_or_create(
name=country_mandate.group.name,
code=country_mandate.group.abbreviation
)
representative.country = country
representative.save()
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter