diff --git a/app/urls.py b/app/urls.py index fe27639..dc27618 100644 --- a/app/urls.py +++ b/app/urls.py @@ -102,6 +102,11 @@ urlpatterns = [ trades.TradeDelete.as_view(), name="trade_delete", ), + path( + "trades/action/delete_all/", + trades.TradeDeleteAll.as_view(), + name="trade_delete_all", + ), path("positions//", positions.Positions.as_view(), name="positions"), path( "positions///", diff --git a/core/templates/window-content/objects.html b/core/templates/window-content/objects.html index db7f621..f4644b9 100644 --- a/core/templates/window-content/objects.html +++ b/core/templates/window-content/objects.html @@ -5,9 +5,9 @@ {% if page_subtitle is not None %}

{{ page_subtitle }}

{% endif %} +
-{% if submit_url is not None %} -
+ {% if submit_url is not None %} -
-{% endif %} + {% endif %} + {% if delete_all_url is not None %} + + {% endif %} +
{% include list_template %} diff --git a/core/views/__init__.py b/core/views/__init__.py index 9868cdc..a2c2fa5 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -32,6 +32,8 @@ class ObjectList(ListView): submit_url_name = None + delete_all_url_name = None + # copied from BaseListView def get(self, request, *args, **kwargs): self.object_list = self.get_queryset() @@ -80,6 +82,8 @@ class ObjectList(ListView): context["list_url"] = list_url context["context_object_name"] = self.context_object_name 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 if self.request.htmx: @@ -102,6 +106,9 @@ class ObjectCreate(CreateView): request = None + def post_save(self, obj): + pass + def form_valid(self, form): obj = form.save(commit=False) if self.request is None: @@ -109,6 +116,7 @@ class ObjectCreate(CreateView): obj.user = self.request.user obj.save() form.save_m2m() + self.post_save(obj) context = {"message": "Object created", "class": "success"} response = self.render_to_response(context) response["HX-Trigger"] = f"{self.context_object_name_singular}Event" @@ -170,12 +178,16 @@ class ObjectUpdate(UpdateView): request = None + def post_save(self, obj): + pass + def form_valid(self, form): obj = form.save(commit=False) if self.request is None: raise Exception("Request is None") obj.save() form.save_m2m() + self.post_save(obj) context = {"message": "Object updated", "class": "success"} response = self.render_to_response(context) response["HX-Trigger"] = f"{self.context_object_name_singular}Event" diff --git a/core/views/trades.py b/core/views/trades.py index a2b4f86..c2165e1 100644 --- a/core/views/trades.py +++ b/core/views/trades.py @@ -1,4 +1,6 @@ from django.contrib.auth.mixins import LoginRequiredMixin +from django.shortcuts import render +from django.views import View from core.forms import TradeForm from core.models import Trade @@ -25,6 +27,8 @@ class TradeList(LoginRequiredMixin, ObjectList): submit_url_name = "trade_create" + delete_all_url_name = "trade_delete_all" + class TradeCreate(LoginRequiredMixin, ObjectCreate): model = Trade @@ -37,6 +41,10 @@ class TradeCreate(LoginRequiredMixin, ObjectCreate): submit_url_name = "trade_create" + def post_save(self, obj): + obj.post() + log.debug(f"Posting trade {obj}") + class TradeUpdate(LoginRequiredMixin, ObjectUpdate): model = Trade @@ -57,3 +65,17 @@ class TradeDelete(LoginRequiredMixin, ObjectDelete): list_url_name = "trades" 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