Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only include APPROVED users for stats on homepage #1952

Merged
merged 10 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions dandiapi/api/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import pytest

from dandiapi.api.models import UserMetadata


@pytest.mark.django_db
def test_stats_baseline(api_client):
assert api_client.get('/api/stats/').data == {
'dandiset_count': 0,
'published_dandiset_count': 0,
# django-guardian automatically creates an AnonymousUser
'user_count': 1,
'user_count': 0,
'size': 0,
}

Expand All @@ -32,11 +33,17 @@ def test_stats_published(api_client, published_version_factory):


@pytest.mark.django_db
def test_stats_user(api_client, user):
def test_stats_user(api_client, user_factory):
# Create multiple users with different statuses
users_per_status = approved_user_count = 3

for status in UserMetadata.Status.choices:
[user_factory(metadata__status=status[0]) for _ in range(users_per_status)]

stats = api_client.get('/api/stats/').data

# django-guardian automatically creates an AnonymousUser
assert stats['user_count'] == 2
# Assert that the user count only includes users with APPROVED status
assert stats['user_count'] == approved_user_count


@pytest.mark.django_db
Expand Down
4 changes: 2 additions & 2 deletions dandiapi/api/views/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response

from dandiapi.api.models import Asset, Dandiset
from dandiapi.api.models import Asset, Dandiset, UserMetadata


# Cache this response for 12 hours
Expand All @@ -14,7 +14,7 @@
def stats_view(self):
dandiset_count = Dandiset.objects.count()
published_dandiset_count = Dandiset.published_count()
user_count = User.objects.count()
user_count = User.objects.filter(metadata__status=UserMetadata.Status.APPROVED).count()
size = Asset.total_size()
return Response(
{
Expand Down