Skip to content

Commit

Permalink
Multiple positions per contact
Browse files Browse the repository at this point in the history
  • Loading branch information
Merlin Buczek committed Sep 2, 2019
1 parent 95740f6 commit ef3b68f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
32 changes: 32 additions & 0 deletions mpicms/personal/migrations/0024_auto_20190902_1059.py
Original file line number Diff line number Diff line change
@@ -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,
},
),
]
24 changes: 22 additions & 2 deletions mpicms/personal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)],
Expand All @@ -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'),
Expand Down
10 changes: 7 additions & 3 deletions mpicms/personal/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions mpicms/templates/personal/contact_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h1 class="title has-text-centered">{% trans 'Contact List' %}</h1>
<th class="sort" data-sort="email"><a>{% trans 'Email' %}</a></th>
<th class="sort" data-sort="phone"><a>{% trans 'Phone' %}</a></th>
<th class="sort" data-sort="room"><a>{% trans 'Room' %}</a></th>
<th class="sort" data-sort="position"><a>{% trans 'Position' %}</a></th>
<th class="sort" data-sort="positions"><a>{% trans 'Positions' %}</a></th>
<th class="sort" data-sort="groups"><a>{% trans 'Groups' %}</a></th>
</tr>
</thead>
Expand All @@ -67,7 +67,7 @@ <h1 class="title has-text-centered">{% trans 'Contact List' %}</h1>
<td class="email">{{ contact.email }}</td>
<td class="phone">{{ contact.phone }}</td>
<td class="room">{{ contact.room }}</td>
<td class="position">{% if contact.position %}{{ contact.position }}{% endif %}</td>
<td class="positions">{% for contactposition in contact.positions.all %}{{ contactposition.position }} {% endfor %}</td>
<td class="groups">{% for contactgroup in contact.groups.all %}{{ contactgroup.group }} {% endfor %}</td>
</tr>
{% endfor %}
Expand All @@ -77,7 +77,7 @@ <h1 class="title has-text-centered">{% trans 'Contact List' %}</h1>
<script>
var options = {
listClass: 'jslist',
valueNames: [ 'degree', 'first_name', 'last_name', 'email', 'phone', 'room', 'position', 'groups' ]
valueNames: [ 'degree', 'first_name', 'last_name', 'email', 'phone', 'room', 'positions', 'groups' ]
};

var userList = new List('contacts', options);
Expand Down

0 comments on commit ef3b68f

Please sign in to comment.