瀏覽代碼

Admin page for Piki pages improved

master
Dirk Alders 2 月之前
父節點
當前提交
b30d86a21d
共有 4 個文件被更改,包括 93 次插入5 次删除
  1. 35
    2
      pages/admin.py
  2. 14
    3
      pages/forms.py
  3. 22
    0
      pages/templates/admin/set_group.html
  4. 22
    0
      pages/templates/admin/set_perms.html

+ 35
- 2
pages/admin.py 查看文件

@@ -2,18 +2,51 @@ from django.contrib import admin
2 2
 from simple_history.admin import SimpleHistoryAdmin
3 3
 
4 4
 from .models import PikiPage
5
+from .forms import GroupForm, PermForm
6
+
7
+from django.shortcuts import render
8
+from django.http import HttpResponseRedirect
9
+from django.contrib.auth.models import Group
5 10
 
6 11
 
7 12
 class PikiPageAdmin(SimpleHistoryAdmin):
8
-    list_display = ('rel_path', 'tags', 'deleted')
13
+    list_display = ('rel_path', 'tags', 'group', 'other_perms_read', 'other_perms_write')
9 14
     history_list_display = ('rel_path', 'tags', 'deleted')
10 15
     search_fields = ('rel_path', 'tags', )
11 16
     list_filter = (
12
-        ('deleted', admin.BooleanFieldListFilter),
17
+        ('group', admin.RelatedFieldListFilter),
13 18
         ('other_perms_read', admin.BooleanFieldListFilter),
14 19
         ('other_perms_write', admin.BooleanFieldListFilter),
15 20
     )
16 21
     ordering = ["rel_path"]
22
+    actions = ["remove_access_others", "set_group", "set_perms", ]
23
+
24
+    @admin.action(description="Remove access for others")
25
+    def remove_access_others(self, request, query_set):
26
+        query_set.update(other_perms_read=False, other_perms_write=False)
27
+
28
+    @admin.action(description="Set group for pages")
29
+    def set_group(self, request, queryset):
30
+        if 'apply' in request.POST:
31
+            if request.POST.get("group"):
32
+                group = Group.objects.get(id=request.POST.get("group"))
33
+            else:
34
+                group = None
35
+            queryset.update(group=group)
36
+            self.message_user(request, "Changed group for {} pages".format(queryset.count()))
37
+            return HttpResponseRedirect(request.get_full_path())
38
+        return render(request, 'admin/set_group.html', context={'pages': queryset, 'form': GroupForm()})
39
+
40
+    @admin.action(description="Set permissions")
41
+    def set_perms(self, request, queryset):
42
+        if 'apply' in request.POST:
43
+            keys = ["owner_perms_read", "owner_perms_write", "group_perms_read", "group_perms_write", "other_perms_read", "other_perms_write"]
44
+            perms = {key: key in request.POST for key in keys}
45
+            queryset.update(**perms)
46
+            self.message_user(request, "Changed permissions for {} pages".format(queryset.count()))
47
+            return HttpResponseRedirect(request.get_full_path())
48
+        return render(request, 'admin/set_perms.html', context={'pages': queryset, 'form': PermForm()})
17 49
 
18 50
 
51
+admin.site.disable_action('delete_selected')
19 52
 admin.site.register(PikiPage, PikiPageAdmin)

+ 14
- 3
pages/forms.py 查看文件

@@ -1,7 +1,5 @@
1
-from typing import Any, Mapping
2 1
 from django import forms
3
-from django.forms.renderers import BaseRenderer
4
-from django.forms.utils import ErrorList
2
+from django.contrib.auth.models import Group
5 3
 
6 4
 from .models import PikiPage
7 5
 
@@ -17,6 +15,19 @@ class EditForm(forms.ModelForm):
17 15
             "other_perms_read", "other_perms_write",]
18 16
 
19 17
 
18
+class GroupForm(forms.Form):
19
+    group = forms.ModelChoiceField(queryset=Group.objects.all(), required=False)
20
+
21
+
22
+class PermForm(forms.Form):
23
+    owner_perms_read = forms.BooleanField(initial=True, required=False, label="Read (owner)")
24
+    owner_perms_write = forms.BooleanField(initial=True, required=False, label="Write (owner)")
25
+    group_perms_read = forms.BooleanField(initial=True, required=False, label="Read (group)")
26
+    group_perms_write = forms.BooleanField(initial=True, required=False, label="Write (group)")
27
+    other_perms_read = forms.BooleanField(initial=True, required=False, label="Read (other)")
28
+    other_perms_write = forms.BooleanField(initial=False, required=False, label="Write (other)")
29
+
30
+
20 31
 class RenameForm(forms.Form):  # Note that it is not inheriting from forms.ModelForm
21 32
     page_name = forms.CharField(max_length=500, label="Change the page name:", required=True)
22 33
 

+ 22
- 0
pages/templates/admin/set_group.html 查看文件

@@ -0,0 +1,22 @@
1
+{% extends "admin/base_site.html" %}
2
+
3
+{% block content %}
4
+<form action="" method="post">
5
+  {% csrf_token %}
6
+<p>
7
+Choose a group for the following pages:
8
+</p>
9
+<ul>  
10
+  {% for page in pages %}
11
+    <li>
12
+      {{ page.rel_path }}
13
+    </li>
14
+    <input type="hidden" name="_selected_action" value="{{ page.pk }}" />
15
+  {% endfor %}
16
+</ul>
17
+
18
+  {{ form.as_p }}
19
+  <input type="hidden" name="action" value="set_group" />
20
+  <input type="submit" name="apply" value="Set"/>
21
+</form>
22
+{% endblock %}

+ 22
- 0
pages/templates/admin/set_perms.html 查看文件

@@ -0,0 +1,22 @@
1
+{% extends "admin/base_site.html" %}
2
+
3
+{% block content %}
4
+<form action="" method="post">
5
+  {% csrf_token %}
6
+<p>
7
+Choose a group for the following pages:
8
+</p>
9
+<ul>  
10
+  {% for page in pages %}
11
+    <li>
12
+      {{ page.rel_path }}
13
+    </li>
14
+    <input type="hidden" name="_selected_action" value="{{ page.pk }}" />
15
+  {% endfor %}
16
+</ul>
17
+
18
+  {{ form.as_p }}
19
+  <input type="hidden" name="action" value="set_perms" />
20
+  <input type="submit" name="apply" value="Set"/>
21
+</form>
22
+{% endblock %}

Loading…
取消
儲存