class-based views in the control panel? that's almost modern code!
This commit is contained in:
parent
48fbf2ca66
commit
41aa6ed2fc
5 changed files with 45 additions and 49 deletions
|
@ -1,21 +1,21 @@
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
<p>
|
<p>
|
||||||
{% if objects.has_previous %}
|
{% if page_obj.has_previous %}
|
||||||
<a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page=1">
|
<a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page=1">
|
||||||
« {% trans 'first' %}
|
« {% trans 'first' %}
|
||||||
</a> ·
|
</a> ·
|
||||||
<a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page={{ objects.previous_page_number }}">
|
<a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page={{ page_obj.previous_page_number }}">
|
||||||
‹ {% trans 'previous' %}
|
‹ {% trans 'previous' %}
|
||||||
</a> ·
|
</a> ·
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% with page_number=objects.number num_pages=objects.paginator.num_pages %}
|
{% with page_number=page_obj.number num_pages=paginator.num_pages %}
|
||||||
{% blocktrans %}Page {{ page_number }} of {{ num_pages }}{% endblocktrans %}
|
{% blocktrans %}Page {{ page_number }} of {{ num_pages }}{% endblocktrans %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% if objects.has_next %}
|
{% if page_obj.has_next %}
|
||||||
· <a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page={{ objects.next_page_number }}">
|
· <a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page={{ page_obj.next_page_number }}">
|
||||||
{% trans 'next' %} ›
|
{% trans 'next' %} ›
|
||||||
</a>
|
</a>
|
||||||
· <a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page={{ objects.paginator.num_pages }}">
|
· <a href="?{% if request.GET.s %}s={{ request.GET.s | urlencode }}&{% endif %}page={{ page_obj.paginator.num_pages }}">
|
||||||
{% trans 'last' %} »
|
{% trans 'last' %} »
|
||||||
</a>
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
{% block heading %}{% trans 'Mesh' %}{% endblock %}
|
{% block heading %}{% trans 'Mesh' %}{% endblock %}
|
||||||
|
|
||||||
{% block subcontent %}
|
{% block subcontent %}
|
||||||
{% include 'control/fragment_pagination.html' with objects=nodes %}
|
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{% trans 'Address' %}</th>
|
<th>{% trans 'Address' %}</th>
|
||||||
|
@ -24,6 +22,4 @@
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
{% include 'control/fragment_pagination.html' with objects=nodes %}
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
<input type="text" name="s" value="{{ request.GET.s }}"> <button type="submit">{% trans 'Search' %}</button>
|
<input type="text" name="s" value="{{ request.GET.s }}"> <button type="submit">{% trans 'Search' %}</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% include 'control/fragment_pagination.html' with objects=users %}
|
{% include 'control/fragment_pagination.html' %}
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -23,5 +23,5 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
{% include 'control/fragment_pagination.html' with objects=users %}
|
{% include 'control/fragment_pagination.html' %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from c3nav.control.views import (announcement_detail, announcement_list, grant_access, grant_access_qr, main_index,
|
from c3nav.control.views import (announcement_detail, announcement_list, grant_access, grant_access_qr, map_updates,
|
||||||
map_updates, user_detail, user_list, mesh_node_list)
|
user_detail, MeshNodeListView, ControlPanelIndexView,
|
||||||
|
UserListView)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('users/', user_list, name='control.users'),
|
path('users/', UserListView.as_view(), name='control.users'),
|
||||||
path('users/<int:user>/', user_detail, name='control.users.detail'),
|
path('users/<int:user>/', user_detail, name='control.users.detail'),
|
||||||
path('access/', grant_access, name='control.access'),
|
path('access/', grant_access, name='control.access'),
|
||||||
path('access/<str:token>', grant_access_qr, name='control.access.qr'),
|
path('access/<str:token>', grant_access_qr, name='control.access.qr'),
|
||||||
path('announcements/', announcement_list, name='control.announcements'),
|
path('announcements/', announcement_list, name='control.announcements'),
|
||||||
path('announcements/<int:annoucement>/', announcement_detail, name='control.announcements.detail'),
|
path('announcements/<int:annoucement>/', announcement_detail, name='control.announcements.detail'),
|
||||||
path('mapupdates/', map_updates, name='control.map_updates'),
|
path('mapupdates/', map_updates, name='control.map_updates'),
|
||||||
path('mesh/', mesh_node_list, name='control.mesh_nodes'),
|
path('mesh/', MeshNodeListView.as_view(), name='control.mesh_nodes'),
|
||||||
path('', main_index, name='control.index'),
|
path('', ControlPanelIndexView.as_view(), name='control.index'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,6 +6,7 @@ from urllib.parse import urlencode
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
|
@ -18,6 +19,7 @@ from django.utils import timezone
|
||||||
from django.utils.crypto import get_random_string
|
from django.utils.crypto import get_random_string
|
||||||
from django.utils.timezone import make_aware
|
from django.utils.timezone import make_aware
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from django.views.generic import TemplateView, ListView
|
||||||
|
|
||||||
from c3nav.control.forms import (AccessPermissionForm, AnnouncementForm, MapUpdateFilterForm, MapUpdateForm,
|
from c3nav.control.forms import (AccessPermissionForm, AnnouncementForm, MapUpdateFilterForm, MapUpdateForm,
|
||||||
UserPermissionsForm, UserSpaceAccessForm)
|
UserPermissionsForm, UserSpaceAccessForm)
|
||||||
|
@ -29,6 +31,16 @@ from c3nav.mesh.models import MeshNode
|
||||||
from c3nav.site.models import Announcement
|
from c3nav.site.models import Announcement
|
||||||
|
|
||||||
|
|
||||||
|
class ControlPanelMixin(UserPassesTestMixin, LoginRequiredMixin):
|
||||||
|
login_url = 'site.login'
|
||||||
|
user_permission = None
|
||||||
|
|
||||||
|
def test_func(self):
|
||||||
|
if not self.user_permission:
|
||||||
|
return True
|
||||||
|
return getattr(self.request.user_permissions, self.user_permission)
|
||||||
|
|
||||||
|
|
||||||
def control_panel_view(func):
|
def control_panel_view(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapped_func(request, *args, **kwargs):
|
def wrapped_func(request, *args, **kwargs):
|
||||||
|
@ -38,28 +50,23 @@ def control_panel_view(func):
|
||||||
return login_required(login_url='site.login')(wrapped_func)
|
return login_required(login_url='site.login')(wrapped_func)
|
||||||
|
|
||||||
|
|
||||||
@login_required(login_url='site.login')
|
class ControlPanelIndexView(ControlPanelMixin, TemplateView):
|
||||||
@control_panel_view
|
template_name = "control/index.html"
|
||||||
def main_index(request):
|
|
||||||
return render(request, 'control/index.html', {})
|
|
||||||
|
|
||||||
|
|
||||||
@login_required(login_url='site.login')
|
class UserListView(ControlPanelMixin, ListView):
|
||||||
@control_panel_view
|
model = User
|
||||||
def user_list(request):
|
paginate_by = 20
|
||||||
search = request.GET.get('s')
|
template_name = "control/users.html"
|
||||||
page = request.GET.get('page', 1)
|
ordering = "id"
|
||||||
|
context_object_name = "users"
|
||||||
|
|
||||||
queryset = User.objects.order_by('id')
|
def get_queryset(self):
|
||||||
if search:
|
qs = super().get_queryset()
|
||||||
queryset = queryset.filter(username__icontains=search.strip())
|
search = self.request.GET.get('s')
|
||||||
|
if search:
|
||||||
paginator = Paginator(queryset, 20)
|
qs = qs.filter(username__icontains=search.strip())
|
||||||
users = paginator.page(page)
|
return qs
|
||||||
|
|
||||||
return render(request, 'control/users.html', {
|
|
||||||
'users': users,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
@login_required(login_url='site.login')
|
@login_required(login_url='site.login')
|
||||||
|
@ -410,16 +417,8 @@ def map_updates(request):
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@login_required(login_url='site.login')
|
class MeshNodeListView(ControlPanelMixin, ListView):
|
||||||
@control_panel_view
|
model = MeshNode
|
||||||
def mesh_node_list(request):
|
template_name = "control/mesh_nodes.html"
|
||||||
page = request.GET.get('page', 1)
|
ordering = "address"
|
||||||
|
context_object_name = "nodes"
|
||||||
queryset = MeshNode.objects.order_by('address')
|
|
||||||
|
|
||||||
paginator = Paginator(queryset, 20)
|
|
||||||
nodes = paginator.page(page)
|
|
||||||
|
|
||||||
return render(request, 'control/mesh_nodes.html', {
|
|
||||||
'nodes': nodes,
|
|
||||||
})
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue