[collector views] switch some views to class-based ones

This commit is contained in:
bronsen 2025-03-13 19:03:57 +01:00
parent 59596f3064
commit 6386d25fff
2 changed files with 17 additions and 17 deletions

View file

@ -5,8 +5,8 @@ from . import views
app_name = "collector"
urlpatterns = [
path("", views.index, name="index"),
path("recorded/<int:teil_id>/", views.detail, name="detail"),
path("", views.IndexView.as_view(), name="index"),
path("recorded/<int:pk>/", views.DetailView.as_view(), name="detail"),
path("enter/", views.enter, name="enter"),
path("all/", views.list_all, name="all"),
path("all/", views.ListView.as_view(), name="all"),
]

View file

@ -1,32 +1,32 @@
import logging
from django.db import transaction
from django.db.models import QuerySet
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from .models import Teil
logger = logging.getLogger(__name__)
def index(request: HttpRequest) -> HttpResponse:
teile_list = Teil.objects.order_by("-modified")[:10]
context = {
"teile_list": teile_list,
}
return render(request, "collector/index.html", context)
class ListView(generic.ListView):
template_name = "collector/index.html"
context_object_name = "teile_list"
def get_queryset(self) -> QuerySet:
return Teil.objects.order_by("-modified")
def list_all(request: HttpRequest) -> HttpResponse:
teile_list = Teil.objects.order_by("-modified")
return render(request, "collector/index.html", {"teile_list": teile_list})
class IndexView(ListView):
def get_queryset(self) -> QuerySet:
return super().get_queryset()[:10]
def detail(request: HttpRequest, teil_id) -> HttpResponse:
teil = get_object_or_404(Teil, pk=teil_id)
return render(request, "collector/detail.html", {"teil": teil})
class DetailView(generic.DetailView):
model = Teil
template_name = "collector/detail.html"
def enter(request: HttpRequest) -> HttpResponse: