From b84076ec3bbdd3e00d748f6d1a228d742bffc045 Mon Sep 17 00:00:00 2001 From: James Graham Date: Wed, 24 Jun 2020 16:29:50 +0100 Subject: [PATCH] refactor: Make Person.discipline free text See #33 --- people/forms.py | 2 +- .../0021_refactor_person_disciplines.py | 61 +++++++++++++++++++ people/models/person.py | 22 +------ people/templates/people/person/detail.html | 6 +- 4 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 people/migrations/0021_refactor_person_disciplines.py diff --git a/people/forms.py b/people/forms.py index 267c2c2..482a086 100644 --- a/people/forms.py +++ b/people/forms.py @@ -25,7 +25,7 @@ class PersonForm(forms.ModelForm): 'organisation', 'organisation_started_date', 'job_title', - 'discipline', + 'disciplines', 'role', 'themes', ] diff --git a/people/migrations/0021_refactor_person_disciplines.py b/people/migrations/0021_refactor_person_disciplines.py new file mode 100644 index 0000000..5f7da69 --- /dev/null +++ b/people/migrations/0021_refactor_person_disciplines.py @@ -0,0 +1,61 @@ +# Generated by Django 2.2.10 on 2020-06-24 14:19 + +from django.db import migrations, models + + +def migrate_forward(apps, schema_editor): + Person = apps.get_model('people', 'Person') + + for person in Person.objects.all(): + try: + person.disciplines = person.discipline.name + person.save() + + except AttributeError: + pass + + +def migrate_backward(apps, schema_editor): + Person = apps.get_model('people', 'Person') + Discipline = apps.get_model('people', 'Discipline') + + for person in Person.objects.all(): + try: + discipline_str = person.disciplines.split(',')[0] + + except AttributeError: + pass + + else: + # Returns None if not found - doesn't raise exception + discipline = Discipline.objects.filter(name=discipline_str).first() + + if not discipline: + discipline = Discipline.objects.create( + name=discipline_str, + code=discipline_str + if len(discipline_str) < 15 else discipline_str[:15]) + + person.discipline = discipline + person.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0020_person_organisation_started_date'), + ] + + operations = [ + migrations.AddField( + model_name='person', + name='disciplines', + field=models.CharField(blank=True, max_length=255, null=True), + ), + migrations.RunPython(migrate_forward, migrate_backward), + migrations.RemoveField( + model_name='person', + name='discipline', + ), + migrations.DeleteModel(name='Discipline', ), + ] diff --git a/people/models/person.py b/people/models/person.py index 56c88ea..6127f66 100644 --- a/people/models/person.py +++ b/people/models/person.py @@ -19,7 +19,6 @@ __all__ = [ 'User', 'Organisation', 'Role', - 'Discipline', 'Theme', 'Person', ] @@ -82,19 +81,6 @@ class Role(models.Model): return self.name -class Discipline(models.Model): - """ - Discipline within which a :class:`Person` works. - """ - name = models.CharField(max_length=255, blank=False, null=False) - - #: Short code using system such as JACS 3 - code = models.CharField(max_length=15, blank=True, null=False) - - def __str__(self) -> str: - return self.name - - class Theme(models.Model): """ Project theme within which a :class:`Person` works. @@ -179,12 +165,8 @@ class Person(models.Model): #: Job title this person holds within their organisation job_title = models.CharField(max_length=255, blank=True, null=False) - #: Discipline within which this person works - discipline = models.ForeignKey(Discipline, - on_delete=models.PROTECT, - related_name='people', - blank=True, - null=True) + #: Discipline(s) within which this person works + disciplines = models.CharField(max_length=255, blank=True, null=True) #: Role this person holds within the project role = models.ForeignKey(Role, diff --git a/people/templates/people/person/detail.html b/people/templates/people/person/detail.html index d2dcd9c..134f27c 100644 --- a/people/templates/people/person/detail.html +++ b/people/templates/people/person/detail.html @@ -55,9 +55,9 @@
{{ person.role }}
{% endif %} - {% if person.dispipline %} -
Discipline
-
{{ person.discipline }}
+ {% if person.disciplines %} +
Discipline(s)
+
{{ person.disciplines }}
{% endif %} {% if person.themes.exists %}