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) allowed = models.JSONField(null=True, blank=True, default=dict)
def __str__(self): 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): class AssetRestriction(models.Model):

View File

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

View File

@ -1,5 +1,6 @@
import json import json
from cachalot.api import invalidate
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse from django.http import HttpResponse
from mixins.views import AbortSave, ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate from mixins.views import AbortSave, ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
@ -148,6 +149,8 @@ class AssetRestrictionAPI(APIView):
for pair in restriction.pairs_parsed: for pair in restriction.pairs_parsed:
group.allowed[pair] = is_match group.allowed[pair] = is_match
group.save() group.save()
invalidate(restriction)
invalidate(group)
return HttpResponse(status=status.HTTP_200_OK) return HttpResponse(status=status.HTTP_200_OK)
return HttpResponse(status=status.HTTP_400_BAD_REQUEST) return HttpResponse(status=status.HTTP_400_BAD_REQUEST)