Make variables passed to CRUD helpers consistent

This commit is contained in:
Mark Veidemanis 2022-12-18 17:37:34 +00:00
parent 246674b03e
commit 8de99c1bcd
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
8 changed files with 25 additions and 65 deletions

View File

@ -185,15 +185,18 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
window_content = "window-content/object-form.html" window_content = "window-content/object-form.html"
parser_classes = [FormParser] parser_classes = [FormParser]
page_title = None
page_subtitle = None
model = None model = None
submit_url_name = None submit_url_name = None
submit_url_args = ["type"]
list_url_name = None
# WARNING: TAKEN FROM locals()
list_url_args = ["type"]
request = None request = None
# Whether to hide the cancel button in the form
hide_cancel = False
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.title = "Create " + self.context_object_name_singular self.title = "Create " + self.context_object_name_singular
@ -230,17 +233,22 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
self.request = request self.request = request
self.kwargs = kwargs self.kwargs = kwargs
list_url_args = {} if type == "widget":
for arg in self.list_url_args: self.hide_cancel = True
list_url_args[arg] = locals()[arg]
if type == "page": if type == "page":
type = "modal" type = "modal"
self.object = None self.object = None
submit_url = reverse(self.submit_url_name, kwargs={"type": type})
list_url = reverse(self.list_url_name, kwargs=list_url_args) submit_url_args = {}
for arg in self.submit_url_args:
if arg in locals():
submit_url_args[arg] = locals()[arg]
elif arg in kwargs:
submit_url_args[arg] = kwargs[arg]
submit_url = reverse(self.submit_url_name, kwargs=submit_url_args)
context = self.get_context_data() context = self.get_context_data()
form = kwargs.get("form", None) form = kwargs.get("form", None)
if form: if form:
@ -250,8 +258,12 @@ class ObjectCreate(RestrictedViewMixin, ObjectNameMixin, CreateView):
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
context["submit_url"] = submit_url context["submit_url"] = submit_url
context["list_url"] = list_url
context["type"] = type context["type"] = type
context["hide_cancel"] = self.hide_cancel
if self.page_title:
context["page_title"] = self.page_title
if self.page_subtitle:
context["page_subtitle"] = self.page_subtitle
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"
return response return response
@ -392,6 +404,9 @@ class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
return HttpResponseBadRequest("Invalid type specified") return HttpResponseBadRequest("Invalid type specified")
self.template_name = f"wm/{type}.html" self.template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8] unique = str(uuid.uuid4())[:8]
if type == "widget":
self.hide_cancel = True
if type == "page": if type == "page":
type = "modal" type = "modal"

View File

@ -66,9 +66,6 @@ class AccountCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Account model = Account
form_class = AccountForm form_class = AccountForm
list_url_name = "accounts"
list_url_args = ["type"]
submit_url_name = "account_create" submit_url_name = "account_create"
@ -76,14 +73,8 @@ class AccountUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Account model = Account
form_class = AccountForm form_class = AccountForm
list_url_name = "accounts"
list_url_args = ["type"]
submit_url_name = "account_update" submit_url_name = "account_update"
class AccountDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete): class AccountDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Account model = Account
list_url_name = "accounts"
list_url_args = ["type"]

View File

@ -163,9 +163,6 @@ class HookCreate(LoginRequiredMixin, ObjectCreate):
model = Hook model = Hook
form_class = HookForm form_class = HookForm
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_create" submit_url_name = "hook_create"
@ -173,14 +170,8 @@ class HookUpdate(LoginRequiredMixin, ObjectUpdate):
model = Hook model = Hook
form_class = HookForm form_class = HookForm
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_update" submit_url_name = "hook_update"
class HookDelete(LoginRequiredMixin, ObjectDelete): class HookDelete(LoginRequiredMixin, ObjectDelete):
model = Hook model = Hook
list_url_name = "hooks"
list_url_args = ["type"]

View File

