2022-10-17 17:56:16 +00:00
|
|
|
import uuid
|
2022-11-10 07:20:20 +00:00
|
|
|
|
2022-10-17 17:56:16 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2022-10-17 06:20:30 +00:00
|
|
|
from django.http import HttpResponseBadRequest
|
2022-10-17 17:56:16 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
2022-11-28 19:45:22 +00:00
|
|
|
from two_factor.views.mixins import OTPRequiredMixin
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
from core.forms import AccountForm
|
2022-10-17 06:20:30 +00:00
|
|
|
from core.models import Account
|
2022-10-17 17:56:16 +00:00
|
|
|
from core.util import logs
|
2022-10-29 11:43:13 +00:00
|
|
|
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, View):
|
2022-11-04 07:20:14 +00:00
|
|
|
VIEWABLE_FIELDS_MODEL = [
|
|
|
|
"name",
|
|
|
|
"exchange",
|
2022-11-10 19:27:46 +00:00
|
|
|
"currency",
|
2022-11-04 07:20:14 +00:00
|
|
|
"api_key",
|
|
|
|
"sandbox",
|
|
|
|
"supported_symbols",
|
2022-11-10 19:27:46 +00:00
|
|
|
"instruments",
|
2022-11-04 07:20:14 +00:00
|
|
|
]
|
2022-10-21 22:57:32 +00:00
|
|
|
allowed_types = ["modal", "widget", "window", "page"]
|
|
|
|
window_content = "window-content/account-info.html"
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
def get(self, request, type, pk):
|
2022-10-21 22:57:32 +00:00
|
|
|
"""
|
|
|
|
Get the account details.
|
|
|
|
:param account_id: The id of the account.
|
|
|
|
"""
|
|
|
|
if type not in self.allowed_types:
|
|
|
|
return HttpResponseBadRequest
|
|
|
|
template_name = f"wm/{type}.html"
|
|
|
|
unique = str(uuid.uuid4())[:8]
|
|
|
|
try:
|
2022-10-29 11:43:13 +00:00
|
|
|
account = Account.get_by_id(pk, request.user)
|
2022-10-21 22:57:32 +00:00
|
|
|
except Account.DoesNotExist:
|
|
|
|
message = "Account does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
return render(request, template_name, context)
|
|
|
|
|
2022-11-04 07:20:42 +00:00
|
|
|
live_info = account.client.get_account()
|
2022-10-30 19:11:07 +00:00
|
|
|
live_info = live_info
|
2022-10-21 22:57:32 +00:00
|
|
|
account_info = account.__dict__
|
2022-10-21 23:15:27 +00:00
|
|
|
account_info = {
|
|
|
|
k: v for k, v in account_info.items() if k in self.VIEWABLE_FIELDS_MODEL
|
|
|
|
}
|
2022-11-04 07:20:14 +00:00
|
|
|
account_info["supported_symbols"] = ", ".join(account_info["supported_symbols"])
|
2022-10-21 22:57:32 +00:00
|
|
|
|
|
|
|
if type == "page":
|
|
|
|
type = "modal"
|
|
|
|
context = {
|
|
|
|
"db_info": account_info,
|
|
|
|
"live_info": live_info,
|
2022-10-29 11:43:13 +00:00
|
|
|
"pk": pk,
|
2022-10-21 22:57:32 +00:00
|
|
|
"type": type,
|
|
|
|
"unique": unique,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, template_name, context)
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-10-21 23:15:27 +00:00
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class AccountList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
2022-10-29 11:43:13 +00:00
|
|
|
list_template = "partials/account-list.html"
|
|
|
|
model = Account
|
|
|
|
page_title = "List of accounts"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
2022-10-29 13:05:01 +00:00
|
|
|
list_url_name = "accounts"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = "account_create"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class AccountCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = Account
|
|
|
|
form_class = AccountForm
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
list_url_name = "accounts"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = "account_create"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
# class AccountRead(LoginRequiredMixin, ObjectRead):
|
|
|
|
# model = Account
|
|
|
|
# context_object_name = "accounts"
|
|
|
|
# submit_url_name = "account_read"
|
|
|
|
# fields = (
|
|
|
|
# "name",
|
|
|
|
# "exchange",
|
|
|
|
# "api_key",
|
|
|
|
# "api_secret",
|
|
|
|
# "sandbox",
|
|
|
|
# )
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class AccountUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = Account
|
|
|
|
form_class = AccountForm
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
list_url_name = "accounts"
|
|
|
|
list_url_args = ["type"]
|
|
|
|
|
2022-10-29 11:43:13 +00:00
|
|
|
submit_url_name = "account_update"
|
2022-10-17 17:56:16 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:45:22 +00:00
|
|
|
class AccountDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
|
2022-10-29 11:43:13 +00:00
|
|
|
model = Account
|
2022-10-29 13:05:01 +00:00
|
|
|
|
|
|
|
list_url_name = "accounts"
|
|
|
|
list_url_args = ["type"]
|