Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Add voting API for search results

Daoud Clarke848a4e592374
diff --git a/mwmbl/migrations/0013_searchresultvote.py b/mwmbl/migrations/0013_searchresultvote.py
new file mode 100644
index 0000000..36eac65
--- /dev/null
+++ b/mwmbl/migrations/0013_searchresultvote.py
@@ -0,0 +1,33 @@
+# Generated by Django 4.2.23 on 2025-07-20 14:00
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('mwmbl', '0012_wasmevaluationjob'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='SearchResultVote',
+ fields=[
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('url', models.URLField(max_length=500)),
+ ('query', models.CharField(max_length=300)),
+ ('vote_type', models.CharField(choices=[('UPVOTE', 'User upvoted this result'), ('DOWNVOTE', 'User downvoted this result')], max_length=10)),
+ ('timestamp', models.DateTimeField(auto_now_add=True)),
+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+ ],
+ options={
+ 'indexes': [models.Index(fields=['url', 'query'], name='mwmbl_search_url_b8e7b8_idx'), models.Index(fields=['timestamp'], name='mwmbl_search_timesta_b8e7b8_idx')],
+ },
+ ),
+ migrations.AddConstraint(
+ model_name='searchresultvote',
+ constraint=models.UniqueConstraint(fields=('user', 'url', 'query'), name='mwmbl_searchresultvote_user_url_query_unique'),
+ ),
+ ]
diff --git a/mwmbl/migrations/0014_remove_searchresultvote_mwmbl_searchresultvote_user_url_query_unique_and_more.py b/mwmbl/migrations/0014_remove_searchresultvote_mwmbl_searchresultvote_user_url_query_unique_and_more.py
new file mode 100644
index 0000000..300f04b
--- /dev/null
+++ b/mwmbl/migrations/0014_remove_searchresultvote_mwmbl_searchresultvote_user_url_query_unique_and_more.py
@@ -0,0 +1,31 @@
+# Generated by Django 4.2.23 on 2025-07-20 14:28
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('mwmbl', '0013_searchresultvote'),
+ ]
+
+ operations = [
+ migrations.RemoveConstraint(
+ model_name='searchresultvote',
+ name='mwmbl_searchresultvote_user_url_query_unique',
+ ),
+ migrations.RenameIndex(
+ model_name='searchresultvote',
+ new_name='mwmbl_searc_url_03d737_idx',
+ old_name='mwmbl_search_url_b8e7b8_idx',
+ ),
+ migrations.RenameIndex(
+ model_name='searchresultvote',
+ new_name='mwmbl_searc_timesta_b8940f_idx',
+ old_name='mwmbl_search_timesta_b8e7b8_idx',
+ ),
+ migrations.AlterUniqueTogether(
+ name='searchresultvote',
+ unique_together={('user', 'url', 'query')},
+ ),
+ ]
diff --git a/mwmbl/models.py b/mwmbl/models.py
index 4fb0eaa..1972afd 100644
--- a/mwmbl/models.py
+++ b/mwmbl/models.py
@@ -129,3 +129,23 @@ class WasmEvaluationJob(models.Model):
completed_at = models.DateTimeField(null=True, blank=True)
results = models.JSONField(null=True, blank=True)
error_message = models.TextField(null=True, blank=True)
+
+
+class SearchResultVote(models.Model):
+ VOTE_TYPES = {
+ "UPVOTE": "User upvoted this result",
+ "DOWNVOTE": "User downvoted this result",
+ }
+
+ user = models.ForeignKey(MwmblUser, on_delete=models.CASCADE)
+ url = models.URLField(max_length=500) # The URL of the search result
+ query = models.CharField(max_length=300) # The search query context
+ vote_type = models.CharField(max_length=10, choices=[(k, v) for k, v in VOTE_TYPES.items()])
+ timestamp = models.DateTimeField(auto_now_add=True)
+
+ class Meta:
+ unique_together = ['user', 'url', 'query'] # One vote per user per result per query
+ indexes = [
+ models.Index(fields=['url', 'query']),
+ models.Index(fields=['timestamp']),
+ ]
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.py
index 97d3574..505e8b6 100644
--- a/mwmbl/platform/api.py
+++ b/mwmbl/platform/api.py
@@ -1,13 +1,17 @@
from allauth.account.adapter import get_adapter
from allauth.account.models import EmailConfirmationHMAC
from allauth.account.utils import setup_user_email, send_email_confirmation
+from django.db.models import Count, Q
from ninja.pagination import paginate
from ninja_extra import NinjaExtraAPI
from ninja_jwt.authentication import JWTAuth
from ninja_jwt.controller import NinjaJWTDefaultController
-from mwmbl.models import MwmblUser, DomainSubmission
-from mwmbl.platform.schemas import Registration, ConfirmEmail, DomainSubmissionSchema, UpdateDomainSubmission
+from mwmbl.models import MwmblUser, DomainSubmission, SearchResultVote
+from mwmbl.platform.schemas import (
+ Registration, ConfirmEmail, DomainSubmissionSchema, UpdateDomainSubmission,
+ VoteRequest, VoteRemoveRequest, VoteResponse, VoteStats, UserVoteHistory
+)
api = NinjaExtraAPI(urls_namespace="platform")
api.register_controllers(NinjaJWTDefaultController)
@@ -148,3 +152,79 @@ def update_submission_status(request, submission_id: int, update_submission: Upd
submission.rejection_detail = update_submission.rejection_detail
submission.save()
return {"status": "ok", "message": "Submission updated."}
+
+
[email protected]("/search-results/vote", auth=JWTAuth())
+def vote_on_search_result(request, vote_request: VoteRequest):
+ check_email_verified(request)
+
+ # Validate vote type
+ if vote_request.vote_type not in SearchResultVote.VOTE_TYPES:
+ raise InvalidRequest("Invalid vote type. Must be 'UPVOTE' or 'DOWNVOTE'.", status=400)
+
+ # Create or update the vote
+ vote, created = SearchResultVote.objects.update_or_create(
+ user=request.user,
+ url=vote_request.url,
+ query=vote_request.query,
+ defaults={'vote_type': vote_request.vote_type}
+ )
+
+ action = "created" if created else "updated"
+ return {"status": "ok", "message": f"Vote {action} successfully."}
+
+
[email protected]("/search-results/votes", response=VoteResponse, auth=JWTAuth())
+def get_vote_counts(request, query: str, urls: str):
+ check_email_verified(request)
+
+ url_list = [url.strip() for url in urls.split(',') if url.strip()]
+ if not url_list:
+ raise InvalidRequest("At least one URL must be provided.", status=400)
+
+ # Get vote counts for each URL
+ vote_data = {}
+ for url in url_list:
+ # Get aggregated vote counts
+ votes = SearchResultVote.objects.filter(url=url, query=query)
+ upvotes = votes.filter(vote_type='UPVOTE').count()
+ downvotes = votes.filter(vote_type='DOWNVOTE').count()
+
+ # Get user's vote if any
+ user_vote = None
+ try:
+ user_vote_obj = votes.get(user=request.user)
+ user_vote = user_vote_obj.vote_type
+ except SearchResultVote.DoesNotExist:
+ pass
+
+ vote_data[url] = VoteStats(
+ upvotes=upvotes,
+ downvotes=downvotes,
+ user_vote=user_vote
+ )
+
+ return VoteResponse(votes=vote_data)
+
+
[email protected]("/search-results/vote", auth=JWTAuth())
+def remove_vote(request, vote_request: VoteRemoveRequest):
+ check_email_verified(request)
+
+ try:
+ vote = SearchResultVote.objects.get(
+ user=request.user,
+ url=vote_request.url,
+ query=vote_request.query
+ )
+ vote.delete()
+ return {"status": "ok", "message": "Vote removed successfully."}
+ except SearchResultVote.DoesNotExist:
+ raise InvalidRequest("No vote found to remove.", status=404)
+
+
[email protected]("/search-results/my-votes", response=list[UserVoteHistory], auth=JWTAuth())
+@paginate
+def get_user_vote_history(request) -> list[SearchResultVote]:
+ check_email_verified(request)
+ return SearchResultVote.objects.filter(user=request.user).order_by('-timestamp')
diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.py
index 85db92c..16336b9 100644
--- a/mwmbl/platform/schemas.py
+++ b/mwmbl/platform/schemas.py
@@ -1,3 +1,5 @@
+from datetime import datetime
+from typing import Optional
from ninja import Schema, ModelSchema
from mwmbl.models import DomainSubmission
@@ -25,3 +27,31 @@ class UpdateDomainSubmission(ModelSchema):
class Meta:
model = DomainSubmission
fields = ["status", "rejection_reason", "rejection_detail"]
+
+
+class VoteRequest(Schema):
+ url: str
+ query: str
+ vote_type: str
+
+
+class VoteRemoveRequest(Schema):
+ url: str
+ query: str
+
+
+class VoteStats(Schema):
+ upvotes: int
+ downvotes: int
+ user_vote: Optional[str] = None
+
+
+class VoteResponse(Schema):
+ votes: dict[str, VoteStats]
+
+
+class UserVoteHistory(Schema):
+ url: str
+ query: str
+ vote_type: str
+ timestamp: datetime
diff --git a/test/test_voting_api.py b/test/test_voting_api.py
new file mode 100644
index 0000000..5ecde80
--- /dev/null
+++ b/test/test_voting_api.py
@@ -0,0 +1,438 @@
+import pytest
+import json
+from django.contrib.auth import get_user_model
+from allauth.account.models import EmailAddress
+from ninja_jwt.tokens import RefreshToken
+
+from mwmbl.models import SearchResultVote
+
+
+User = get_user_model()
+
+
+def user():
+ """Create a test user"""
+ return User.objects.create_user(
+ username='testuser',
+ password='testpass123'
+ )
+
+
+def user2():
+ """Create a second test user"""
+ return User.objects.create_user(
+ username='testuser2',
+ password='testpass123'
+ )
+
+
+def verified_user(user):
+ """Create a user with verified email"""
+ EmailAddress.objects.create(
+ user=user,
+ verified=True,
+ primary=True
+ )
+ return user
+
+
+def verified_user2(user2):
+ """Create a second user with verified email"""
+ EmailAddress.objects.create(
+ user=user2,
+ verified=True,
+ primary=True
+ )
+ return user2
+
+
+def unverified_user():
+ """Create a user with unverified email"""
+ user = User.objects.create_user(
+ username='unverified',
+ password='testpass123'
+ )
+ EmailAddress.objects.create(
+ user=user,
+ verified=False,
+ primary=True
+ )
+ return user
+
+
+def access_token(verified_user):
+ """Generate JWT access token for verified user"""
+ refresh = RefreshToken.for_user(verified_user)
+ return str(refresh.access_token)
+
+
+def access_token2(verified_user2):
+ """Generate JWT access token for second verified user"""
+ refresh = RefreshToken.for_user(verified_user2)
+ return str(refresh.access_token)
+
+
+def unverified_access_token(unverified_user):
+ """Generate JWT access token for unverified user"""
+ refresh = RefreshToken.for_user(unverified_user)
+ return str(refresh.access_token)
+
+
+def sample_vote_data():
+ """Sample vote data for testing"""
+ return {
+ "url": "https://example.com/test-page",
+ "query": "test search query",
+ "vote_type": "UPVOTE"
+ }
+
+
+def test_vote_on_search_result_success(client, verified_user, access_token, sample_vote_data):
+ """Test successfully voting on a search result"""
+ response = client.post(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps(sample_vote_data),
+ content_type='application/json',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 200
+
+ response_data = response.json()
+ assert response_data['status'] == 'ok'
+ assert 'Vote created successfully' in response_data['message']
+
+ # Verify vote was created in database
+ vote = SearchResultVote.objects.get(
+ user=verified_user,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query']
+ )
+ assert vote.vote_type == 'UPVOTE'
+
+
+def test_vote_update_existing_vote(client, verified_user, access_token, sample_vote_data):
+ """Test updating an existing vote"""
+ # Create initial vote
+ SearchResultVote.objects.create(
+ user=verified_user,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query'],
+ vote_type='UPVOTE'
+ )
+
+ # Update to downvote
+ updated_vote_data = sample_vote_data.copy()
+ updated_vote_data['vote_type'] = 'DOWNVOTE'
+
+ response = client.post(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps(updated_vote_data),
+ content_type='application/json',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 200
+
+ response_data = response.json()
+ assert response_data['status'] == 'ok'
+ assert 'Vote updated successfully' in response_data['message']
+
+ # Verify vote was updated
+ vote = SearchResultVote.objects.get(
+ user=verified_user,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query']
+ )
+ assert vote.vote_type == 'DOWNVOTE'
+
+
+def test_vote_invalid_vote_type(client, access_token, sample_vote_data):
+ """Test voting with invalid vote type"""
+ invalid_vote_data = sample_vote_data.copy()
+ invalid_vote_data['vote_type'] = 'INVALID'
+
+ response = client.post(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps(invalid_vote_data),
+ content_type='application/json',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 400
+
+ response_data = response.json()
+ assert response_data['status'] == 'error'
+ assert 'Invalid vote type' in response_data['message']
+
+
+def test_vote_without_authentication(client, sample_vote_data):
+ """Test voting without JWT token"""
+ response = client.post(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps(sample_vote_data),
+ content_type='application/json'
+ )
+
+ assert response.status_code == 401
+
+
+def test_vote_with_unverified_email(client, unverified_access_token, sample_vote_data):
+ """Test voting with unverified email address"""
+ response = client.post(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps(sample_vote_data),
+ content_type='application/json',
+ HTTP_AUTHORIZATION=f'Bearer {unverified_access_token}'
+ )
+
+ assert response.status_code == 403
+
+ response_data = response.json()
+ assert response_data['status'] == 'error'
+ assert 'Email address is not verified' in response_data['message']
+
+
+def test_get_vote_counts_single_url(client, verified_user, verified_user2, access_token, sample_vote_data):
+ """Test getting vote counts for a single URL"""
+ # Create votes from different users
+ SearchResultVote.objects.create(
+ user=verified_user,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query'],
+ vote_type='UPVOTE'
+ )
+ SearchResultVote.objects.create(
+ user=verified_user2,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query'],
+ vote_type='DOWNVOTE'
+ )
+
+ response = client.get(
+ f'/api/v1/platform/search-results/votes?query={sample_vote_data["query"]}&urls={sample_vote_data["url"]}',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 200
+
+ response_data = response.json()
+ assert 'votes' in response_data
+ assert sample_vote_data['url'] in response_data['votes']
+
+ vote_stats = response_data['votes'][sample_vote_data['url']]
+ assert vote_stats['upvotes'] == 1
+ assert vote_stats['downvotes'] == 1
+ assert vote_stats['user_vote'] == 'UPVOTE' # Current user's vote
+
+
+def test_get_vote_counts_multiple_urls(client, verified_user, access_token):
+ """Test getting vote counts for multiple URLs"""
+ url1 = "https://example.com/page1"
+ url2 = "https://example.com/page2"
+ query = "test query"
+
+ # Create votes for different URLs
+ SearchResultVote.objects.create(
+ user=verified_user,
+ url=url1,
+ query=query,
+ vote_type='UPVOTE'
+ )
+ SearchResultVote.objects.create(
+ user=verified_user,
+ url=url2,
+ query=query,
+ vote_type='DOWNVOTE'
+ )
+
+ urls_param = f"{url1},{url2}"
+ response = client.get(
+ f'/api/v1/platform/search-results/votes?query={query}&urls={urls_param}',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 200
+
+ response_data = response.json()
+ assert len(response_data['votes']) == 2
+ assert url1 in response_data['votes']
+ assert url2 in response_data['votes']
+
+ assert response_data['votes'][url1]['user_vote'] == 'UPVOTE'
+ assert response_data['votes'][url2]['user_vote'] == 'DOWNVOTE'
+
+
+def test_get_vote_counts_no_urls(client, access_token):
+ """Test getting vote counts without providing URLs"""
+ response = client.get(
+ '/api/v1/platform/search-results/votes?query=test&urls=',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 400
+
+ response_data = response.json()
+ assert response_data['status'] == 'error'
+ assert 'At least one URL must be provided' in response_data['message']
+
+
+def test_remove_vote_success(client, verified_user, access_token, sample_vote_data):
+ """Test successfully removing a vote"""
+ # Create a vote first
+ SearchResultVote.objects.create(
+ user=verified_user,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query'],
+ vote_type='UPVOTE'
+ )
+
+ response = client.delete(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps({
+ 'url': sample_vote_data['url'],
+ 'query': sample_vote_data['query']
+ }),
+ content_type='application/json',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 200
+
+ response_data = response.json()
+ assert response_data['status'] == 'ok'
+ assert 'Vote removed successfully' in response_data['message']
+
+ # Verify vote was deleted
+ assert not SearchResultVote.objects.filter(
+ user=verified_user,
+ url=sample_vote_data['url'],
+ query=sample_vote_data['query']
+ ).exists()
+
+
+def test_remove_nonexistent_vote(client, access_token, sample_vote_data):
+ """Test removing a vote that doesn't exist"""
+ response = client.delete(
+ '/api/v1/platform/search-results/vote',
+ data=json.dumps({
+ 'url': sample_vote_data['url'],
+ 'query': sample_vote_data['query']
+ }),
+ content_type='application/json',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 404
+
+ response_data = response.json()
+ assert response_data['status'] == 'error'
+ assert 'No vote found to remove' in response_data['message']
+
+
+def test_get_user_vote_history(client, verified_user, access_token):
+ """Test getting user's voting history"""
+ # Create multiple votes
+ votes_data = [
+ {"url": "https://example.com/page1", "query": "query1", "vote_type": "UPVOTE"},
+ {"url": "https://example.com/page2", "query": "query2", "vote_type": "DOWNVOTE"},
+ {"url": "https://example.com/page3", "query": "query3", "vote_type": "UPVOTE"},
+ ]
+
+ for vote_data in votes_data:
+ SearchResultVote.objects.create(
+ user=verified_user,
+ **vote_data
+ )
+
+ response = client.get(
+ '/api/v1/platform/search-results/my-votes',
+ HTTP_AUTHORIZATION=f'Bearer {access_token}'
+ )
+
+ assert response.status_code == 200
+
+ response_data = response.json()
+ assert 'items' in response_data # Paginated response
+ assert len(response_data['items']) == 3
+
+ # Verify the votes are ordered by timestamp (newest first)
+ for i, vote in enumerate(response_data['items']):
+ assert vote['url'] == votes_data[2-i]['url'] # Reverse order due to ordering by -timestamp
+ assert vote['query'] == votes_data[2-i]['query']
+ assert vote['vote_type'] == votes_data[2-i]['vote_type']
+
+
+def test_vote_unique_constraint(verified_user):
+ """Test that the unique constraint works (one vote per user per URL per query)"""
+ from django.db import IntegrityError, transaction
+
+ vote_data = {
+ "user": verified_user,
+ "url": "https://example.com/test",
+ "query": "test query",
+ "vote_type": "UPVOTE"
+ }
+
+ # Create first vote
+ vote1 = SearchResultVote.objects.create(**vote_data)
+
+ # Try to create duplicate vote - should raise IntegrityError
+ with pytest.raises(IntegrityError):
+ with transaction.atomic():
+ SearchResultVote.objects.create(**vote_data)
+
+ # Verify only one vote exists (in a new transaction)
+ with transaction.atomic():
+ count = SearchResultVote.objects.filter(
+ user=verified_user,
+ url=vote_data['url'],
+ query=vote_data['query']
+ ).count()
+ assert count == 1
+
+
+def test_vote_model_str_representation(verified_user):
+ """Test the string representation of SearchResultVote model"""
+ vote = SearchResultVote.objects.create(
+ user=verified_user,
+ url="https://example.com/test",
+ query="test query",
+ vote_type="UPVOTE"
+ )
+
+ # The model doesn't have a custom __str__ method, so it will use the default
+ # This test ensures the model can be created and accessed properly
+ assert vote.user == verified_user
+ assert vote.url == "https://example.com/test"
+ assert vote.query == "test query"
+ assert vote.vote_type == "UPVOTE"
+ assert vote.timestamp is not None