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 4 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
5 changes: 1 addition & 4 deletions dandiapi/api/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ def is_different_from(
if self.path != path:
return True

if self.metadata != metadata:
return True

return False
return self.metadata != metadata
waxlamp marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def dandi_asset_id(asset_id: str | uuid.UUID):
Expand Down
24 changes: 20 additions & 4 deletions dandiapi/api/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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,
# django-guardian automatically creates an AnonymousUser, but user is not approved
waxlamp marked this conversation as resolved.
Show resolved Hide resolved
'user_count': 0,
'size': 0,
}

Expand All @@ -33,10 +36,23 @@ def test_stats_published(api_client, published_version_factory):

@pytest.mark.django_db()
def test_stats_user(api_client, user):
# Reset the User table for test
User.objects.all().delete()
waxlamp marked this conversation as resolved.
Show resolved Hide resolved

# Create users with different statuses
approved_user_count = 0
for status in UserMetadata.Status.choices:
status_value = status[0]
user = User.objects.create(username=f'{status_value.lower()}_user')
UserMetadata.objects.create(user=user, status=status_value)
if status_value == UserMetadata.Status.APPROVED:
approved_user_count += 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'] == 1
waxlamp marked this conversation as resolved.
Show resolved Hide resolved
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