diff --git a/core/forms.py b/core/forms.py index c36c8d9..f5b82d3 100644 --- a/core/forms.py +++ b/core/forms.py @@ -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 = ( diff --git a/core/migrations/0045_hook_type.py b/core/migrations/0045_hook_type.py new file mode 100644 index 0000000..e0605c1 --- /dev/null +++ b/core/migrations/0045_hook_type.py @@ -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, + ), + ] diff --git a/core/models.py b/core/models.py index ee71a48..1908a3a 100644 --- a/core/models.py +++ b/core/models.py @@ -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})"