Don't show all signals on strategy page
This commit is contained in:
@@ -11,9 +11,14 @@ from .models import Account, Hook, Signal, Strategy, Trade, TradingTime, User
|
||||
class RestrictedFormMixin:
|
||||
"""
|
||||
This mixin is used to restrict the queryset of a form to the current user.
|
||||
The request object is passed from the view."""
|
||||
The request object is passed from the view.
|
||||
Fieldargs is used to pass additional arguments to the queryset filter.
|
||||
"""
|
||||
|
||||
fieldargs = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# self.fieldargs = {}
|
||||
self.request = kwargs.pop("request")
|
||||
super().__init__(*args, **kwargs)
|
||||
for field in self.fields:
|
||||
@@ -27,7 +32,7 @@ class RestrictedFormMixin:
|
||||
model._meta.get_field("user")
|
||||
# Add the user to the queryset filters
|
||||
self.fields[field].queryset = model.objects.filter(
|
||||
user=self.request.user
|
||||
user=self.request.user, **self.fieldargs.get(field, {})
|
||||
)
|
||||
except FieldDoesNotExist:
|
||||
pass
|
||||
@@ -67,10 +72,12 @@ class HookForm(RestrictedFormMixin, ModelForm):
|
||||
fields = (
|
||||
"name",
|
||||
"hook",
|
||||
"type",
|
||||
)
|
||||
help_texts = {
|
||||
"name": "Name of the hook. Informational only.",
|
||||
"hook": "The URL slug to use for the hook. Make it unique.",
|
||||
"type": "Whether the hook is used for entering or exiting trades, or determining the trend.",
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +118,12 @@ class AccountForm(RestrictedFormMixin, ModelForm):
|
||||
|
||||
|
||||
class StrategyForm(RestrictedFormMixin, ModelForm):
|
||||
fieldargs = {
|
||||
"entry_signals": {"hook__type": "entry"},
|
||||
"exit_signals": {"hook__type": "exit"},
|
||||
"trend_signals": {"hook__type": "trend"},
|
||||
}
|
||||
|
||||
class Meta:
|
||||
model = Strategy
|
||||
fields = (
|
||||
|
||||
Reference in New Issue
Block a user