Implement viewing and altering trends

Mark Veidemanis 1 year ago
parent b882ba15d0
commit 3a39181261
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -193,4 +193,19 @@ urlpatterns = [
limits.TradingTimeDelete.as_view(),
name="tradingtime_delete",
),
path(
"trend_directions/<str:strategy_id>/flip/<str:symbol>/",
limits.TrendDirectionFlip.as_view(),
name="trenddirection_flip",
),
path(
"trend_directions/<str:strategy_id>/delete/<str:symbol>/",
limits.TrendDirectionDelete.as_view(),
name="trenddirection_delete",
),
path(
"trend_directions/<str:type>/view/<str:strategy_id>/",
limits.TrendDirectionList.as_view(),
name="trenddirections",
),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

@ -267,9 +267,6 @@
</a>
<div class="navbar-dropdown">
<!-- <a class="navbar-item" href="#">
Directions
</a> -->
<a class="navbar-item" href="{% url 'tradingtimes' type='page' %}">
Trading Times
</a>

@ -65,12 +65,12 @@
</span>
</span>
</button>
<!-- {% if type == 'page' %}
<a href="#"><button
{% if type == 'page' %}
<a href="{% url 'trenddirections' type=type strategy_id=item.id %}"><button
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-eye"></i>
<span class="icon" data-tooltip="View trends">
<i class="fa-solid fa-arrows-up-down"></i>
</span>
</span>
</button>
@ -78,18 +78,18 @@
{% else %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="#"
hx-get="{% url 'trenddirections' type=type strategy_id=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-eye"></i>
<span class="icon" data-tooltip="View trends">
<i class="fa-solid fa-arrows-up-down"></i>
</span>
</span>
</button>
{% endif %} -->
{% endif %}
</div>
</td>
</tr>

@ -0,0 +1,55 @@
{% include 'partials/notify.html' %}
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>symbol</th>
<th>direction</th>
<th>actions</th>
</thead>
{% for key, item in object_list.items %}
<tr class="
{% if item == 'buy' %}has-background-success-light
{% elif item == 'sell' %}has-background-danger-light
{% endif %}">
<td>{{ key }}</td>
<td>{{ item }}</td>
<td>
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'trenddirection_flip' strategy_id=strategy_id symbol=key %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon" data-tooltip="Flip direction">
<i class="fa-solid fa-arrows-repeat"></i>
</span>
</span>
</button>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{% url 'trenddirection_delete' strategy_id=strategy_id symbol=key %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-xmark"></i>
</span>
</span>
</button>
</div>
</td>
</tr>
{% endfor %}
</table>

@ -126,6 +126,8 @@ class ObjectList(RestrictedViewMixin, ObjectNameMixin, ListView):
for arg in self.list_url_args:
if arg in locals():
list_url_args[arg] = locals()[arg]
elif arg in kwargs:
list_url_args[arg] = kwargs[arg]
orig_type = type
if type == "page":

@ -1,13 +1,91 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import View
from core.forms import TradingTimeForm
from core.models import TradingTime
from core.models import Strategy, TradingTime
from core.util import logs
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
from core.views import (
ObjectCreate,
ObjectDelete,
ObjectList,
ObjectNameMixin,
ObjectUpdate,
)
log = logs.get_logger(__name__)
# Trend directions
class TrendDirectionList(LoginRequiredMixin, ObjectList):
list_template = "partials/trend-direction-list.html"
# model = TrendDirection
page_title = "List of trend directions for a strategy."
page_subtitle = None
context_object_name_singular = "trend direction"
context_object_name = "trend directions"
list_url_name = "trenddirections"
list_url_args = ["type", "strategy_id"]
# submit_url_name = "trenddirection_create"
def get_queryset(self, **kwargs):
strategy_id = kwargs.get("strategy_id", None)
self.extra_context = {"strategy_id": strategy_id}
try:
strategy = Strategy.objects.get(id=strategy_id, user=self.request.user)
except Strategy.DoesNotExist:
context = {"message": "Strategy does not exist", "class": "danger"}
return self.render_to_response(context)
return strategy.trends
class TrendDirectionFlip(LoginRequiredMixin, ObjectNameMixin, View):
template_name = "partials/notify.html"
model = Strategy
def get(self, request, strategy_id, symbol):
try:
strategy = Strategy.objects.get(id=strategy_id, user=request.user)
except Strategy.DoesNotExist:
context = {"message": "Strategy does not exist", "class": "danger"}
return render(request, self.template_name, context)
if symbol not in strategy.trends:
context = {"message": "Trend direction does not exist", "class": "danger"}
return render(request, self.template_name, context)
if strategy.trends[symbol] == "buy":
strategy.trends[symbol] = "sell"
elif strategy.trends[symbol] == "sell":
strategy.trends[symbol] = "buy"
strategy.save()
context = {"message": "Trend direction updated", "class": "success"}
return render(request, self.template_name, context)
class TrendDirectionDelete(LoginRequiredMixin, ObjectNameMixin, View):
template_name = "partials/notify.html"
model = Strategy
def delete(self, request, strategy_id, symbol):
try:
strategy = Strategy.objects.get(id=strategy_id, user=request.user)
except Strategy.DoesNotExist:
context = {"message": "Strategy does not exist", "class": "danger"}
return render(request, self.template_name, context)
if symbol not in strategy.trends:
context = {"message": "Trend direction does not exist", "class": "danger"}
return render(request, self.template_name, context)
del strategy.trends[symbol]
strategy.save()
context = {"message": "Trend direction deleted", "class": "success"}
return render(request, self.template_name, context)
# Trading times
class TradingTimeList(LoginRequiredMixin, ObjectList):
list_template = "partials/trading-time-list.html"
model = TradingTime

Loading…
Cancel
Save