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 8 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
38 changes: 33 additions & 5 deletions dandiapi/api/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from __future__ import annotations

from django.contrib.auth.models import User
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 +34,37 @@ 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):
User.objects.all().delete()
waxlamp marked this conversation as resolved.
Show resolved Hide resolved

# Create multiple users with different statuses
approved_user_count = 0
users_per_status = 3
user_index = 0

for status in UserMetadata.Status.choices:
status_value = status[0]
for _ in range(users_per_status):
username = f'{status_value.lower()}_user_{user_index}'
user = user_factory(username=username)
user_metadata, created = UserMetadata.objects.get_or_create(
user=user,
defaults={
'status': status_value,
'questionnaire_form': None,
'rejection_reason': ''
}
)
if not created:
pass
if status_value == UserMetadata.Status.APPROVED:
approved_user_count += 1
user_index += 1
waxlamp marked this conversation as resolved.
Show resolved Hide resolved

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
Loading