Skip to content

Commit

Permalink
Improve TraderoBotGroup
Browse files Browse the repository at this point in the history
- Add missing bot validation when updating through group
  • Loading branch information
math-a3k committed Aug 29, 2023
1 parent e9aba14 commit c98c9f1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
39 changes: 36 additions & 3 deletions base/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal

from django import forms
from django.core.exceptions import ValidationError
from django.db.models import Model

from .models import Symbol, TraderoBot, TraderoBotGroup, User
Expand Down Expand Up @@ -36,11 +37,15 @@ class Meta:
"should_stop",
]

def __init__(self, *args, for_group=False, **kwargs):
def __init__(self, *args, for_group=False, for_group_edit=False, **kwargs):
user = kwargs.pop("user", None)
super().__init__(*args, **kwargs)
if for_group:
self.fields.pop("group")
if for_group_edit:
self.fields.pop("symbol")
for field in self.fields:
self.fields[field].required = False
else:
self.fields["group"].queryset = TraderoBotGroup.objects.filter(
user=user
Expand Down Expand Up @@ -68,7 +73,7 @@ class Meta:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["add_edit_bots"].label = "Add Botzinhos"
bot_form_fields = TraderoBotForm(for_group=True).fields
bot_form_fields = self.get_bot_form().fields
for field in bot_form_fields:
new_field = f"{self.prefix_bot_data}{field}"
self.fields[new_field] = bot_form_fields[field]
Expand All @@ -95,8 +100,12 @@ def get_bot_data(self, data_dict):
k[len(self.prefix_bot_data) :]: v
for k, v in data_dict.items()
if k.startswith(self.prefix_bot_data)
and data_dict[k] not in self.fields[k].empty_values
}

def get_bot_form(self):
return TraderoBotForm(for_group=True)


class TraderoBotGroupEditForm(TraderoBotGroupForm):
def __init__(self, *args, **kwargs):
Expand All @@ -119,7 +128,31 @@ def __init__(self, *args, **kwargs):
self.fields[field].required = False

def clean(self):
pass
cleaned_data = super(TraderoBotGroupForm, self).clean()
if cleaned_data["add_edit_bots"]:
bot_data = self.get_bot_data(cleaned_data)
bots = self.instance.bots.all()
for bot in bots:
for field in bot_data:
setattr(bot, field, bot_data[field])
try:
bot.full_clean()
except ValidationError as ve:
self.add_error(
None,
"The following fields produce erros with at least one "
"bot of the group:",
)
for field, message in ve.message_dict.items():
self.add_error(
f"{self.prefix_bot_data}{field}", message
)

break
return cleaned_data

def get_bot_form(self):
return TraderoBotForm(for_group=True, for_group_edit=True)


class UserForm(forms.ModelForm):
Expand Down
3 changes: 2 additions & 1 deletion base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2116,7 +2116,8 @@ def clean(self):
raise ValidationError(
{
"strategy_params": (
"Unrecognized parameters or format for the strategy"
"Unrecognized parameters or format for the strategy "
"- or missing symbol for the bot"
)
}
)
Expand Down
14 changes: 13 additions & 1 deletion base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ def test_botzinhos_group_update(self):
TraderoBot.objects.filter(name__startswith="Group 1").count(),
3,
)
# import ipdb; ipdb.set_trace()
response = self.client.post(
url,
{
Expand All @@ -531,6 +530,19 @@ def test_botzinhos_group_update(self):
).count(),
3,
)
response = self.client.post(
url,
{
"add_edit_bots": True,
"bot_fund_quote_asset_initial": 5,
},
follow=True,
)
self.assertEqual(response.status_code, 200)
self.assertIn(
b"The following fields produce erros",
response.content,
)

def test_botzinhos_actions(self):
self.client.force_login(self.user1)
Expand Down
10 changes: 2 additions & 8 deletions base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def form_valid(self, form):
bot_kwargs.update(
{"user": self.request.user, "group": self.object}
)
bot_name = bot_kwargs["name"]
bot_name = bot_kwargs.get("name", None)
for i in range(form.cleaned_data["bots_quantity"]):
if bot_name:
bot_kwargs.update({"name": f"{bot_name}-{i+1:03d}"})
Expand All @@ -335,13 +335,7 @@ def form_valid(self, form):
if bot_name:
bot.name = f"{bot_name}-{index + 1:03d}"
for field in bot_data:
if (
bot_data[field]
not in form.fields[
f"{form.prefix_bot_data}{field}"
].empty_values
):
setattr(bot, field, bot_data[field])
setattr(bot, field, bot_data[field])
bot.save()
return super().form_valid(form)

Expand Down

0 comments on commit c98c9f1

Please sign in to comment.