Update tables when objects are changed

Mark Veidemanis 2 years ago
parent c685b6d25f
commit 6902665ffb
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -1,6 +1,12 @@
{% include 'partials/notify.html' %}
<table class="table is-fullwidth is-hoverable" id="accounts-table">
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>user</th>
@ -35,6 +41,7 @@
hx-get="{% url 'account_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button is-info">
<span class="icon-text">
<span class="icon">
@ -47,6 +54,7 @@
hx-delete="{% url 'account_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
class="button is-danger">
<span class="icon-text">

@ -1,6 +1,12 @@
{% include 'partials/notify.html' %}
<table class="table is-fullwidth is-hoverable" id="hooks-table">
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>user</th>
@ -25,6 +31,7 @@
hx-get="{% url 'hook_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button is-info">
<span class="icon-text">
<span class="icon">
@ -37,6 +44,7 @@
hx-delete="{% url 'hook_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
class="button is-danger">
<span class="icon-text">

@ -1,6 +1,12 @@
{% include 'partials/notify.html' %}
<table class="table is-fullwidth is-hoverable" id="strategies-table">
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>name</th>
@ -37,6 +43,7 @@
hx-get="{% url 'strategy_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button is-info">
<span class="icon-text">
<span class="icon">
@ -49,6 +56,7 @@
hx-delete="{% url 'strategy_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
class="button is-danger">
<span class="icon-text">

@ -1,6 +1,12 @@
{% include 'partials/notify.html' %}
<table class="table is-fullwidth is-hoverable" id="trades-table">
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>status</th>
@ -31,6 +37,7 @@
hx-get="{% url 'trade_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button is-info">
<span class="icon-text">
<span class="icon">
@ -43,6 +50,7 @@
hx-delete="{% url 'trade_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
class="button is-danger">
<span class="icon-text">
<span class="icon">

@ -1,6 +0,0 @@
<h1 class="title is-4"></h1>
<h1 class="subtitle"></h1>
{% include 'partials/notify.html' %}
{% include 'partials/position-list.html' %}SSSSSSSSSSSSSSSSS

