Re-add property fields

This commit is contained in:
Mark Veidemanis 2023-02-11 18:07:05 +00:00
parent 7afdd39af7
commit bdf8f04210
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 25 additions and 2 deletions

View File

@ -418,7 +418,23 @@ class AssetGroup(models.Model):
allowed = models.JSONField(null=True, blank=True, default=dict)
def __str__(self):
return f"{self.name}"
return f"{self.name} ({self.restrictions})"
@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)}"
@property
def restrictions(self):
"""
Get the total number of restrictions for this group.
"""
return self.assetrestriction_set.count()
class AssetRestriction(models.Model):

View File

@ -1,6 +1,6 @@
{% load cache %}
{% load cachalot cache %}
{% get_last_invalidation 'core.AssetGroup' as last %}
{% get_last_invalidation 'core.AssetGroup' 'core.AssetRestriction' as last %}
{% include 'mixins/partials/notify.html' %}
{% cache 600 objects_assetgroups request.user.id object_list last %}
<table
@ -16,6 +16,8 @@
<th>name</th>
<th>description</th>
<th>account</th>
<th>status</th>
<th>restrictions</th>
<th>actions</th>
</thead>
{% for item in object_list %}
@ -25,6 +27,8 @@
<td>{{ item.name }}</td>
<td>{{ item.description }}</td>
<td>{{ item.account }}</td>
<td>{{ item.matches }}</td>
<td>{{ item.restrictions }}</td>
<td>
<div class="buttons">
<button

View File

@ -1,5 +1,6 @@
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
@ -148,6 +149,8 @@ class AssetRestrictionAPI(APIView):
for pair in restriction.pairs_parsed:
group.allowed[pair] = is_match
group.save()
invalidate(restriction)
invalidate(group)
return HttpResponse(status=status.HTTP_200_OK)
return HttpResponse(status=status.HTTP_400_BAD_REQUEST)