Implement helper for deleting all database objects

This commit is contained in:
Mark Veidemanis 2022-10-30 10:57:41 +00:00
parent 2a876a20ba
commit 49a3737a72
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 60 additions and 4 deletions

View File

@ -102,6 +102,11 @@ urlpatterns = [
trades.TradeDelete.as_view(), trades.TradeDelete.as_view(),
name="trade_delete", name="trade_delete",
), ),
path(
"trades/action/delete_all/",
trades.TradeDeleteAll.as_view(),
name="trade_delete_all",
),
path("positions/<str:type>/", positions.Positions.as_view(), name="positions"), path("positions/<str:type>/", positions.Positions.as_view(), name="positions"),
path( path(
"positions/<str:type>/<str:account_id>/", "positions/<str:type>/<str:account_id>/",

View File

@ -5,9 +5,9 @@
{% if page_subtitle is not None %} {% if page_subtitle is not None %}
<h1 class="subtitle">{{ page_subtitle }}</h1> <h1 class="subtitle">{{ page_subtitle }}</h1>
{% endif %} {% endif %}
<div class="buttons">
{% if submit_url is not None %} {% if submit_url is not None %}
<div class="buttons">
<button <button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{{ submit_url }}" hx-get="{{ submit_url }}"
@ -21,8 +21,25 @@
<span>{{ title_singular }}</span> <span>{{ title_singular }}</span>
</span> </span>
</button> </button>
</div>
{% endif %} {% endif %}
{% if delete_all_url is not None %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{{ delete_all_url }}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete all {{ context_object_name }}?"
class="button is-info">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-plus"></i>
</span>
<span>Delete all {{ context_object_name }} </span>
</span>
</button>
{% endif %}
</div>
{% include list_template %} {% include list_template %}

View File

@ -32,6 +32,8 @@ class ObjectList(ListView):
submit_url_name = None submit_url_name = None
delete_all_url_name = None
# copied from BaseListView # copied from BaseListView
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset() self.object_list = self.get_queryset()
@ -80,6 +82,8 @@ class ObjectList(ListView):
context["list_url"] = list_url context["list_url"] = list_url
context["context_object_name"] = self.context_object_name context["context_object_name"] = self.context_object_name
context["context_object_name_singular"] = self.context_object_name_singular context["context_object_name_singular"] = self.context_object_name_singular
if self.delete_all_url_name:
context["delete_all_url"] = reverse(self.delete_all_url_name)
# Return partials for HTMX # Return partials for HTMX
if self.request.htmx: if self.request.htmx:
@ -102,6 +106,9 @@ class ObjectCreate(CreateView):
request = None request = None
def post_save(self, obj):
pass
def form_valid(self, form): def form_valid(self, form):
obj = form.save(commit=False) obj = form.save(commit=False)
if self.request is None: if self.request is None:
@ -109,6 +116,7 @@ class ObjectCreate(CreateView):
obj.user = self.request.user obj.user = self.request.user
obj.save() obj.save()
form.save_m2m() form.save_m2m()
self.post_save(obj)
context = {"message": "Object created", "class": "success"} context = {"message": "Object created", "class": "success"}
response = self.render_to_response(context) response = self.render_to_response(context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event" response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
@ -170,12 +178,16 @@ class ObjectUpdate(UpdateView):
request = None request = None
def post_save(self, obj):
pass
def form_valid(self, form): def form_valid(self, form):
obj = form.save(commit=False) obj = form.save(commit=False)
if self.request is None: if self.request is None:
raise Exception("Request is None") raise Exception("Request is None")
obj.save() obj.save()
form.save_m2m() form.save_m2m()
self.post_save(obj)
context = {"message": "Object updated", "class": "success"} context = {"message": "Object updated", "class": "success"}
response = self.render_to_response(context) response = self.render_to_response(context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event" response["HX-Trigger"] = f"{self.context_object_name_singular}Event"

View File

@ -1,4 +1,6 @@
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import View
from core.forms import TradeForm from core.forms import TradeForm
from core.models import Trade from core.models import Trade
@ -25,6 +27,8 @@ class TradeList(LoginRequiredMixin, ObjectList):
submit_url_name = "trade_create" submit_url_name = "trade_create"
delete_all_url_name = "trade_delete_all"
class TradeCreate(LoginRequiredMixin, ObjectCreate): class TradeCreate(LoginRequiredMixin, ObjectCreate):
model = Trade model = Trade
@ -37,6 +41,10 @@ class TradeCreate(LoginRequiredMixin, ObjectCreate):
submit_url_name = "trade_create" submit_url_name = "trade_create"
def post_save(self, obj):
obj.post()
log.debug(f"Posting trade {obj}")
class TradeUpdate(LoginRequiredMixin, ObjectUpdate): class TradeUpdate(LoginRequiredMixin, ObjectUpdate):
model = Trade model = Trade
@ -57,3 +65,17 @@ class TradeDelete(LoginRequiredMixin, ObjectDelete):
list_url_name = "trades" list_url_name = "trades"
list_url_args = ["type"] list_url_args = ["type"]
class TradeDeleteAll(LoginRequiredMixin, View):
template_name = "partials/notify.html"
def delete(self, request):
"""
Delete all trades by the current user
"""
Trade.objects.filter(user=request.user).delete()
context = {"message": "All trades deleted", "class": "success"}
response = render(request, self.template_name, context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response