Skip to content

Commit

Permalink
Adding a proposed document stats endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Nov 4, 2023
1 parent c67845b commit 930060a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api_v2/models/document.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.db import models
from django.urls import reverse
from django.apps import apps


from .abstracts import HasName, HasDescription

from api_v2 import models as v2_models

class Document(HasName, HasDescription):

Expand Down Expand Up @@ -38,6 +41,33 @@ class Document(HasName, HasDescription):
help_text="Link to the document."
)

@property
def stats(self):
stats = {}
for model in apps.get_models():
# Filter out api_v1.
if model._meta.app_label != 'api_v2': continue

SKIPPED_MODEL_NAMES = [
'Document',
'Ruleset',
'License',
'Publisher']
if model.__name__ in SKIPPED_MODEL_NAMES: continue

CHILD_MODEL_NAMES = [
'Trait',
'FeatBenefit',
'BackgroundBenefit',
'CreatureAction',
'CreatureAttack']
if model.__name__ in CHILD_MODEL_NAMES: continue


object_count = model.objects.filter(document=self.key).count()
stats[model.__name__.lower()]=object_count
return stats


class License(HasName, HasDescription):
key = models.CharField(
Expand Down
1 change: 1 addition & 0 deletions api_v2/serializers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Meta:

class DocumentSerializer(serializers.HyperlinkedModelSerializer):
key = serializers.ReadOnlyField()
stats = serializers.ReadOnlyField()

class Meta:
model = models.Document
Expand Down

0 comments on commit 930060a

Please sign in to comment.