Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Add possibility to filter by group. #145

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions aldryn_people/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class PeoplePlugin(CMSPluginBase):
'people',
)
}),
(_('Filter'), {
'description': _('Select and arrange specific groups, or leave '
'blank to not use a filter at all.'),
'fields': (
'groups',
)
}),
(_('Options'), {
'fields': (
('group_by_group', 'show_ungrouped', ),
Expand All @@ -49,12 +56,17 @@ class PeoplePlugin(CMSPluginBase):
})
)

def group_people(self, people):
def group_people(self, people, selected_groups):
groups = defaultdict(list)

for person in people:
for group in person.groups.all():
groups[group].append(person)
if not selected_groups:
for group in person.groups.all():
groups[group].append(person)
else:
group_list = [group.id for group in selected_groups]
for group in person.groups.filter(id__in=group_list):
groups[group].append(person)

# Fixes a template resolution-related issue. See:
# http://stackoverflow.com/questions/4764110/django-template-cant-loop-defaultdict # noqa
Expand All @@ -65,6 +77,7 @@ def render(self, context, instance, placeholder):
people = instance.get_selected_people()
if not people:
people = models.Person.objects.all()
sel_groups = instance.get_selected_groups()
valid_languages = get_valid_languages(
DEFAULT_APP_NAMESPACE, instance.language, context['request'])
people = people.translated(*valid_languages)
Expand All @@ -73,10 +86,13 @@ def render(self, context, instance, placeholder):
self.render_template = self.TEMPLATE_NAME % instance.style

context['instance'] = instance
context['people'] = people
if sel_groups:
context['people'] = people.filter(groups__in=sel_groups)
else:
context['people'] = people

if instance.group_by_group:
context['people_groups'] = self.group_people(people)
context['people_groups'] = self.group_people(people, sel_groups)
if instance.show_ungrouped:
groupless = people.filter(groups__isnull=True)
else:
Expand Down
21 changes: 21 additions & 0 deletions aldryn_people/migrations/0019_peopleplugin_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-08-07 19:34
from __future__ import unicode_literals

import aldryn_common.admin_fields.sortedm2m
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('aldryn_people', '0018_auto_20160802_1852'),
]

operations = [
migrations.AddField(
model_name='peopleplugin',
name='groups',
field=aldryn_common.admin_fields.sortedm2m.SortedM2MModelField(blank=True, help_text='Select and arrange specific groups, or, leave blank to select all.', to='aldryn_people.Group'),
),
]
9 changes: 9 additions & 0 deletions aldryn_people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ class BasePeoplePlugin(CMSPlugin):
'select all.')
)

groups = SortedM2MModelField(
Group, blank=True,
help_text=_('Select and arrange specific groups, or, leave blank to '
'select all.')
)

# Add an app namespace to related_name to avoid field name clashes
# with any other plugins that have a field with the same name as the
# lowercase of the class name of this model.
Expand All @@ -378,6 +384,9 @@ class Meta:
def copy_relations(self, oldinstance):
self.people = oldinstance.people.all()

def get_selected_groups(self):
return self.groups.select_related()

def get_selected_people(self):
return self.people.select_related('visual')

Expand Down