90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from two_factor.views.mixins import OTPRequiredMixin
|
|
|
|
from core.forms import AccountForm
|
|
from core.models import Account
|
|
from core.util import logs
|
|
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectRead, ObjectUpdate
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
class AccountInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
|
|
context_object_name_singular = "account"
|
|
context_object_name = "accounts"
|
|
|
|
detail_url_name = "account_info"
|
|
detail_url_args = ["type", "pk"]
|
|
|
|
VIEWABLE_FIELDS_MODEL = [
|
|
"name",
|
|
"exchange",
|
|
"currency",
|
|
"api_key",
|
|
"sandbox",
|
|
"supported_symbols",
|
|
# "instruments",
|
|
]
|
|
|
|
def get_object(self, **kwargs):
|
|
pk = kwargs.get("pk")
|
|
try:
|
|
account = Account.get_by_id(pk, self.request.user)
|
|
except Account.DoesNotExist:
|
|
message = "Account does not exist"
|
|
message_class = "danger"
|
|
context = {
|
|
"message": message,
|
|
"message_class": message_class,
|
|
"window_content": self.window_content,
|
|
}
|
|
return self.render_to_response(context)
|
|
live_info = account.client.get_account()
|
|
live_info = live_info
|
|
account_info = account.__dict__
|
|
account_info = {
|
|
k: v for k, v in account_info.items() if k in self.VIEWABLE_FIELDS_MODEL
|
|
}
|
|
account_info["supported_symbols"] = ", ".join(account_info["supported_symbols"])
|
|
|
|
self.extra_context = {"live": live_info}
|
|
return account_info
|
|
|
|
|
|
class AccountList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
|
|
list_template = "partials/account-list.html"
|
|
model = Account
|
|
page_title = "List of accounts"
|
|
|
|
list_url_name = "accounts"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "account_create"
|
|
|
|
|
|
class AccountCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
|
|
model = Account
|
|
form_class = AccountForm
|
|
|
|
list_url_name = "accounts"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "account_create"
|
|
|
|
|
|
class AccountUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
|
|
model = Account
|
|
form_class = AccountForm
|
|
|
|
list_url_name = "accounts"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "account_update"
|
|
|
|
|
|
class AccountDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
|
|
model = Account
|
|
|
|
list_url_name = "accounts"
|
|
list_url_args = ["type"]
|