Don't show all signals on strategy page

This commit is contained in:
Mark Veidemanis 2022-12-01 07:20:35 +00:00
parent baed991eca
commit 2ee5f7b937
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 40 additions and 2 deletions

View File

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

View File

@ -0,0 +1,19 @@
# Generated by Django 4.1.3 on 2022-12-07 09:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0044_strategy_trends'),
]
operations = [
migrations.AddField(
model_name='hook',
name='type',
field=models.CharField(choices=[('entry', 'Entry'), ('exit', 'Exit'), ('trend', 'Trend')], default='entry', max_length=255),
preserve_default=False,
),
]

View File

@ -35,6 +35,11 @@ DAY_CHOICES = (
(6, "Saturday"),
(7, "Sunday"),
)
HOOK_TYPE_CHOICES = (
("entry", "Entry"),
("exit", "Exit"),
("trend", "Trend"),
)
class Plan(models.Model):
@ -169,6 +174,7 @@ class Hook(models.Model):
name = models.CharField(max_length=1024)
hook = models.CharField(max_length=255, unique=True) # hook URL
received = models.IntegerField(default=0)
type = models.CharField(choices=HOOK_TYPE_CHOICES, max_length=255)
def __str__(self):
return f"{self.name} ({self.hook})"