An open source, non-profit web search engine
Store the Seed Search preference on the user
Claude631943e2ad9e
diff --git a/mwmbl/migrations/0042_mwmbluser_seed_search_enabled.py b/mwmbl/migrations/0042_mwmbluser_seed_search_enabled.pynew file mode 100644index 0000000..97f15d3--- /dev/null+++ b/mwmbl/migrations/0042_mwmbluser_seed_search_enabled.py@@ -0,0 +1,22 @@+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("mwmbl", "0041_usagebucket_combined_search"),+ ]++ # Existing users get False, so nobody already registered has Seed Search switched on for them;+ # the second step makes it on for accounts created from now on.+ operations = [+ migrations.AddField(+ model_name="mwmbluser",+ name="seed_search_enabled",+ field=models.BooleanField(default=False),+ ),+ migrations.AlterField(+ model_name="mwmbluser",+ name="seed_search_enabled",+ field=models.BooleanField(default=True),+ ),+ ]diff --git a/mwmbl/models.py b/mwmbl/models.pyindex 233c986..a09fdf3 100644--- a/mwmbl/models.py+++ b/mwmbl/models.py@@ -11,7 +11,8 @@ from mwmbl.utils import bare_hostclass MwmblUser(AbstractUser):- pass+ # On for new accounts. Accounts created before this field existed start off (see the migration).+ seed_search_enabled = models.BooleanField(default=True)class UserCuration(models.Model):diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.pyindex 67a4d2c..29dd7ff 100644--- a/mwmbl/platform/api.py+++ b/mwmbl/platform/api.py@@ -76,6 +76,7 @@ from mwmbl.platform.schemas import (QueueItemSchema,Registration,ResetPasswordRequest,+ SeedSearchPreference,SubmissionDetailSchema,SubscriptionResponse,UpdateDeviceRequest,@@ -1365,6 +1366,33 @@ def get_combined_search_usage(request):)+ "/combined-search/preference",+ auth=JWTAuth(),+ response=SeedSearchPreference,+ summary="Get Seed Search preference",+ description="Returns whether the user has Seed Search (Combined Search) switched on in the mwmbl.org "+ "interface. It does not affect calls to the Combined Search API.",+ tags=["Membership"],+)+def get_seed_search_preference(request):+ return SeedSearchPreference(enabled=request.user.seed_search_enabled)+++ "/combined-search/preference",+ auth=JWTAuth(),+ response=SeedSearchPreference,+ summary="Set Seed Search preference",+ description="Switches Seed Search (Combined Search) on or off for the user in the mwmbl.org interface.",+ tags=["Membership"],+)+def set_seed_search_preference(request, body: SeedSearchPreference):+ request.user.seed_search_enabled = body.enabled+ request.user.save(update_fields=["seed_search_enabled"])+ return SeedSearchPreference(enabled=request.user.seed_search_enabled)++@router.post("/membership/checkout",auth=JWTAuth(),diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.pyindex 30e9e42..f0f7b28 100644--- a/mwmbl/platform/schemas.py+++ b/mwmbl/platform/schemas.py@@ -63,6 +63,10 @@ class CombinedSearchUsageResponse(Schema):monthly_limit: int+class SeedSearchPreference(Schema):+ enabled: bool++class MembershipCheckoutRequest(Schema):tier: MembershipTiersuccess_url: Optional[str] = Nonediff --git a/test/test_seed_search_preference.py b/test/test_seed_search_preference.pynew file mode 100644index 0000000..cf7e1b2--- /dev/null+++ b/test/test_seed_search_preference.py@@ -0,0 +1,71 @@+import json++import pytest+from django.contrib.auth import get_user_model+from django.test import Client+from ninja_jwt.tokens import RefreshToken++User = get_user_model()++PREFERENCE_URL = "/api/v1/platform/combined-search/preference"+REGISTER_URL = "/api/v1/platform/register"+++def user(db):+ return User.objects.create_user(username="seeder", email="[email protected]", password="x")+++def auth(user):+ return {"HTTP_AUTHORIZATION": f"Bearer {RefreshToken.for_user(user).access_token}"}+++def client(db):+ return Client()+++def _set(client, auth, enabled):+ return client.put(PREFERENCE_URL, data=json.dumps({"enabled": enabled}), content_type="application/json", **auth)+++def test_a_newly_registered_user_has_seed_search_on(client):+ body = {"email": "[email protected]", "password": "correctpassword", "username": "newbie"}+ client.post(REGISTER_URL, data=json.dumps(body), content_type="application/json")++ assert User.objects.get(username="newbie").seed_search_enabled+++def test_the_preference_endpoint_reports_the_users_setting(client, user, auth):+ response = client.get(PREFERENCE_URL, **auth)++ assert response.status_code == 200+ assert response.json() == {"enabled": True}+++def test_switching_seed_search_off_is_saved(client, user, auth):+ response = _set(client, auth, False)++ user.refresh_from_db()+ assert response.json() == {"enabled": False}+ assert not user.seed_search_enabled+ assert client.get(PREFERENCE_URL, **auth).json() == {"enabled": False}+++def test_switching_seed_search_back_on_is_saved(client, user, auth):+ _set(client, auth, False)+ _set(client, auth, True)++ user.refresh_from_db()+ assert user.seed_search_enabled+++def test_the_preference_endpoints_require_a_jwt(client):+ assert client.get(PREFERENCE_URL).status_code == 401+ assert client.put(PREFERENCE_URL, data="{}", content_type="application/json").status_code == 401