Update tables when objects are changed

This commit is contained in:
2022-10-29 14:05:01 +01:00
parent c685b6d25f
commit 6902665ffb
10 changed files with 160 additions and 14 deletions

View File

@@ -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

View File

@@ -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"]

View File

@@ -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"]

View File

@@ -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"]

View File

@@ -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"]