@ -19,12 +19,17 @@ class ObjectList(ListView):
model = None
context_object_name = "objects"
context_object_name_singular = "object"
page_title = None
page_subtitle = None
title = "Objects"
title_singular = "Object"
list_url_name = None
# WARNING: TAKEN FROM locals()
list_url_args = ["type"]
submit_url_name = None
# copied from BaseListView
@ -39,6 +44,12 @@ class ObjectList(ListView):
return HttpResponseBadRequest("Invalid type specified")
self.template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
list_url_args = {}
for arg in self.list_url_args:
list_url_args[arg] = locals()[arg]
print("LIST URL ARGS", list_url_args)
if type == "page":
type = "modal"
@ -55,6 +66,8 @@ class ObjectList(ListView):
if is_empty:
raise Http404("Empty list")
submit_url = reverse(self.submit_url_name, kwargs={"type": type})
list_url = reverse(self.list_url_name, kwargs=list_url_args)
context = self.get_context_data()
context["title"] = self.title + f" ({type})"
context["title_singular"] = self.title_singular
@ -65,6 +78,13 @@ class ObjectList(ListView):
context["page_subtitle"] = self.page_subtitle
context["type"] = type
context["submit_url"] = submit_url
context["list_url"] = list_url
context["context_object_name"] = self.context_object_name
context["context_object_name_singular"] = self.context_object_name_singular
# Return partials for HTMX
if self.request.htmx:
self.template_name = self.list_template
return self.render_to_response(context)
@ -77,6 +97,10 @@ class ObjectCreate(CreateView):
context_object_name = "objects"
submit_url_name = None
list_url_name = None
# WARNING: TAKEN FROM locals()
list_url_args = ["type"]
request = None
def form_valid(self, form):
@ -87,7 +111,9 @@ class ObjectCreate(CreateView):
obj.save()
form.save_m2m()
context = {"message": "Object created", "class": "success"}
return self.render_to_response(context)
response = self.render_to_response(context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response
def get(self, request, *args, **kwargs):
self.request = request
@ -98,16 +124,26 @@ class ObjectCreate(CreateView):
return HttpResponseBadRequest("Invalid type specified")
self.template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
list_url_args = {}
for arg in self.list_url_args:
list_url_args[arg] = locals()[arg]
print("LIST URL ARGS", list_url_args)
if type == "page":
type = "modal"
self.object = None
submit_url = reverse(self.submit_url_name, kwargs={"type": type})
list_url = reverse(self.list_url_name, kwargs=list_url_args)
context = self.get_context_data()
context["unique"] = unique
context["window_content"] = self.window_content
context["context_object_name"] = self.context_object_name
context["context_object_name_singular"] = self.context_object_name_singular
context["submit_url"] = submit_url
context["list_url"] = list_url
context["type"] = type
return self.render_to_response(context)
@ -143,7 +179,9 @@ class ObjectUpdate(UpdateView):
obj.save()
form.save_m2m()
context = {"message": "Object updated", "class": "success"}
return self.render_to_response(context)
response = self.render_to_response(context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response
def get(self, request, *args, **kwargs):
self.request = request
@ -166,6 +204,7 @@ class ObjectUpdate(UpdateView):
context["unique"] = unique
context["window_content"] = self.window_content
context["context_object_name"] = self.context_object_name
context["context_object_name_singular"] = self.context_object_name_singular
context["submit_url"] = submit_url
context["type"] = type
return self.render_to_response(context)
@ -178,6 +217,7 @@ class ObjectUpdate(UpdateView):
class ObjectDelete(DeleteView):
model = None
context_object_name_singular = "object"
template_name = "partials/notify.html"
# Overriden to prevent success URL from being used
@ -190,7 +230,9 @@ class ObjectDelete(DeleteView):
# success_url = self.get_success_url()
self.object.delete()
context = {"message": "Object deleted", "class": "success"}
return self.render_to_response(context)
response = self.render_to_response(context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response
# This will be used in newer Django versions, until then we get a warning
def form_valid(self, form):
@ -200,4 +242,6 @@ class ObjectDelete(DeleteView):
self.object = self.get_object()
self.object.delete()
context = {"message": "Object deleted", "class": "success"}
return self.render_to_response(context)
response = self.render_to_response(context)
response["HX-Trigger"] = f"{self.context_object_name_singular}Event"
return response

@ -63,10 +63,14 @@ class AccountList(LoginRequiredMixin, ObjectList):
list_template = "partials/account-list.html"
model = Account
context_object_name = "accounts"
context_object_name_singular = "account"
title = "Accounts"
title_singular = "Account"
page_title = "List of accounts"
list_url_name = "accounts"
list_url_args = ["type"]
submit_url_name = "account_create"
@ -74,6 +78,11 @@ class AccountCreate(LoginRequiredMixin, ObjectCreate):
model = Account
form_class = AccountForm
context_object_name = "accounts"
context_object_name_singular = "account"
list_url_name = "accounts"
list_url_args = ["type"]
submit_url_name = "account_create"
@ -94,8 +103,18 @@ class AccountUpdate(LoginRequiredMixin, ObjectUpdate):
model = Account
form_class = AccountForm
context_object_name = "accounts"
context_object_name_singular = "account"
list_url_name = "accounts"
list_url_args = ["type"]
submit_url_name = "account_update"
class AccountDelete(LoginRequiredMixin, ObjectDelete):
model = Account
context_object_name = "accounts"
context_object_name_singular = "account"
list_url_name = "accounts"
list_url_args = ["type"]

@ -104,11 +104,15 @@ class HookList(LoginRequiredMixin, ObjectList):
list_template = "partials/hook-list.html"
model = Hook
context_object_name = "hooks"
context_object_name_singular = "hook"
title = "Hooks"
title_singular = "Hook"
page_title = "List of active URL endpoints for receiving hooks."
page_subtitle = "Add URLs here to receive Drakdoo callbacks. Make then unique!"
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_create"
@ -116,6 +120,11 @@ class HookCreate(LoginRequiredMixin, ObjectCreate):
model = Hook
form_class = HookForm
context_object_name = "hooks"
context_object_name_singular = "hook"
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_create"
@ -123,8 +132,18 @@ class HookUpdate(LoginRequiredMixin, ObjectUpdate):
model = Hook
form_class = HookForm
context_object_name = "hooks"
context_object_name_singular = "hook"
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_update"
class HookDelete(LoginRequiredMixin, ObjectDelete):
model = Hook
context_object_name = "hooks"
context_object_name_singular = "hook"
list_url_name = "hooks"
list_url_args = ["type"]

@ -15,10 +15,14 @@ class StrategyList(LoginRequiredMixin, ObjectList):
list_template = "partials/strategy-list.html"
model = Strategy
context_object_name = "strategies"
context_object_name_singular = "strategy"
title = "Strategies"
title_singular = "Strategy"
page_title = "List of strategies"
list_url_name = "strategies"
list_url_args = ["type"]
submit_url_name = "strategy_create"
@ -26,6 +30,11 @@ class StrategyCreate(LoginRequiredMixin, ObjectCreate):
model = Strategy
form_class = StrategyForm
context_object_name = "strategies"
context_object_name_singular = "strategy"
list_url_name = "strategies"
list_url_args = ["type"]
submit_url_name = "strategy_create"
@ -33,8 +42,18 @@ class StrategyUpdate(LoginRequiredMixin, ObjectUpdate):
model = Strategy
form_class = StrategyForm
context_object_name = "strategies"
context_object_name_singular = "strategy"
list_url_name = "strategies"
list_url_args = ["type"]
submit_url_name = "strategy_update"
class StrategyDelete(LoginRequiredMixin, ObjectDelete):
model = Strategy
context_object_name = "strategies"
context_object_name_singular = "strategy"
list_url_name = "strategies"
list_url_args = ["type"]

@ -12,6 +12,7 @@ class TradeList(LoginRequiredMixin, ObjectList):
list_template = "partials/trade-list.html"
model = Trade
context_object_name = "trades"
context_object_name_singular = "trade"
title = "Trades"
title_singular = "Trade"
page_title = (
@ -19,6 +20,9 @@ class TradeList(LoginRequiredMixin, ObjectList):
)
page_subtitle = "Trades deleted here will not be closed on the exchange."
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_create"
@ -26,6 +30,11 @@ class TradeCreate(LoginRequiredMixin, ObjectCreate):
model = Trade
form_class = TradeForm
context_object_name = "trades"
context_object_name_singular = "trade"
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_create"
@ -33,8 +42,18 @@ class TradeUpdate(LoginRequiredMixin, ObjectUpdate):
model = Trade
form_class = TradeForm
context_object_name = "trades"
context_object_name_singular = "trade"
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_update"
class TradeDelete(LoginRequiredMixin, ObjectDelete):
model = Trade
context_object_name = "trades"
context_object_name_singular = "trade"
list_url_name = "trades"
list_url_args = ["type"]

Loading…
Cancel
Save