From 72055181bcfef988b7d6b557e8dcb17ba42ef0c8 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 10 Feb 2023 07:20:19 +0000 Subject: [PATCH] Implement CRUD for asset groups --- app/urls.py | 23 +++++ core/forms.py | 16 ++++ .../0052_assetgroup_assetrestriction.py | 37 ++++++++ core/models.py | 35 ++++++++ core/templates/base.html | 3 + core/templates/partials/assetgroup-list.html | 86 +++++++++++++++++++ core/views/assets.py | 37 ++++++++ 7 files changed, 237 insertions(+) create mode 100644 core/migrations/0052_assetgroup_assetrestriction.py create mode 100644 core/templates/partials/assetgroup-list.html create mode 100644 core/views/assets.py diff --git a/app/urls.py b/app/urls.py index 4010844..5357efa 100644 --- a/app/urls.py +++ b/app/urls.py @@ -23,6 +23,7 @@ from two_factor.urls import urlpatterns as tf_urls from core.views import ( accounts, + assets, base, callbacks, hooks, @@ -215,6 +216,7 @@ urlpatterns = [ notifications.NotificationsUpdate.as_view(), name="notifications_update", ), + # Risks path( "risk//", risk.RiskList.as_view(), @@ -235,4 +237,25 @@ urlpatterns = [ risk.RiskDelete.as_view(), name="risk_delete", ), + # Asset Groups + path( + "assetgroup//", + assets.AssetGroupList.as_view(), + name="assetgroups", + ), + path( + "assetgroup//create/", + assets.AssetGroupCreate.as_view(), + name="assetgroup_create", + ), + path( + "assetgroup//update//", + assets.AssetGroupUpdate.as_view(), + name="assetgroup_update", + ), + path( + "assetgroup//delete//", + assets.AssetGroupDelete.as_view(), + name="assetgroup_delete", + ), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/forms.py b/core/forms.py index aeaa7f2..6a7da8e 100644 --- a/core/forms.py +++ b/core/forms.py @@ -5,6 +5,7 @@ from django.forms import ModelForm from .models import ( Account, + AssetGroup, Hook, NotificationSettings, RiskModel, @@ -326,3 +327,18 @@ class RiskModelForm(RestrictedFormMixin, ModelForm): "max_open_trades": "The maximum number of open trades.", "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.", + } diff --git a/core/migrations/0052_assetgroup_assetrestriction.py b/core/migrations/0052_assetgroup_assetrestriction.py new file mode 100644 index 0000000..38e8337 --- /dev/null +++ b/core/migrations/0052_assetgroup_assetrestriction.py @@ -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)), + ], + ), + ] diff --git a/core/models.py b/core/models.py index e13f16d..8545fa4 100644 --- a/core/models.py +++ b/core/models.py @@ -399,3 +399,38 @@ class RiskModel(models.Model): def __str__(self): 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 + ) diff --git a/core/templates/base.html b/core/templates/base.html index 8762ca3..b958646 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -257,6 +257,9 @@ Risk Management + + Asset Groups +