Skip to content

Commit

Permalink
added layer create view
Browse files Browse the repository at this point in the history
  • Loading branch information
andywar65 committed Nov 21, 2022
1 parent b38760a commit 5fc5f1f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
14 changes: 11 additions & 3 deletions templates/djeocad/includes/drawing_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ <h4 class="card-title">
</div>
<div class="mx-auto" style="max-width: 480px; margin-top: 20px">
<h4>{% trans "Layers / Blocks" %}:</h4>
{% 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 %}
<p>{% trans "No layers yet" %}</p>
{% endif %}
<a class="btn btn-primary"
href="{% url 'djeocad:layer_create' username=object.user.username pk=object.id %}">
{% trans "Add layer" %}
</a>
</div>
{% endblock content %}
27 changes: 27 additions & 0 deletions templates/djeocad/includes/layer_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "djeocad/base_form.html" %}
{% load bootstrap5 %}
{% load i18n %}

{% block content %}
<div class="card mx-auto" style="max-width: 480px; margin-top: 60px">
<div class="card-header">
<h4 class="card-title">
{% trans "Create layer"%}
</h4>
</div>
<div class="card-body">
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
<input class="btn btn-primary"
type="submit"
value="{% trans 'Add' %}" />
<a class="btn btn-secondary"
href="{% url 'djeocad:drawing_update' username=drawing.user.username pk=drawing.id %}"
>
{% trans "Cancel" %}
</a>
</form>
</div>
</div>
{% endblock content %}
1 change: 0 additions & 1 deletion templates/djeocad/includes/layer_update.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "djeocad/base_form.html" %}
{% load bootstrap5 %}
{% load i18n %}
{% load fb_versions %}

{% block content %}
<div class="card mx-auto" style="max-width: 480px; margin-top: 60px">
Expand Down
6 changes: 6 additions & 0 deletions urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DrawingDeleteView,
DrawingDetailView,
DrawingUpdateView,
LayerCreateView,
LayerDeleteView,
LayerUpdateView,
drawing_download,
Expand Down Expand Up @@ -37,6 +38,11 @@
DrawingDeleteView.as_view(),
name="drawing_delete",
),
path(
_("author/<username>/drawing/<pk>/layer-add/"),
LayerCreateView.as_view(),
name="layer_create",
),
path(
_("author/<username>/layer/<pk>/update/"),
LayerUpdateView.as_view(),
Expand Down
39 changes: 38 additions & 1 deletion views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
)
Expand Down

0 comments on commit 5fc5f1f

Please sign in to comment.