fisk/core/views/accounts.py

209 lines
6.7 KiB
Python
Raw Normal View History

2022-10-17 17:56:16 +00:00
import uuid
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-10-17 06:20:30 +00:00
from rest_framework.parsers import FormParser
2022-10-17 17:56:16 +00:00
from rest_framework.views import APIView
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
log = logs.get_logger(__name__)
def get_accounts(user):
accounts = Account.objects.filter(user=user)
return accounts
2022-10-21 23:15:27 +00:00
class AccountInfo(LoginRequiredMixin, View):
VIEWABLE_FIELDS_MODEL = ["name", "exchange", "api_key", "sandbox"]
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/account-info.html"
def get(self, request, type, account_id):
"""
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:
account = Account.objects.get(id=account_id, user=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 render(request, template_name, context)
live_info = dict(account.get_account())
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
}
if type == "page":
type = "modal"
context = {
"db_info": account_info,
"live_info": live_info,
"account_id": account_id,
"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-10-17 17:56:16 +00:00
class Accounts(LoginRequiredMixin, View):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/accounts.html"
async def get(self, request, type):
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
accounts = get_accounts(request.user)
if type == "page":
type = "modal"
context = {
"title": f"Accounts ({type})",
"unique": unique,
"window_content": self.window_content,
"items": accounts,
"type": type,
}
return render(request, template_name, context)
class AccountAction(LoginRequiredMixin, APIView):
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/add-account.html"
parser_classes = [FormParser]
def get(self, request, type, account_id=None):
"""
Get the form for adding or editing a account.
:param account_id: The id of the account to edit. Optional.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if account_id:
try:
account = Account.objects.get(id=account_id, user=request.user)
form = AccountForm(instance=account)
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)
else:
form = AccountForm()
if type == "page":
type = "modal"
context = {
"form": form,
"account_id": account_id,
"type": type,
"unique": unique,
"window_content": self.window_content,
}
return render(request, template_name, context)
def put(self, request, type, account_id=None):
"""
Add or edit a account.
:param account_id: The id of the account to edit. Optional.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
message = None
message_class = "success"
if account_id:
try:
2022-10-17 18:06:09 +00:00
form = AccountForm(
2022-10-18 06:22:22 +00:00
request.data, instance=Account.objects.get(id=account_id)
2022-10-17 18:06:09 +00:00
)
2022-10-17 06:20:30 +00:00
except Account.DoesNotExist:
2022-10-17 17:56:16 +00:00
message = "Account does not exist"
message_class = "danger"
context = {
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
else:
form = AccountForm(request.data)
if form.is_valid():
account = form.save(commit=False)
account.user = request.user
account.save()
if account_id:
message = f"Account {account_id} edited successfully"
else:
message = f"Account {account.id} added successfully"
else:
message = "Error adding account"
message_class = "danger"
accounts = get_accounts(request.user)
context = {
"items": accounts,
"type": type,
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/account-list.html"
return render(request, template_name, context)
def delete(self, request, type, account_id):
"""
Delete a account.
:param account_id: The id of the account to delete.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
message = None
message_class = "success"
try:
account = Account.objects.get(id=account_id, user=request.user)
account.delete()
message = "Account deleted successfully"
except Account.DoesNotExist:
message = "Error deleting account"
message_class = "danger"
accounts = get_accounts(request.user)
context = {
"items": accounts,
"type": type,
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/account-list.html"
return render(request, template_name, context)