Skip to content

Commit

Permalink
Improve TraderoBot UX
Browse files Browse the repository at this point in the history
- Add possibility of multiple bots when creating
  • Loading branch information
math-a3k committed Aug 30, 2023
1 parent c653d68 commit e92d2c6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
13 changes: 12 additions & 1 deletion base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ class TraderoBotForm(forms.ModelForm):
choices=[(None, "(Selecionar)")]
+ [(k, v.__name__) for k, v in get_strategies().items()]
)
bots_quantity = forms.IntegerField(
label="# of Botzinhos",
initial=1,
required=False,
help_text="Create several Botzinhos using this as a template",
)

class Meta:
model = TraderoBot
fields = [
"bots_quantity",
"group",
"name",
"is_dummy",
Expand All @@ -39,17 +46,21 @@ class Meta:

def __init__(self, *args, for_group=False, for_group_edit=False, **kwargs):
user = kwargs.pop("user", None)
for_edit = kwargs.pop("for_edit", None)
super().__init__(*args, **kwargs)
if for_edit:
self.fields.pop("bots_quantity")
if for_group:
self.fields.pop("group")
self.fields.pop("bots_quantity")
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
)
).order_by("name")


class TraderoBotGroupForm(forms.ModelForm):
Expand Down
2 changes: 1 addition & 1 deletion base/templates/base/botzinhos.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h4 class="mt-1 mb-0 p-2 text-light">GROUP: <a class="text-warning" href="{% url
</div>

<div class="container">
<a class="btn btn-primary btn-lg" href="{% url "base:botzinhos-create" %}" role="button">Novo Botzinho</a> &nbsp;
<a class="btn btn-primary btn-lg" href="{% url "base:botzinhos-create" %}" role="button">Novo(s) Botzinho(s)</a> &nbsp;
<a class="btn btn-primary btn-lg" href="{% url "base:botzinhos-group-create" %}" role="button">Novo Grupo</a>
</div>

Expand Down
2 changes: 1 addition & 1 deletion base/templates/base/botzinhos_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% block content %}
<div class="container">
<h3>{% if form.instance.pk %}Atualizar{% else %}Novo{% endif %} Botzinho</h3>
<h3>{% if form.instance.pk %}Atualizar{% else %}Novo(s){% endif %} Botzinho(s)</h3>
<form method="post">{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
Expand Down
2 changes: 1 addition & 1 deletion base/templates/base/botzinhos_group_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ <h4 class="m-2">Group Actions</h4>

<div class="btn-group">
<a class="btn btn-warning" href="{% url "base:botzinhos-group-update" group.pk %}">Atualizar Grupo</a>
<a class="btn btn-primary" href="{% url "base:botzinhos-create" %}?group={{group.pk}}">Novo Botzinho</a>
<a class="btn btn-primary" href="{% url "base:botzinhos-create" %}?group={{group.pk}}">Novo(s) Botzinho(s)</a>
</div>
<div class="btn-group">
{% url "base:botzinhos-group-action" group.pk "off" as action_url %}
Expand Down
20 changes: 15 additions & 5 deletions base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def test_botzinhos_create(self):
response = self.client.post(
url,
{
"bots_quantity": 1,
"name": "testing botzinho",
"group": self.group1.pk,
"symbol": self.s1.pk,
Expand All @@ -278,12 +279,15 @@ def test_botzinhos_create(self):
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
TraderoBot.objects.filter(name="testing botzinho").count(),
TraderoBot.objects.filter(
name__startswith="testing botzinho"
).count(),
1,
)
response = self.client.post(
url,
{
"bots_quantity": 1,
"name": "testing botzinho 2",
"group": self.group1.pk,
"symbol": self.s1.pk,
Expand All @@ -302,6 +306,7 @@ def test_botzinhos_create(self):
response = self.client.post(
url,
{
"bots_quantity": 1,
"name": "testing botzinho 2",
"group": self.group1.pk,
"symbol": self.s1.pk,
Expand All @@ -320,25 +325,28 @@ def test_botzinhos_create(self):
response = self.client.post(
url,
{
"name": "testing botzinho 2",
"bots_quantity": 2,
"group": self.group1.pk,
"symbol": self.s1.pk,
"strategy": "acmadness",
"strategy_params": "microgain=0.3",
"fund_quote_asset_initial": Decimal("20"),
"fund_quote_asset_initial": Decimal("3.1416") * 10,
"jumpy_whitelist": "s1busd",
"jumpy_blacklist": "s1busd",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
TraderoBot.objects.filter(name="testing botzinho 2").count(),
1,
TraderoBot.objects.filter(
fund_quote_asset_initial=Decimal("3.1416") * 10
).count(),
2,
)
response = self.client.post(
url,
{
"bots_quantity": 1,
"name": "testing botzinho 2",
"group": self.group1.pk,
"symbol": self.s1.pk,
Expand All @@ -355,6 +363,7 @@ def test_botzinhos_create(self):
response = self.client.post(
url,
{
"bots_quantity": 1,
"name": "testing botzinho 2",
"group": self.group1.pk,
"symbol": self.s1.pk,
Expand All @@ -372,6 +381,7 @@ def test_botzinhos_create(self):
response = self.client.post(
url,
{
"bots_quantity": 1,
"name": "testing botzinho 2",
"group": self.group1.pk,
"symbol": self.s1.pk,
Expand Down
22 changes: 19 additions & 3 deletions base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from django.core.paginator import Paginator
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import (
CreateView,
DetailView,
FormView,
ListView,
RedirectView,
TemplateView,
Expand Down Expand Up @@ -152,7 +154,7 @@ def get_context_data(self, **kwargs):
return context


class BotzinhosCreateView(LoginRequiredMixin, CreateView):
class BotzinhosCreateView(LoginRequiredMixin, FormView):
model = TraderoBot
form_class = TraderoBotForm
template_name = "base/botzinhos_form.html"
Expand All @@ -165,14 +167,28 @@ def get_initial(self):
return initial

def form_valid(self, form):
form.instance.user = self.request.user
bot_kwargs = form.cleaned_data
bot_kwargs.update({"user": self.request.user})
bot_name = bot_kwargs.pop("name", None)
bots_quantity = bot_kwargs.pop("bots_quantity", 1)
for i in range(bots_quantity):
if bot_name:
bot_kwargs.update({"name": f"{bot_name}-{i+1:03d}"})
bot = TraderoBot(**bot_kwargs)
bot.save()
self.group = bot_kwargs.pop("group")
return super().form_valid(form)

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update(user=self.request.user)
return kwargs

def get_success_url(self):
return reverse(
"base:botzinhos-group-detail", kwargs={"pk": self.group.pk}
)


class BotzinhosUpdateView(OwnerMixin, LoginRequiredMixin, UpdateView):
model = TraderoBot
Expand All @@ -181,7 +197,7 @@ class BotzinhosUpdateView(OwnerMixin, LoginRequiredMixin, UpdateView):

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update(user=self.request.user)
kwargs.update(user=self.request.user, for_edit=True)
return kwargs


Expand Down

0 comments on commit e92d2c6

Please sign in to comment.