Implement viewing and altering trends
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user