Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Fix duplicated Seed Search tables and crash on deleted users

Claudea3d477c7b91a
diff --git a/mwmbl/admin_views.py b/mwmbl/admin_views.py
index 4036417..a000a5f 100644
--- a/mwmbl/admin_views.py
+++ b/mwmbl/admin_views.py
@@ -284,8 +284,10 @@ def _seed_search_stats() -> dict:
"free" is everyone without a membership, matching COMBINED_SEARCH_MONTHLY_LIMITS.
"""
counts = quota.get_all_monthly_combined_search_counts()
- tier_by_user = dict(Membership.objects.filter(user_id__in=counts).values_list("user_id", "tier"))
users_by_id = MwmblUser.objects.in_bulk(list(counts))
+ # Counters outlive deleted accounts until they expire, so skip users that no longer exist.
+ counts = {user_id: count for user_id, count in counts.items() if user_id in users_by_id}
+ tier_by_user = dict(Membership.objects.filter(user_id__in=counts).values_list("user_id", "tier"))
tier_names = ["free"] + [tier_info.tier.value for tier_info in TIERS]
by_tier = {tier: {"tier": tier, "users": 0, "queries": 0, "at_limit": 0} for tier in tier_names}
diff --git a/mwmbl/templates/admin/paying_users.html b/mwmbl/templates/admin/paying_users.html
index 2f0e47b..ad4dcc1 100644
--- a/mwmbl/templates/admin/paying_users.html
+++ b/mwmbl/templates/admin/paying_users.html
@@ -7,64 +7,6 @@
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">Home</a>
&rsaquo; Paying users
- <div class="module">
- <h2>Seed Search this month</h2>
- <p class="note">
- Seed Search (Combined Search) is only available to signed-in users. Counts are this
- calendar month's live counters; "free" is everyone without a membership.
- {{ seed_search.users }} users have made {{ seed_search.queries }} queries, and
- {{ seed_search.at_limit }} have reached their quota.
- </p>
- <table>
- <thead>
- <tr>
- <th>Tier</th>
- <th class="num">Monthly quota</th>
- <th class="num">Users</th>
- <th class="num">Queries</th>
- <th class="num">At quota</th>
- </tr>
- </thead>
- <tbody>
- {% for tier in seed_search.by_tier %}
- <tr>
- <td>{{ tier.tier|capfirst }}</td>
- <td class="num">{{ tier.limit }}</td>
- <td class="num">{{ tier.users }}</td>
- <td class="num">{{ tier.queries }}</td>
- <td class="num">{{ tier.at_limit }}</td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
- </div>
-
- <div class="module">
- <h2>Top Seed Search users this month</h2>
- <table>
- <thead>
- <tr>
- <th>User</th>
- <th>Tier</th>
- <th class="num">Usage</th>
- <th class="num">Quota</th>
- </tr>
- </thead>
- <tbody>
- {% for row in seed_search.top_users %}
- <tr>
- <td><a href="{% url 'admin:mwmbl_mwmbluser_change' row.user.pk %}">{{ row.user.username }}</a></td>
- <td>{{ row.tier|capfirst }}</td>
- <td class="num{% if row.usage >= row.limit %} over{% endif %}">{{ row.usage }}</td>
- <td class="num">{{ row.limit }}</td>
- </tr>
- {% empty %}
- <tr><td colspan="4">No Seed Search queries this month.</td></tr>
- {% endfor %}
- </tbody>
- </table>
- </div>
-
</div>
{% endblock %}
diff --git a/test/test_admin_paying_users.py b/test/test_admin_paying_users.py
index c34a474..d38df13 100644
--- a/test/test_admin_paying_users.py
+++ b/test/test_admin_paying_users.py
@@ -143,6 +143,25 @@ def test_seed_search_usage_by_tier(staff_client):
assert [row["user"].username for row in seed_search["top_users"]] == ["free_user", "sprout_member"]
+def test_seed_search_skips_deleted_users(staff_client):
+ deleted = User.objects.create_user(username="deleted_user")
+ quota.increment_monthly_combined_search(deleted.id)
+ deleted.delete()
+
+ response = staff_client.get(PAYING_USERS_URL)
+
+ assert response.status_code == 200
+ seed_search = response.context["seed_search"]
+ assert seed_search["users"] == 0
+ assert seed_search["top_users"] == []
+
+
+def test_breadcrumbs_do_not_contain_seed_search_tables(staff_client):
+ response = staff_client.get(PAYING_USERS_URL)
+
+ assert response.content.count(b"Seed Search this month") == 1
+
+
def test_minor_units():
assert minor_units(0, "$") == "$0.00"
assert minor_units(123_456, "£") == "£1,234.56"