diff --git a/config/settings/base.py b/config/settings/base.py index bc6f4b4..18a8f31 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -71,6 +71,7 @@ 'modelcluster', 'taggit', 'rest_framework', + 'treebeard', ] LOCAL_APPS = [ 'mpicms.base.apps.BaseAppConfig', @@ -188,7 +189,6 @@ # SECURITY # ------------------------------------------------------------------------------ SESSION_COOKIE_HTTPONLY = True -CSRF_COOKIE_HTTPONLY = True SECURE_BROWSER_XSS_FILTER = True X_FRAME_OPTIONS = 'DENY' diff --git a/mpicms/personal/admin.py b/mpicms/personal/admin.py new file mode 100644 index 0000000..73f9e62 --- /dev/null +++ b/mpicms/personal/admin.py @@ -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) diff --git a/mpicms/personal/migrations/0027_group_to_tree.py b/mpicms/personal/migrations/0027_group_to_tree.py new file mode 100644 index 0000000..2798bae --- /dev/null +++ b/mpicms/personal/migrations/0027_group_to_tree.py @@ -0,0 +1,59 @@ +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().order_by('name'), 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.AlterModelOptions( + name='group', + options={'verbose_name': 'Group', 'verbose_name_plural': 'Groups'}, + ), + migrations.RemoveField( + model_name='group', + name='priority', + ), + 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), + ), + ] diff --git a/mpicms/personal/models.py b/mpicms/personal/models.py index e7c8035..3b1d133 100644 --- a/mpicms/personal/models.py +++ b/mpicms/personal/models.py @@ -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 @@ -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): @@ -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'), @@ -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'] diff --git a/mpicms/personal/views.py b/mpicms/personal/views.py index 740eeca..acac3bb 100644 --- a/mpicms/personal/views.py +++ b/mpicms/personal/views.py @@ -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 "" diff --git a/mpicms/templates/personal/list.html b/mpicms/templates/personal/list.html index 2382ada..dba2463 100644 --- a/mpicms/templates/personal/list.html +++ b/mpicms/templates/personal/list.html @@ -15,17 +15,15 @@

{% trans 'Contact List' %}

diff --git a/requirements/base.in b/requirements/base.in index ec5640a..92f3081 100644 --- a/requirements/base.in +++ b/requirements/base.in @@ -12,3 +12,4 @@ markdown pygments ics django-auth-ldap +django-treebeard diff --git a/requirements/base.txt b/requirements/base.txt index 91e7ff3..d4cb5e7 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -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