diff --git a/app/urls.py b/app/urls.py index 87be357..d226624 100644 --- a/app/urls.py +++ b/app/urls.py @@ -284,4 +284,20 @@ urlpatterns = [ assets.AssetRestrictionDelete.as_view(), name="assetrestriction_delete", ), + # Asset group filters + path( + "assetfilter//flip//", + assets.AssetFilterFlip.as_view(), + name="assetfilter_flip", + ), + path( + "assetfilter//delete//", + assets.AssetFilterDelete.as_view(), + name="assetfilter_delete", + ), + path( + "assetfilter//view//", + assets.AssetFilterList.as_view(), + name="assetfilters", + ), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/admin.py b/core/admin.py index 34524ee..504e742 100644 --- a/core/admin.py +++ b/core/admin.py @@ -94,7 +94,7 @@ class RiskModelAdmin(admin.ModelAdmin): class AssetGroupAdmin(admin.ModelAdmin): - list_display = ("user", "name", "description", "account") + list_display = ("user", "name", "description") class AssetRestrictionAdmin(admin.ModelAdmin): diff --git a/core/forms.py b/core/forms.py index ab2190f..a817d1c 100644 --- a/core/forms.py +++ b/core/forms.py @@ -309,12 +309,10 @@ class AssetGroupForm(RestrictedFormMixin, ModelForm): 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/0058_remove_assetgroup_account.py b/core/migrations/0058_remove_assetgroup_account.py new file mode 100644 index 0000000..960e43a --- /dev/null +++ b/core/migrations/0058_remove_assetgroup_account.py @@ -0,0 +1,17 @@ +# Generated by Django 4.1.6 on 2023-02-11 18:46 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0057_alter_assetgroup_account_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='assetgroup', + name='account', + ), + ] diff --git a/core/models.py b/core/models.py index d65d33e..694ec26 100644 --- a/core/models.py +++ b/core/models.py @@ -411,11 +411,6 @@ class AssetGroup(models.Model): 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.PROTECT, null=True, blank=True - ) - # Dict like {"RUB": True, "USD": False} allowed = models.JSONField(null=True, blank=True, default=dict) diff --git a/core/templates/partials/asset-filter-list.html b/core/templates/partials/asset-filter-list.html new file mode 100644 index 0000000..21d5436 --- /dev/null +++ b/core/templates/partials/asset-filter-list.html @@ -0,0 +1,59 @@ +{% load cache %} +{% load cachalot cache %} +{% get_last_invalidation 'core.AssetGroup' as last %} +{% include 'mixins/partials/notify.html' %} +{% cache 600 objects_assetgroups_field request.user.id object_list last %} + + + + + + + {% for key, item in object_list.items %} + + + + + + {% endfor %} + +
symbolstatusactions
{{ key }}{{ item }} +
+ + +
+
+{% endcache %} \ No newline at end of file diff --git a/core/templates/partials/assetgroup-list.html b/core/templates/partials/assetgroup-list.html index f037eae..b531cfa 100644 --- a/core/templates/partials/assetgroup-list.html +++ b/core/templates/partials/assetgroup-list.html @@ -15,7 +15,6 @@ user name description - account status restrictions actions @@ -26,8 +25,16 @@ {{ item.user }} {{ item.name }} {{ item.description }} - {{ item.account }} - {{ item.matches }} + + + {{ item.matches }} + + {{ item.restrictions }}
diff --git a/core/views/assets.py b/core/views/assets.py index 4674fbd..e61be37 100644 --- a/core/views/assets.py +++ b/core/views/assets.py @@ -3,7 +3,16 @@ import json from cachalot.api import invalidate from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse -from mixins.views import AbortSave, ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate +from django.shortcuts import render +from django.views import View +from mixins.views import ( + AbortSave, + ObjectCreate, + ObjectDelete, + ObjectList, + ObjectNameMixin, + ObjectUpdate, +) from rest_framework import status from rest_framework.parsers import JSONParser from rest_framework.views import APIView @@ -154,3 +163,68 @@ class AssetRestrictionAPI(APIView): return HttpResponse(status=status.HTTP_200_OK) return HttpResponse(status=status.HTTP_400_BAD_REQUEST) + + +# Asset group allowed field +class AssetFilterList(LoginRequiredMixin, ObjectList): + list_template = "partials/asset-filter-list.html" + page_title = "List of asset filters." + page_subtitle = None + + context_object_name_singular = "asset filter" + context_object_name = "asset filters" + + list_url_name = "assetfilters" + list_url_args = ["type", "group_id"] + + def get_queryset(self, **kwargs): + group_id = kwargs.get("group_id", None) + self.extra_context = {"group_id": group_id} + try: + group = AssetGroup.objects.get(id=group_id, user=self.request.user) + except AssetGroup.DoesNotExist: + context = {"message": "Asset group does not exist", "class": "danger"} + return self.render_to_response(context) + return group.allowed + + +class AssetFilterFlip(LoginRequiredMixin, ObjectNameMixin, View): + template_name = "mixins/partials/notify.html" + model = AssetGroup + + def get(self, request, group_id, symbol): + try: + group = AssetGroup.objects.get(id=group_id, user=request.user) + except AssetGroup.DoesNotExist: + context = {"message": "Asset group does not exist", "class": "danger"} + return render(request, self.template_name, context) + if symbol not in group.allowed: + context = {"message": "Asset filter does not exist", "class": "danger"} + return render(request, self.template_name, context) + group.allowed[symbol] = not group.allowed[symbol] + group.save() + context = {"message": "Asset filter updated", "class": "success"} + response = render(request, self.template_name, context) + response["HX-Trigger"] = "assetgroupEvent" + return response + + +class AssetFilterDelete(LoginRequiredMixin, ObjectNameMixin, View): + template_name = "mixins/partials/notify.html" + model = AssetGroup + + def delete(self, request, group_id, symbol): + try: + group = AssetGroup.objects.get(id=group_id, user=request.user) + except AssetGroup.DoesNotExist: + context = {"message": "Asset group does not exist", "class": "danger"} + return render(request, self.template_name, context) + if symbol not in group.allowed: + context = {"message": "Asset filter does not exist", "class": "danger"} + return render(request, self.template_name, context) + del group.allowed[symbol] + group.save() + context = {"message": "Asset filter deleted", "class": "success"} + response = render(request, self.template_name, context) + response["HX-Trigger"] = "assetgroupEvent" + return response