Skip to content

Commit

Permalink
personal: Change grous to ManyToManyField
Browse files Browse the repository at this point in the history
We want to split responsibilities for defining groups and assigning
groups to position. Therefore we no longer want thegroupspositions inline
panel in the contact model admin.

Make groups into a simple ManyToMany field.

Add sql code to autogenerated migration to transfer data from the old
through table "personal_contactgroups" to the new one
("personal_contact_groups").

Adapt queries.
  • Loading branch information
donald committed Feb 9, 2025
1 parent 108adbb commit 05a33eb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
22 changes: 22 additions & 0 deletions mpicms/personal/migrations/0030_auto_20250209_1143.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.1.7 on 2025-02-09 10:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('personal', '0029_auto_20250209_1010'),
]

operations = [
migrations.AddField(
model_name='contact',
name='groups',
field=models.ManyToManyField(blank=True, related_name='contacts', to='personal.Group', verbose_name='groups'),
),
migrations.RunSQL("INSERT INTO personal_contact_groups(id, contact_id, group_id) SELECT id, contact_id, group_id FROM personal_contactgroups"),
migrations.DeleteModel(
name='ContactGroups',
),
]
25 changes: 3 additions & 22 deletions mpicms/personal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,12 @@
from modelcluster.fields import ParentalKey


# still used in migrations
class FilterableParentalKey(ParentalKey):
def get_related_field(self):
return self.remote_field


class ContactGroups(Orderable, models.Model):
"""
This defines the relationship between the `Group` within the `Contact` model below.
"""
contact = FilterableParentalKey(
'Contact', related_name=_('groups'), on_delete=models.CASCADE
)
group = models.ForeignKey(
'personal.Group',
related_name='contacts',
on_delete=models.CASCADE,
verbose_name=_('group')
)

panels = [
SnippetChooserPanel('group'),
]


class ContactManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active=True)
Expand Down Expand Up @@ -146,6 +128,7 @@ class Contact(index.Indexed, ClusterableModel):
status = models.ForeignKey(Status, on_delete=models.SET_NULL, blank=True, null=True)
special_functions = models.ManyToManyField(SpecialFunction, verbose_name=_('special functions'), blank=True)
positions = models.ManyToManyField(Position, related_name="contacts", blank=True, verbose_name=_('positions'))
groups = models.ManyToManyField(Group, related_name="contacts", blank=True, verbose_name=_('groups'))

objects = ContactManager()

Expand All @@ -157,14 +140,12 @@ class Contact(index.Indexed, ClusterableModel):
FieldPanel('academic_suffix'),
], heading='Name'),
FieldPanel('status'),
FieldPanel('groups'),
FieldPanel('positions'),
FieldPanel('special_functions'),
FieldPanel('email'),
FieldPanel('phone'),
FieldPanel('room'),
InlinePanel(
'groups', label="Groups",
panels=None),
FieldPanel('is_active'),
FieldPanel('priority'),
]
Expand Down
2 changes: 1 addition & 1 deletion mpicms/personal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_queryset(self):
special_function_pk = self.request.GET.get('special_function')
qs = super().get_queryset();
if group_pk is not None and group_pk != "":
qs = qs.filter(groups__group_id=group_pk)
qs = qs.filter(groups=group_pk)

if position_pk is not None and position_pk != "":
qs = qs.filter(positions=position_pk)
Expand Down
4 changes: 2 additions & 2 deletions mpicms/personal/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class ContactAdmin(ModelAdmin):
menu_label = _('Persons')
menu_icon = 'user'
list_display = ['title', 'first_name', 'last_name', 'get_positions', 'email', 'phone', 'room', 'get_groups']
list_filter = ['groups__group', 'is_active', 'positions']
list_filter = ['groups', 'is_active', 'positions']
search_fields = ['title', 'first_name', 'last_name', 'email', 'phone', 'room']

edit_view_class = ContactEditView
inspect_view_class = ContactInspectView
delete_view_class = ContactDeleteView

def get_groups(self, obj):
return ", ".join([group.__str__() for group in Group.objects.filter(contacts__in=obj.groups.all()).distinct()])
return ", ".join([group.__str__() for group in obj.groups.all()])
get_groups.short_description = _('Groups')

def get_positions(self, obj):
Expand Down
2 changes: 1 addition & 1 deletion mpicms/templates/personal/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h1 class="title has-text-centered">{% trans 'Contact List' %}</h1>
<td class="email"><a href="mailto:{{ contact.email }}">{{ contact.email }}</a></td>
<td class="phone">{{ contact.phone }}</td>
<td class="room">{{ contact.room | add_room_links }}</td>
<td class="groups">{% for contactgroup in contact.groups.all %}{{ contactgroup.group }}<br>{% endfor %}</td>
<td class="groups">{% for group in contact.groups.all %}{{ group }}<br>{% endfor %}</td>
<td class="positions">{% for position in contact.positions.all %}{{ position }}<br>{% endfor %}</td>
<td class="special_functions">{% for specialfunction in contact.special_functions.all %}{{ specialfunction.title }}<br>{% endfor %}</td>
</tr>
Expand Down

0 comments on commit 05a33eb

Please sign in to comment.