2023-01-11 20:43:11 +00:00
|
|
|
from decimal import Decimal as D
|
|
|
|
|
2022-11-29 07:20:39 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2023-02-10 20:49:35 +00:00
|
|
|
from mixins.views import ObjectList
|
2022-11-29 07:20:39 +00:00
|
|
|
from two_factor.views.mixins import OTPRequiredMixin
|
|
|
|
|
|
|
|
from core.exchanges import GenericAPIError
|
2022-11-29 07:20:39 +00:00
|
|
|
from core.models import Account
|
2022-11-29 07:20:39 +00:00
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
2022-11-29 07:20:39 +00:00
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
class Profit(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
2022-11-29 07:20:39 +00:00
|
|
|
list_template = "partials/profit-list.html"
|
|
|
|
page_title = "Profit by account"
|
|
|
|
page_subtitle = None
|
2022-12-08 07:20:46 +00:00
|
|
|
|
2022-11-29 07:20:39 +00:00
|
|
|
context_object_name_singular = "profit"
|
|
|
|
context_object_name = "profit"
|
|
|
|
|
2022-12-08 07:20:46 +00:00
|
|
|
list_url_name = "profit"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
|
|
|
def get_queryset(self, **kwargs):
|
|
|
|
items = []
|
2023-01-01 15:46:40 +00:00
|
|
|
# Only get enabled accounts
|
|
|
|
accounts = Account.objects.filter(user=self.request.user, enabled=True)
|
2022-12-08 07:20:46 +00:00
|
|
|
for account in accounts:
|
|
|
|
try:
|
|
|
|
details = account.client.get_account()
|
|
|
|
item = {
|
|
|
|
"account": account,
|
2023-01-11 20:43:11 +00:00
|
|
|
"pl": D(account.initial_balance) - D(details["balance"]),
|
|
|
|
"unrealizedPL": D(details["unrealizedPL"]),
|
2022-12-08 07:20:46 +00:00
|
|
|
"balance": details["balance"],
|
|
|
|
"currency": details["currency"],
|
|
|
|
}
|
|
|
|
items.append(item)
|
|
|
|
except GenericAPIError:
|
|
|
|
continue
|
|
|
|
return items
|