Update corporation ext_id during imports if applicable
This commit is contained in:
parent
b84289e25c
commit
178d88eb54
2 changed files with 22 additions and 5 deletions
|
|
@ -465,6 +465,8 @@ class ImportTests(TestCase):
|
|||
assc = Section.objects.create(name='ASSC')
|
||||
Option.objects.create(name='Accompagnement des enfants')
|
||||
k1 = Klass.objects.create(name='1ASSCFEa', section=assc, level=lev1)
|
||||
# Corporation without ext_id will have its ext_id added.
|
||||
corp = Corporation.objects.create(name='Centre Cantonal de peinture', city='Marin-Epagnier')
|
||||
inst = CorpContact.objects.create(first_name="Roberto", last_name="Rastapopoulos")
|
||||
Candidate.objects.create(
|
||||
first_name="Bianca", last_name="Castafiore", deposite_date="2018-06-06",
|
||||
|
|
@ -511,6 +513,9 @@ class ImportTests(TestCase):
|
|||
# Tournesol was archived
|
||||
stud_arch = Student.objects.get(ext_id=44444)
|
||||
self.assertTrue(stud_arch.archived)
|
||||
# Corporation.ext_id is updated
|
||||
corp.refresh_from_db()
|
||||
self.assertEqual(corp.ext_id, 100)
|
||||
|
||||
def test_import_students_ESTER(self):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from tabimport import CSVImportedFile, FileFactory
|
|||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.core.files import File
|
||||
from django.db import transaction
|
||||
from django.db import IntegrityError, transaction
|
||||
from django.db.models import Value
|
||||
from django.db.models.functions import Concat
|
||||
from django.http import HttpResponseRedirect
|
||||
|
|
@ -248,10 +248,22 @@ class StudentImportView(ImportViewBase):
|
|||
return None
|
||||
if 'city' in corp_values and is_int(corp_values['city'][:4]):
|
||||
corp_values['pcode'], _, corp_values['city'] = corp_values['city'].partition(' ')
|
||||
corp, created = Corporation.objects.get_or_create(
|
||||
ext_id=corp_values['ext_id'],
|
||||
defaults=corp_values
|
||||
)
|
||||
try:
|
||||
corp, created = Corporation.objects.get_or_create(
|
||||
ext_id=corp_values['ext_id'],
|
||||
defaults=corp_values
|
||||
)
|
||||
except IntegrityError:
|
||||
# It may happen that the corporation exists (name and city are enforced unique)
|
||||
# but without the ext_id. In that case, we update the ext_id.
|
||||
try:
|
||||
corp = Corporation.objects.get(name=corp_values['name'], city=corp_values['city'])
|
||||
if corp.ext_id:
|
||||
raise
|
||||
corp.ext_id = corp_values['ext_id']
|
||||
corp.save()
|
||||
except Corporation.DoesNotExist:
|
||||
raise
|
||||
return corp
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue