From 5fc5f1f62dc8c2845e86d37178d00ea8873449ba Mon Sep 17 00:00:00 2001 From: andywar65 Date: Mon, 21 Nov 2022 18:57:18 +0100 Subject: [PATCH] added layer create view --- .../djeocad/includes/drawing_update.html | 14 +++++-- templates/djeocad/includes/layer_create.html | 27 +++++++++++++ templates/djeocad/includes/layer_update.html | 1 - urls.py | 6 +++ views.py | 39 ++++++++++++++++++- 5 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 templates/djeocad/includes/layer_create.html diff --git a/templates/djeocad/includes/drawing_update.html b/templates/djeocad/includes/drawing_update.html index af8486e..d1ca097 100644 --- a/templates/djeocad/includes/drawing_update.html +++ b/templates/djeocad/includes/drawing_update.html @@ -31,8 +31,16 @@

{% trans "Layers / Blocks" %}:

- {% for layer in object.related_layers.all %} - {% include "djeocad/htmx/layer_detail.html" %} - {% endfor %} + {% if object.related_layers %} + {% for layer in object.related_layers.all %} + {% include "djeocad/htmx/layer_detail.html" %} + {% endfor %} + {% else %} +

{% trans "No layers yet" %}

+ {% endif %} + + {% trans "Add layer" %} +
{% endblock content %} diff --git a/templates/djeocad/includes/layer_create.html b/templates/djeocad/includes/layer_create.html new file mode 100644 index 0000000..c448463 --- /dev/null +++ b/templates/djeocad/includes/layer_create.html @@ -0,0 +1,27 @@ +{% extends "djeocad/base_form.html" %} +{% load bootstrap5 %} +{% load i18n %} + +{% block content %} +
+
+

+ {% trans "Create layer"%} +

+
+
+
+ {% csrf_token %} + {% bootstrap_form form %} + + + {% trans "Cancel" %} + +
+
+
+{% endblock content %} diff --git a/templates/djeocad/includes/layer_update.html b/templates/djeocad/includes/layer_update.html index 8b79d13..2a48667 100644 --- a/templates/djeocad/includes/layer_update.html +++ b/templates/djeocad/includes/layer_update.html @@ -1,7 +1,6 @@ {% extends "djeocad/base_form.html" %} {% load bootstrap5 %} {% load i18n %} -{% load fb_versions %} {% block content %}
diff --git a/urls.py b/urls.py index d5aec23..0aa05be 100644 --- a/urls.py +++ b/urls.py @@ -8,6 +8,7 @@ DrawingDeleteView, DrawingDetailView, DrawingUpdateView, + LayerCreateView, LayerDeleteView, LayerUpdateView, drawing_download, @@ -37,6 +38,11 @@ DrawingDeleteView.as_view(), name="drawing_delete", ), + path( + _("author//drawing//layer-add/"), + LayerCreateView.as_view(), + name="layer_create", + ), path( _("author//layer//update/"), LayerUpdateView.as_view(), diff --git a/views.py b/views.py index 99538c4..0917f14 100644 --- a/views.py +++ b/views.py @@ -200,6 +200,43 @@ def dispatch(self, request, *args, **kwargs): return response +class LayerCreateView(LoginRequiredMixin, CreateView): + model = Layer + form_class = LayerCreateForm + template_name = "djeocad/includes/layer_create.html" + + def setup(self, request, *args, **kwargs): + super(LayerCreateView, self).setup(request, *args, **kwargs) + get_object_or_404(User, username=self.kwargs["username"]) + self.drawing = get_object_or_404(Drawing, id=self.kwargs["pk"]) + if request.user != self.drawing.user: + raise PermissionDenied + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["drawing"] = self.drawing + return context + + def post(self, request, *args, **kwargs): + geometry = json.loads(request.POST["geom"]) + if geometry["type"] != "GeometryCollection": + request.POST = request.POST.copy() + request.POST["geom"] = json.dumps( + {"type": "GeometryCollection", "geometries": [geometry]} + ) + return super(LayerCreateView, self).post(request, *args, **kwargs) + + def form_valid(self, form): + form.instance.drawing = self.drawing + return super(LayerCreateView, self).form_valid(form) + + def get_success_url(self): + return reverse( + "djeocad:drawing_detail", + kwargs={"username": self.drawing.user.username, "pk": self.drawing.id}, + ) + + class LayerUpdateView(LoginRequiredMixin, UpdateView): model = Layer form_class = LayerCreateForm @@ -228,7 +265,7 @@ def get_success_url(self): return reverse( "djeocad:drawing_detail", kwargs={ - "username": self.request.user.username, + "username": self.object.drawing.user.username, "pk": self.object.drawing.id, }, )