Implement CRUD for asset groups

This commit is contained in:
Mark Veidemanis 2023-02-10 07:20:19 +00:00
parent e00cdc906e
commit 72055181bc
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
7 changed files with 237 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from two_factor.urls import urlpatterns as tf_urls
from core.views import ( from core.views import (
accounts, accounts,
assets,
base, base,
callbacks, callbacks,
hooks, hooks,
@ -215,6 +216,7 @@ urlpatterns = [
notifications.NotificationsUpdate.as_view(), notifications.NotificationsUpdate.as_view(),
name="notifications_update", name="notifications_update",
), ),
# Risks
path( path(
"risk/<str:type>/", "risk/<str:type>/",
risk.RiskList.as_view(), risk.RiskList.as_view(),
@ -235,4 +237,25 @@ urlpatterns = [
risk.RiskDelete.as_view(), risk.RiskDelete.as_view(),
name="risk_delete", name="risk_delete",
), ),
# Asset Groups
path(
"assetgroup/<str:type>/",
assets.AssetGroupList.as_view(),
name="assetgroups",
),
path(
"assetgroup/<str:type>/create/",
assets.AssetGroupCreate.as_view(),
name="assetgroup_create",
),
path(
"assetgroup/<str:type>/update/<str:pk>/",
assets.AssetGroupUpdate.as_view(),
name="assetgroup_update",
),
path(
"assetgroup/<str:type>/delete/<str:pk>/",
assets.AssetGroupDelete.as_view(),
name="assetgroup_delete",
),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -5,6 +5,7 @@ from django.forms import ModelForm
from .models import ( from .models import (
Account, Account,
AssetGroup,
Hook, Hook,
NotificationSettings, NotificationSettings,
RiskModel, RiskModel,
@ -326,3 +327,18 @@ class RiskModelForm(RestrictedFormMixin, ModelForm):
"max_open_trades": "The maximum number of open trades.", "max_open_trades": "The maximum number of open trades.",
"max_open_trades_per_symbol": "The maximum number of open trades per symbol.", "max_open_trades_per_symbol": "The maximum number of open trades per symbol.",
} }
class AssetGroupForm(RestrictedFormMixin, ModelForm):
class Meta:
model = AssetGroup
fields = (
"name",
"description",
"account",
)
help_texts = {
"name": "Name of the asset group. Informational only.",
"description": "Description of the asset group. Informational only.",
"account": "Account to pull assets from.",
}

View File

@ -0,0 +1,37 @@
# Generated by Django 4.1.4 on 2023-02-10 13:29
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0051_account_initial_balance'),
]
operations = [
migrations.CreateModel(
name='AssetGroup',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('description', models.TextField(blank=True, null=True)),
('allowed', models.JSONField(blank=True, null=True)),
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.account')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='AssetRestriction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('description', models.TextField(blank=True, null=True)),
('pairs', models.CharField(blank=True, max_length=4096, null=True)),
('group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='core.assetgroup')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -399,3 +399,38 @@ class RiskModel(models.Model):
def __str__(self): def __str__(self):
return self.name return self.name
class AssetGroup(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)
# Account for checking pairs on children if specified
account = models.ForeignKey(Account, on_delete=models.CASCADE)
# Dict like {"RUB": True, "USD": False}
allowed = models.JSONField(null=True, blank=True, default={})
def __str__(self):
return self.name
@property
def matches(self):
"""
Get the total number of matches for this group.
"""
if isinstance(self.allowed, dict):
truthy_values = [x for x in self.allowed.values() if x is True]
return f"{len(truthy_values)}/{len(self.allowed)}"
class AssetRestriction(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)
pairs = models.CharField(max_length=4096, null=True, blank=True)
group = models.ForeignKey(
AssetGroup, on_delete=models.CASCADE, null=True, blank=True
)

View File

@ -257,6 +257,9 @@
<a class="navbar-item" href="{% url 'risks' type='page' %}"> <a class="navbar-item" href="{% url 'risks' type='page' %}">
Risk Management Risk Management
</a> </a>
<a class="navbar-item" href="{% url 'assetgroups' type='page' %}">
Asset Groups
</a>
</div> </div>
</div> </div>
<div class="navbar-item has-dropdown is-hoverable"> <div class="navbar-item has-dropdown is-hoverable">

View File

@ -0,0 +1,86 @@
{% include 'partials/notify.html' %}
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>user</th>
<th>name</th>
<th>description</th>
<th>account</th>
<th>status</th>
<th>actions</th>
</thead>
{% for item in object_list %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.user }}</td>
<td>{{ item.name }}</td>
<td>{{ item.description }}</td>
<td>{{ item.account }}</td>
<td>{{ item.matches }}</td>
<td>
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'assetgroup_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-pencil"></i>
</span>
</span>
</button>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{% url 'assetgroup_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-xmark"></i>
</span>
</span>
</button>
{% if type == 'page' %}
<a href="{# url 'account_info' type=type pk=item.id #}"><button
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-eye"></i>
</span>
</span>
</button>
</a>
{% else %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{# url 'account_info' type=type pk=item.id #}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-eye"></i>
</span>
</span>
</button>
{% endif %}
</div>
</td>
</tr>
{% endfor %}
</table>

37
core/views/assets.py Normal file
View File

@ -0,0 +1,37 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from core.forms import AssetGroupForm
from core.models import AssetGroup
from core.util import logs
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
log = logs.get_logger(__name__)
class AssetGroupList(LoginRequiredMixin, ObjectList):
list_template = "partials/assetgroup-list.html"
model = AssetGroup
page_title = "List of asset groups for restrictions. Linked to accounts."
list_url_name = "assetgroups"
list_url_args = ["type"]
submit_url_name = "assetgroup_create"
class AssetGroupCreate(LoginRequiredMixin, ObjectCreate):
model = AssetGroup
form_class = AssetGroupForm
submit_url_name = "assetgroup_create"
class AssetGroupUpdate(LoginRequiredMixin, ObjectUpdate):
model = AssetGroup
form_class = AssetGroupForm
submit_url_name = "assetgroup_update"
class AssetGroupDelete(LoginRequiredMixin, ObjectDelete):
model = AssetGroup