@ -99,9 +99,6 @@ class TradingTimeCreate(LoginRequiredMixin, ObjectCreate):
model = TradingTime model = TradingTime
form_class = TradingTimeForm form_class = TradingTimeForm
list_url_name = "tradingtimes"
list_url_args = ["type"]
submit_url_name = "tradingtime_create" submit_url_name = "tradingtime_create"
@ -109,14 +106,8 @@ class TradingTimeUpdate(LoginRequiredMixin, ObjectUpdate):
model = TradingTime model = TradingTime
form_class = TradingTimeForm form_class = TradingTimeForm
list_url_name = "tradingtimes"
list_url_args = ["type"]
submit_url_name = "tradingtime_update" submit_url_name = "tradingtime_update"
class TradingTimeDelete(LoginRequiredMixin, ObjectDelete): class TradingTimeDelete(LoginRequiredMixin, ObjectDelete):
model = TradingTime model = TradingTime
list_url_name = "tradingtimes"
list_url_args = ["type"]

View File

@ -16,9 +16,6 @@ class NotificationsUpdate(LoginRequiredMixin, ObjectUpdate):
"At least the topic must be set if you want to receive notifications." "At least the topic must be set if you want to receive notifications."
) )
# list_url_name = "notifications"
# list_url_args = ["type"]
submit_url_name = "notifications_update" submit_url_name = "notifications_update"
submit_url_args = ["type"] submit_url_args = ["type"]

View File

@ -26,9 +26,6 @@ class SignalCreate(LoginRequiredMixin, ObjectCreate):
model = Signal model = Signal
form_class = SignalForm form_class = SignalForm
list_url_name = "signals"
list_url_args = ["type"]
submit_url_name = "signal_create" submit_url_name = "signal_create"
@ -36,14 +33,8 @@ class SignalUpdate(LoginRequiredMixin, ObjectUpdate):
model = Signal model = Signal
form_class = SignalForm form_class = SignalForm
list_url_name = "signals"
list_url_args = ["type"]
submit_url_name = "signal_update" submit_url_name = "signal_update"
class SignalDelete(LoginRequiredMixin, ObjectDelete): class SignalDelete(LoginRequiredMixin, ObjectDelete):
model = Signal model = Signal
list_url_name = "signals"
list_url_args = ["type"]

View File

@ -25,8 +25,6 @@ class StrategyList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
class StrategyCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate): class StrategyCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Strategy model = Strategy
form_class = StrategyForm form_class = StrategyForm
list_url_name = "strategies"
list_url_args = ["type"]
submit_url_name = "strategy_create" submit_url_name = "strategy_create"
@ -35,14 +33,8 @@ class StrategyUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Strategy model = Strategy
form_class = StrategyForm form_class = StrategyForm
list_url_name = "strategies"
list_url_args = ["type"]
submit_url_name = "strategy_update" submit_url_name = "strategy_update"
class StrategyDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete): class StrategyDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Strategy model = Strategy
list_url_name = "strategies"
list_url_args = ["type"]

View File

@ -74,9 +74,6 @@ class TradeList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
class TradeCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate): class TradeCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Trade model = Trade
form_class = TradeForm form_class = TradeForm
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_create" submit_url_name = "trade_create"
def post_save(self, obj): def post_save(self, obj):
@ -87,8 +84,6 @@ class TradeCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
class TradeUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate): class TradeUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Trade model = Trade
form_class = TradeForm form_class = TradeForm
list_url_name = "trades"
list_url_args = ["type"]
submit_url_name = "trade_update" submit_url_name = "trade_update"
@ -96,9 +91,6 @@ class TradeUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
class TradeDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete): class TradeDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Trade model = Trade
list_url_name = "trades"
list_url_args = ["type"]
class TradeDeleteAll(LoginRequiredMixin, OTPRequiredMixin, ObjectNameMixin, View): class TradeDeleteAll(LoginRequiredMixin, OTPRequiredMixin, ObjectNameMixin, View):
template_name = "partials/notify.html" template_name = "partials/notify.html"