diff --git a/stages/migrations/0009_student_archived_text.py b/stages/migrations/0009_student_archived_text.py new file mode 100644 index 0000000..3e004f2 --- /dev/null +++ b/stages/migrations/0009_student_archived_text.py @@ -0,0 +1,21 @@ +# Generated by Django 1.9.1 on 2016-01-29 17:52 +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stages', '0008_availability_priority'), + ] + + operations = [ + migrations.AlterModelOptions( + name='training', + options={'ordering': ('-availability__period',), 'verbose_name': 'Stage'}, + ), + migrations.AddField( + model_name='student', + name='archived_text', + field=models.TextField(blank=True), + ), + ] diff --git a/stages/models.py b/stages/models.py index db6fe85..16d0c4d 100644 --- a/stages/models.py +++ b/stages/models.py @@ -1,4 +1,5 @@ from datetime import date, timedelta +import json from django.db import models @@ -68,6 +69,7 @@ class Student(models.Model): email = models.EmailField(verbose_name='Courriel', blank=True) klass = models.ForeignKey(Klass, verbose_name='Classe') archived = models.BooleanField(default=False, verbose_name='Archivé') + archived_text = models.TextField(blank=True) support_tabimport = True @@ -77,6 +79,17 @@ class Student(models.Model): def __str__(self): return '%s %s' % (self.last_name, self.first_name) + def save(self, **kwargs): + if self.archived and not self.archived_text: + # Fill archived_text with training data, JSON-formatted + trainings = [ + tr.serialize() for tr in self.training_set.all().select_related('availability') + ] + self.archived_text = json.dumps(trainings) + if self.archived_text and not self.archived: + self.archived_text = "" + super().save(**kwargs) + def age_at(self, date_): """Return age of student at `date_` time, as a string.""" age = (date.today() - self.birth_date) / timedelta(days=365.2425) @@ -249,3 +262,17 @@ class Training(models.Model): def __str__(self): return '%s chez %s (%s)' % (self.student, self.availability.corporation, self.availability.period) + + def serialize(self): + """ + Compute a summary of the training as a dict representation (for archiving purpose). + """ + return { + 'period': str(self.availability.period), + 'corporation': str(self.availability.corporation), + 'referent': str(self.referent), + 'comment': self.comment, + 'contact': str(self.availability.contact), + 'comment_avail': self.availability.comment, + 'domain': str(self.availability.domain), + } diff --git a/stages/tests.py b/stages/tests.py index 8e583b1..eda46fb 100644 --- a/stages/tests.py +++ b/stages/tests.py @@ -98,6 +98,21 @@ class StagesTest(TestCase): avail = Availability.objects.get(pk=2) self.assertEqual(avail.training.student, student) + def test_archived_trainings(self): + """ + Once a student is archived, training data are serialized in its archive_text field. + """ + st = Student.objects.get(first_name="Albin") + st.archived = True + st.save() + self.assertGreater(len(st.archived_text), 0) + arch = eval(st.archived_text) + self.assertEqual(arch[0]['corporation'], "Centre pédagogique XY, 2500 Moulineaux") + # Un-archiving should delete archived_text content + st.archived = False + st.save() + self.assertEqual(st.archived_text, "") + class PeriodTest(TestCase): def setUp(self):