Implement helper for deleting all database objects
This commit is contained in:
parent
2a876a20ba
commit
49a3737a72
|
@ -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/<str:type>/", positions.Positions.as_view(), name="positions"),
|
||||
path(
|
||||
"positions/<str:type>/<str:account_id>/",
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
{% if page_subtitle is not None %}
|
||||
<h1 class="subtitle">{{ page_subtitle }}</h1>
|
||||
{% endif %}
|
||||
<div class="buttons">
|
||||
|
||||
{% if submit_url is not None %}
|
||||
<div class="buttons">
|
||||
{% if submit_url is not None %}
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-get="{{ submit_url }}"
|
||||
|
@ -21,8 +21,25 @@
|
|||
<span>{{ title_singular }}</span>
|
||||
</span>
|
||||
</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 %}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue