Archive non-imported students in the same category

This commit is contained in:
Claude Paroz 2018-07-13 15:28:31 +02:00
parent c0074d186d
commit d9099f039b
2 changed files with 24 additions and 3 deletions

View file

@ -472,6 +472,7 @@ class ImportTests(TestCase):
)
# An existing student, klass should be changed.
Student.objects.create(ext_id=11111, first_name="Séraphin", last_name="Lampion", klass=k1)
Student.objects.create(ext_id=44444, first_name="Tryphon", last_name="Tournesol", klass=k1)
path = os.path.join(os.path.dirname(__file__), 'test_files', 'CLOEE2_Export_FE_2018.xlsx')
self.client.login(username='me', password='mepassword')
@ -505,6 +506,9 @@ class ImportTests(TestCase):
self.assertEqual(student_cand.option_ase.name, 'Accompagnement des enfants')
self.assertEqual(student_cand.corporation.name, 'Accueil Haut les mains')
self.assertFalse(student_cand.dispense_eps)
# Tournesol was archived
stud_arch = Student.objects.get(ext_id=44444)
self.assertTrue(stud_arch.archived)
def test_import_hp(self):
teacher = Teacher.objects.create(

View file

@ -22,7 +22,7 @@ from django.views.generic import FormView
from candidats.models import Candidate
from ..forms import StudentImportForm, UploadHPFileForm, UploadReportForm
from ..models import (
Corporation, CorpContact, Course, Klass, Option, Student, Teacher,
Corporation, CorpContact, Course, Klass, Option, Section, Student, Teacher,
)
from ..utils import is_int
@ -148,6 +148,12 @@ class StudentImportView(ImportViewBase):
obj_created = obj_modified = 0
err_msg = []
seen_students_ids = set()
fe_students_ids = set(
Student.objects.filter(
klass__section__in=[s for s in Section.objects.all() if s.is_fe()]
).values_list('ext_id', flat=True)
)
for line in up_file:
student_defaults = {
val: strip(line[key]) for key, val in self.student_mapping.items()
@ -200,8 +206,19 @@ class StudentImportView(ImportViewBase):
Student.objects.create(**defaults)
obj_created += 1
# FIXME: implement arch_staled
return {'created': obj_created, 'modified': obj_modified, 'errors': err_msg}
# Archive students who have not been exported
rest = fe_students_ids - seen_students_ids
archived = 0
for student_id in rest:
st = Student.objects.get(ext_id=student_id)
st.archived = True
st.save()
archived += 1
return {
'created': obj_created, 'modified': obj_modified, 'archived': archived,
'errors': err_msg,
}
def get_corporation(self, corp_values):
if corp_values['ext_id'] == '':