diff --git a/training/management/commands/import_old_db.py b/training/management/commands/import_old_db.py index 9271844a..b0f4ae9e 100644 --- a/training/management/commands/import_old_db.py +++ b/training/management/commands/import_old_db.py @@ -87,15 +87,25 @@ class Command(BaseCommand): root = self.parse_xml(self.xml_path('Training Items.xml')) for child in root: - if child.find('active').text == '0': active = False - else: active = True - obj, created = models.TrainingItem.objects.update_or_create( - pk = int(child.find('ID').text), - reference_number = int(child.find('Item_x0020_Number').text), - name = child.find('Item_x0020_Name').text, - category = models.TrainingCategory.objects.get(pk=int(child.find('Category_x0020_ID').text)), - active = active - ) + if child.find('active').text == '0': + active = False + else: + active = True + + number = int(child.find('Item_x0020_Number').text) + name = child.find('Item_x0020_Name').text + category = models.TrainingCategory.objects.get(pk=int(child.find('Category_x0020_ID').text)) + + try: + obj, created = models.TrainingItem.objects.update_or_create( + pk = int(child.find('ID').text), + reference_number = number, + name = name, + category = category, + active = active + ) + except IntegrityError: + print("Training Item {}.{} {} has a duplicate reference number".format(category.reference_number, number, name)) if created: tally[1] += 1 @@ -174,7 +184,7 @@ class Command(BaseCommand): depString = None desc = "" - if child.find('Desc'): + if child.find('Desc') is not None: desc = child.find('Desc').text obj, created = models.TrainingLevel.objects.update_or_create( @@ -232,7 +242,7 @@ class Command(BaseCommand): for child in root: try: item = child.find('Item').text.split(".") - obj, created = models.TrainingLevelRequirement.objects.update_or_create(level=models.TrainingLevel.objects.get(pk=int(child.find('Level').text)),item=models.TrainingItem.objects.get(reference_number=item[1], category=models.TrainingCategory.objects.get(reference_number=item[0])), depth=int(child.find('Depth').text)) + obj, created = models.TrainingLevelRequirement.objects.update_or_create(level=models.TrainingLevel.objects.get(pk=int(child.find('Level').text)),item=models.TrainingItem.objects.get(active=True, reference_number=item[1], category=models.TrainingCategory.objects.get(reference_number=item[0])), depth=int(child.find('Depth').text)) if created: tally[1] += 1 diff --git a/training/migrations/0009_auto_20211221_1539.py b/training/migrations/0009_auto_20211221_1539.py new file mode 100644 index 00000000..6b1c9b51 --- /dev/null +++ b/training/migrations/0009_auto_20211221_1539.py @@ -0,0 +1,22 @@ +# Generated by Django 3.1.13 on 2021-12-21 15:39 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('training', '0008_trainingitem_active'), + ] + + operations = [ + migrations.AlterField( + model_name='trainingcategory', + name='reference_number', + field=models.CharField(max_length=3, unique=True), + ), + migrations.AlterUniqueTogether( + name='trainingitem', + unique_together={('reference_number', 'active', 'category')}, + ), + ] diff --git a/training/models.py b/training/models.py index 948a3a5e..8328ab9e 100644 --- a/training/models.py +++ b/training/models.py @@ -44,7 +44,7 @@ class Trainee(Profile, RevisionMixin): class TrainingCategory(models.Model): - reference_number = models.CharField(max_length=3) + reference_number = models.CharField(max_length=3, unique=True) name = models.CharField(max_length=50) def __str__(self): @@ -74,7 +74,7 @@ class TrainingItem(models.Model): return True class Meta: - unique_together = ["reference_number", "name", "category"] + unique_together = ["reference_number", "active", "category"] ordering = ['category__reference_number', 'reference_number'] diff --git a/training/templates/level_detail.html b/training/templates/level_detail.html index bf8774d4..5def8af7 100644 --- a/training/templates/level_detail.html +++ b/training/templates/level_detail.html @@ -58,6 +58,7 @@

Users with this level

+
@@ -79,6 +80,7 @@ {% endfor %}
+

Level Requirements