Skip to content

Commit

Permalink
feat: make generic crudl views
Browse files Browse the repository at this point in the history
Create generic Create, View, Update, Delete and List views that work
with ContentTypes
  • Loading branch information
b1rger committed Dec 21, 2023
1 parent 3fcd5d8 commit 9675809
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
32 changes: 32 additions & 0 deletions apis_core/core/templates/core/generic_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends basetemplate|default:"base.html" %}

{% block content %}
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Confirm delete</h3>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<form action="" method="post">
{% csrf_token %}
<h4>
Are you sure you want to delete: <strong>{{ object }}</strong> ?
</h4>
<input class="btn btn-danger" type="submit" value="Yes, I want to delete" />
</form>
</div>
<div class="modal-footer">
<input class="btn"
type="submit"
value="No, bring me back"
onclick="goBack()" />
</div>
</div>
</div>
<script>
function goBack() {
window.history.back();
}
</script>
{% endblock content %}
6 changes: 6 additions & 0 deletions apis_core/core/templates/core/generic_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends basetemplate|default:"base.html" %}
{% load render_table from django_tables2 %}

{% block content %}
{{ object }}
{% endblock content %}
6 changes: 6 additions & 0 deletions apis_core/core/templates/core/generic_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends basetemplate|default:"base.html" %}
{% load crispy_forms_tags %}

{% block content %}
{% crispy form %}
{% endblock content %}
6 changes: 6 additions & 0 deletions apis_core/core/templates/core/generic_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends basetemplate|default:"base.html" %}
{% load render_table from django_tables2 %}

{% block content %}
{% render_table table %}
{% endblock content %}
36 changes: 36 additions & 0 deletions apis_core/core/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import get_object_or_404
from django.urls import include, path, register_converter

from apis_core.core import views


class ContenttypeConverter:
regex = r"\w+\.\w+"

def to_python(self, value):
app_label, model = value.split(".")
return get_object_or_404(ContentType, app_label=app_label, model=model)

def to_url(self, value):
return f"{value.app_label}.{value.model}"


register_converter(ContenttypeConverter, "ccc")

app_name = "core"

urlpatterns = [
path(
"<ccc:contenttype>/",
include(
[
path("", views.ListCC.as_view(), name="genericlist"),
path("<int:pk>", views.DetailCC.as_view()),
path("create", views.CreateCC.as_view()),
path("delete/<int:pk>", views.DeleteCC.as_view()),
path("update/<int:pk>", views.UpdateCC.as_view()),
]
),
)
]
43 changes: 43 additions & 0 deletions apis_core/core/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import json

from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse

from django_tables2 import SingleTableView
from django_tables2.tables import table_factory

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
Expand All @@ -25,3 +32,39 @@ def get(self, request, *args, **kwargs):
if app_labels:
app_labels = app_labels.split(",")
return Response(json.loads(datadump_serializer(app_labels, "json")))


class CCMixin:
def get_template_names(self):
return super().get_template_names() + [
f"core/generic{self.template_name_suffix}.html"
]

def get_queryset(self):
return self.kwargs.get("contenttype").model_class().objects.all()


class ListCC(CCMixin, SingleTableView):
def get_table_class(self):
return table_factory(self.kwargs.get("contenttype").model_class())


class DetailCC(CCMixin, DetailView):
pass


class CreateCC(CCMixin, CreateView):
fields = "__all__"
template_name = "core/generic_form.html"


class DeleteCC(CCMixin, DeleteView):
def get_success_url(self):
return reverse(
"apis:core:genericlist",
args=[self.request.resolver_match.kwargs["contenttype"]],
)


class UpdateCC(CCMixin, UpdateView):
fields = "__all__"
1 change: 1 addition & 0 deletions apis_core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def build_apis_mock_request(method, path, view, original_request, **kwargs):
name="GetEntityGeneric",
),
path("api/dumpdata", Dumpdata.as_view()),
path("", include("apis_core.core.urls", namespace="core"))
]

if "apis_fulltext_download" in settings.INSTALLED_APPS:
Expand Down

0 comments on commit 9675809

Please sign in to comment.