From 4aa10c2c7818877364e5384badb982e5e28f171d Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Wed, 17 May 2023 23:28:38 -0400 Subject: [PATCH] :sparkles: Add feed (#489) --- blossom/api/migrations/0028_submission_feed.py | 18 ++++++++++++++++++ blossom/api/models.py | 10 +++++++++- blossom/api/serializers.py | 1 + .../tests/submissions/test_submission_get.py | 14 +++++++++++++- .../tests/submissions/test_submission_yeet.py | 2 +- blossom/api/views/submission.py | 1 + 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 blossom/api/migrations/0028_submission_feed.py diff --git a/blossom/api/migrations/0028_submission_feed.py b/blossom/api/migrations/0028_submission_feed.py new file mode 100644 index 00000000..417074e9 --- /dev/null +++ b/blossom/api/migrations/0028_submission_feed.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.19 on 2023-05-18 02:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("api", "0027_auto_20221004_2042"), + ] + + operations = [ + migrations.AddField( + model_name="submission", + name="feed", + field=models.CharField(blank=True, max_length=50, null=True), + ), + ] diff --git a/blossom/api/models.py b/blossom/api/models.py index 1662cfdf..00761aba 100644 --- a/blossom/api/models.py +++ b/blossom/api/models.py @@ -173,6 +173,10 @@ class Meta: # be run through OCR, this flag should be set. cannot_ocr = models.BooleanField(default=False) + # For reddit posts, keep the source as "reddit" but put the subreddit here so + # we can search by subreddit. + feed = models.CharField(max_length=50, null=True, blank=True) + def __str__(self) -> str: # pragma: no cover return f"{self.original_id}" @@ -255,8 +259,12 @@ def save(self, *args: Any, skip_extras: bool = False, **kwargs: Any) -> None: simply "save the object to the db". """ super(Submission, self).save(*args, **kwargs) + + if not self.feed: + self.feed = self.get_subreddit_name() + if not skip_extras: - if self.is_image and not self.has_ocr_transcription: + if self.is_image and not self.has_ocr_transcription and not self.cannot_ocr: # TODO: This is a great candidate for a basic queue system self.generate_ocr_transcription() diff --git a/blossom/api/serializers.py b/blossom/api/serializers.py index 9f892bd3..71bf38a7 100644 --- a/blossom/api/serializers.py +++ b/blossom/api/serializers.py @@ -69,6 +69,7 @@ class Meta: "cannot_ocr", "redis_id", "removed_from_queue", + "feed", ) # TODO: Omitting the below line while adding `transcription_set` makes # a call to a single submission created with the test data set take diff --git a/blossom/api/tests/submissions/test_submission_get.py b/blossom/api/tests/submissions/test_submission_get.py index fe7f30aa..befc1f88 100644 --- a/blossom/api/tests/submissions/test_submission_get.py +++ b/blossom/api/tests/submissions/test_submission_get.py @@ -68,7 +68,7 @@ def test_list_with_filters(self, client: Client) -> None: create_submission(source=aaa) create_submission(source=aaa) create_submission(source=bbb) - create_submission(source=bbb) + create_submission(source=bbb, feed="ccc") result = client.get(reverse("submission-list"), content_type="application/json", **headers) @@ -96,6 +96,18 @@ def test_list_with_filters(self, client: Client) -> None: assert "AAA" in result.json()["results"][0]["source"] # it will be a full link assert result.json()["results"][0]["id"] == 1 + result = client.get( + reverse("submission-list") + "?feed=ccc", + content_type="application/json", + **headers, + ) + + assert result.status_code == status.HTTP_200_OK + assert len(result.json()["results"]) == 1 + assert "BBB" in result.json()["results"][0]["source"] # it will be a full link + assert result.json()["results"][0]["feed"] == "ccc" + assert result.json()["results"][0]["id"] == 4 + @pytest.mark.parametrize( "time_query,result_count", [ diff --git a/blossom/api/tests/submissions/test_submission_yeet.py b/blossom/api/tests/submissions/test_submission_yeet.py index 81352d55..cff7dfbe 100644 --- a/blossom/api/tests/submissions/test_submission_yeet.py +++ b/blossom/api/tests/submissions/test_submission_yeet.py @@ -65,7 +65,7 @@ def test_yeet_with_no_count(self, client: Client) -> None: assert Submission.objects.count() == 3 def test_yeet_with_no_user(self, client: Client) -> None: - """Verify that yeeting without a count only deletes one.""" + """Verify that yeeting without a user returns an error.""" client, headers, user = setup_user_client(client) response = client.post( diff --git a/blossom/api/views/submission.py b/blossom/api/views/submission.py index 039647c4..e67002d0 100644 --- a/blossom/api/views/submission.py +++ b/blossom/api/views/submission.py @@ -148,6 +148,7 @@ class SubmissionViewSet(viewsets.ModelViewSet): "content_url": ["exact", "isnull"], "redis_id": ["exact", "isnull"], "removed_from_queue": ["exact"], + "feed": ["exact"], } ordering_fields = [ "id",