Skip to content

Commit

Permalink
Group Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Sep 18, 2020
1 parent f60a548 commit 0b90922
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
3 changes: 2 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
'modelcluster',
'taggit',
'rest_framework',
'treebeard',

]
LOCAL_APPS = [
'mpicms.base.apps.BaseAppConfig',
Expand Down Expand Up @@ -188,7 +190,6 @@
# SECURITY
# ------------------------------------------------------------------------------
SESSION_COOKIE_HTTPONLY = True
CSRF_COOKIE_HTTPONLY = True
SECURE_BROWSER_XSS_FILTER = True
X_FRAME_OPTIONS = 'DENY'

Expand Down
10 changes: 10 additions & 0 deletions mpicms/personal/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib import admin
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory
from mpicms.personal.models import Group

class GroupAdmin(TreeAdmin):
form = movenodeform_factory(Group)
pass

admin.site.register(Group, GroupAdmin)
53 changes: 53 additions & 0 deletions mpicms/personal/migrations/0027_auto_20200918_1218.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 3.1 on 2020-09-18 10:18

from django.db import migrations, models
from treebeard.mp_tree import MP_Node


def update_path(apps, schema_editor):

class Node(MP_Node):
# may overwrite, should match new model:
# steplen = 4
# alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# node_order_by = []
pass;

Group = apps.get_model("personal","Group")
db_alias = schema_editor.connection.alias

for i, group in enumerate(Group.objects.all(), 1):
group.path = Node._get_path(None, 1, i)
group.save(using=db_alias)

class Migration(migrations.Migration):

dependencies = [
('personal', '0026_auto_20200813_1342'),
]

operations = [
migrations.AddField(
model_name='group',
name='depth',
field=models.PositiveIntegerField(default=1),
preserve_default=False,
),
migrations.AddField(
model_name='group',
name='numchild',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='group',
name='path',
field=models.CharField(default='', max_length=255),
preserve_default=False,
),
migrations.RunPython(update_path, migrations.RunPython.noop),
migrations.AlterField(
model_name='group',
name='path',
field=models.CharField(max_length=255, unique=True),
),
]
12 changes: 7 additions & 5 deletions mpicms/personal/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils.safestring import mark_safe
from django.core.validators import MaxValueValidator

from wagtail.core.models import Orderable
Expand All @@ -11,6 +12,8 @@
from modelcluster.models import ClusterableModel
from modelcluster.fields import ParentalKey

from treebeard.mp_tree import MP_Node


class FilterableParentalKey(ParentalKey):
def get_related_field(self):
Expand Down Expand Up @@ -152,12 +155,9 @@ class Meta: # noqa


@register_snippet
class Group(index.Indexed, ClusterableModel):
class Group(index.Indexed, ClusterableModel, MP_Node):
slug = models.CharField(_("slug"), max_length=254)
name = models.CharField(_("name"), max_length=254, blank=True)
priority = models.PositiveSmallIntegerField(
_("priority"), blank=True, default=0, validators=[MaxValueValidator(99)],
help_text=_("Priority from 0-99 to determine the sorting order."))

panels = [
FieldPanel('name'),
Expand All @@ -176,7 +176,9 @@ def members(self):
def __str__(self):
return self.name or self.slug

def get_depth_fill(self):
return (mark_safe(" " * ((self.get_depth()-1) * 2)))

class Meta: # noqa
verbose_name = 'Group'
verbose_name_plural = 'Groups'
ordering = ['-priority']
2 changes: 1 addition & 1 deletion mpicms/personal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_queryset(self):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['groups'] = Group.objects.all().order_by("name")
context['groups'] = Group.get_tree()
group_pk = self.request.GET.get('group')
context['selected_group_pk'] = group_pk
context['selected_group'] = get_object_or_404(Group, pk=group_pk) if group_pk else ""
Expand Down
17 changes: 8 additions & 9 deletions mpicms/templates/personal/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<h1 class="title has-text-centered">{% trans 'Contact List' %}</h1>


<div class="contact_search">
<input id="search_field" type="text" class="search input" placeholder="{% trans 'Search contacts' %}"/>

Expand All @@ -15,17 +16,15 @@ <h1 class="title has-text-centered">{% trans 'Contact List' %}</h1>
</button>
</div>
<div class="dropdown-menu" role="menu">
<hr class="dropdown-divider">
<a href="?" class="dropdown-item">
<em>{% trans 'Reset filter' %}</em>
</a>
<div class="dropdown-content">
{% for group in groups %}
<a href="?group={{ group.pk }}" class="{% if group.pk|stringformat:'d' == selected_group_pk %}is-active {% endif %}dropdown-item">
{{ group }}
<a href="?" class="dropdown-item">
<em>{% trans 'Reset filter' %}</em>
</a>
{% endfor %}

{% for group in groups %}
<a href="?group={{ group.pk }}" class="{% if group.pk|stringformat:'d' == selected_group_pk %}is-active {% endif %}dropdown-item">
{{group.get_depth_fill }} {{ group }}
</a>
{% endfor %}
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ markdown
pygments
ics
django-auth-ldap
django-treebeard
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ django-filter==2.3.0 # via wagtail
django-modelcluster==5.0.2 # via wagtail
django-modeltranslation==0.15.1 # via wagtail-modeltranslation
django-taggit==1.3.0 # via wagtail
django-treebeard==4.3.1 # via wagtail
django-treebeard==4.3.1 # via -r base.in, wagtail
django==3.1 # via -r base.in, django-auth-ldap, django-filter, django-modeltranslation, django-taggit, django-treebeard, djangorestframework, wagtail
djangorestframework==3.11.1 # via wagtail
draftjs-exporter==2.1.7 # via wagtail
Expand Down

0 comments on commit 0b90922

Please sign in to comment.