From ef3b68f1e27e7be91a37a611a2d33bd1781489ac Mon Sep 17 00:00:00 2001 From: Merlin Buczek Date: Mon, 2 Sep 2019 11:09:31 +0200 Subject: [PATCH] Multiple positions per contact --- .../migrations/0024_auto_20190902_1059.py | 32 +++++++++++++++++++ mpicms/personal/models.py | 24 ++++++++++++-- mpicms/personal/wagtail_hooks.py | 10 ++++-- mpicms/templates/personal/contact_list.html | 6 ++-- 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 mpicms/personal/migrations/0024_auto_20190902_1059.py diff --git a/mpicms/personal/migrations/0024_auto_20190902_1059.py b/mpicms/personal/migrations/0024_auto_20190902_1059.py new file mode 100644 index 0000000..1d8d0a9 --- /dev/null +++ b/mpicms/personal/migrations/0024_auto_20190902_1059.py @@ -0,0 +1,32 @@ +# Generated by Django 2.2.4 on 2019-09-02 08:59 + +from django.db import migrations, models +import django.db.models.deletion +import mpicms.personal.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('personal', '0023_auto_20190729_1130'), + ] + + operations = [ + migrations.RemoveField( + model_name='contact', + name='position', + ), + migrations.CreateModel( + name='ContactPositions', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sort_order', models.IntegerField(blank=True, editable=False, null=True)), + ('contact', mpicms.personal.models.FilterableParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='positions', to='personal.Contact')), + ('position', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contacts', to='personal.Position', verbose_name='positions')), + ], + options={ + 'ordering': ['sort_order'], + 'abstract': False, + }, + ), + ] diff --git a/mpicms/personal/models.py b/mpicms/personal/models.py index deb23fb..3b71e6e 100644 --- a/mpicms/personal/models.py +++ b/mpicms/personal/models.py @@ -37,6 +37,25 @@ class ContactGroups(Orderable, models.Model): ] +class ContactPositions(Orderable, models.Model): + """ + This defines the relationship between the `Position` within the `Contact` model below. + """ + contact = FilterableParentalKey( + 'Contact', related_name=_('positions'), on_delete=models.CASCADE + ) + position = models.ForeignKey( + 'personal.Position', + related_name='contacts', + on_delete=models.CASCADE, + verbose_name=_('positions') + ) + + panels = [ + SnippetChooserPanel('position'), + ] + + class ContactManager(models.Manager): def get_queryset(self): return super().get_queryset().filter(is_active=True) @@ -80,7 +99,6 @@ class Contact(index.Indexed, ClusterableModel): email = models.EmailField(_("email"), blank=True) phone = models.CharField(_("phone number"), blank=True, max_length=50) room = models.CharField(_("room"), max_length=25, blank=True) - position = models.ForeignKey('personal.Position', verbose_name=_("position"), blank=True, null=True, on_delete=models.SET_NULL) is_active = models.BooleanField(_("is active"), default=True) priority = models.PositiveSmallIntegerField( _("priority"), blank=True, default=0, validators=[MaxValueValidator(999)], @@ -94,7 +112,9 @@ class Contact(index.Indexed, ClusterableModel): FieldPanel('first_name'), FieldPanel('last_name'), ], heading='Name'), - SnippetChooserPanel('position'), + InlinePanel( + 'positions', label="Positions", + panels=None), FieldPanel('email'), FieldPanel('phone'), FieldPanel('room'), diff --git a/mpicms/personal/wagtail_hooks.py b/mpicms/personal/wagtail_hooks.py index bac8da1..263a84d 100644 --- a/mpicms/personal/wagtail_hooks.py +++ b/mpicms/personal/wagtail_hooks.py @@ -33,9 +33,9 @@ class ContactAdmin(ModelAdmin): model = Contact menu_label = _('Persons') menu_icon = 'user' - list_display = ['title', 'first_name', 'last_name', 'position', 'email', 'phone', 'room', 'get_groups'] - list_filter = ['groups__group', 'is_active', 'position'] - search_fields = ['title', 'first_name', 'last_name', 'position__title', 'email', 'phone', 'room'] + list_display = ['title', 'first_name', 'last_name', 'get_positions', 'email', 'phone', 'room', 'get_groups'] + list_filter = ['groups__group', 'is_active', 'positions__position'] + search_fields = ['title', 'first_name', 'last_name', 'email', 'phone', 'room'] edit_view_class = ContactEditView inspect_view_class = ContactInspectView @@ -45,6 +45,10 @@ def get_groups(self, obj): return ", ".join([group.__str__() for group in Group.objects.filter(contacts__in=obj.groups.all()).distinct()]) get_groups.short_description = _('Groups') + def get_positions(self, obj): + return ", ".join([position.__str__() for position in Position.objects.filter(contacts__in=obj.positions.all()).distinct()]) + get_groups.short_description = _('Positions') + def get_queryset(self, request): qs = self.model._default_manager.include_inactive() ordering = self.get_ordering(request) diff --git a/mpicms/templates/personal/contact_list.html b/mpicms/templates/personal/contact_list.html index 7c19514..97cd0d6 100644 --- a/mpicms/templates/personal/contact_list.html +++ b/mpicms/templates/personal/contact_list.html @@ -54,7 +54,7 @@

{% trans 'Contact List' %}

{% trans 'Email' %} {% trans 'Phone' %} {% trans 'Room' %} - {% trans 'Position' %} + {% trans 'Positions' %} {% trans 'Groups' %} @@ -67,7 +67,7 @@

{% trans 'Contact List' %}

{{ contact.email }} {{ contact.phone }} {{ contact.room }} - {% if contact.position %}{{ contact.position }}{% endif %} + {% for contactposition in contact.positions.all %}{{ contactposition.position }} {% endfor %} {% for contactgroup in contact.groups.all %}{{ contactgroup.group }} {% endfor %} {% endfor %} @@ -77,7 +77,7 @@

{% trans 'Contact List' %}