Make account required for trade information

This commit is contained in:
Mark Veidemanis 2022-12-08 07:20:46 +00:00
parent 312ddb4dc1
commit 633894ae75
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
5 changed files with 15 additions and 7 deletions

View File

@ -124,7 +124,7 @@ urlpatterns = [
name="trade_update",
),
path(
"trades/<str:type>/view/<str:trade_id>/",
"trades/<str:type>/view/<str:account_id>/<str:trade_id>/",
trades.TradeAction.as_view(),
name="trade_action",
),

View File

@ -103,7 +103,10 @@ class OANDAExchange(BaseExchange):
def get_position_info(self, symbol):
r = positions.PositionDetails(self.account_id, symbol)
return self.call(r)
response = self.call(r)
response["account"] = self.account.name
response["account_id"] = self.account.id
return response
def get_all_positions(self):
items = []

View File

@ -235,12 +235,16 @@ class Trade(models.Model):
return cls.objects.get(id=trade_id, user=user)
@classmethod
def get_by_id_or_order(cls, trade_id, user):
def get_by_id_or_order(cls, trade_id, account_id, user):
try:
return cls.objects.get(id=trade_id, user=user)
account = Account.objects.get(id=account_id, user=user)
except Account.DoesNotExist:
return None
try:
return cls.objects.get(id=trade_id, account=account, user=user)
except cls.DoesNotExist:
try:
return cls.objects.get(order_id=trade_id, user=user)
return cls.objects.get(order_id=trade_id, account=account, user=user)
except cls.DoesNotExist:
return None

View File

@ -10,7 +10,7 @@
{% for trade_id in item %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'trade_action' type=type trade_id=trade_id %}"
hx-get="{% url 'trade_action' type=type trade_id=trade_id account_id=object.account_id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"

View File

@ -29,7 +29,8 @@ class TradeAction(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
def get_object(self, **kwargs):
trade_id = kwargs.get("trade_id")
db_info = Trade.get_by_id_or_order(trade_id, self.request.user)
account_id = kwargs.get("account_id")
db_info = Trade.get_by_id_or_order(trade_id, account_id, self.request.user)
if db_info is None:
return HttpResponseBadRequest("Trade not found.")
if db_info.order_id is not None: