From 4dddd67d0c514819226adadded639225459c8e42 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 17:48:29 -0700 Subject: [PATCH 1/9] Resolves ambiguous ordering in views --- bookwyrm/views/announcements.py | 2 +- bookwyrm/views/user.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bookwyrm/views/announcements.py b/bookwyrm/views/announcements.py index e522fc7f..05386b7b 100644 --- a/bookwyrm/views/announcements.py +++ b/bookwyrm/views/announcements.py @@ -51,7 +51,7 @@ class Announcements(View): form = forms.AnnouncementForm() data = { "announcements": Paginator( - models.Announcement.objects.all(), PAGE_LENGTH + models.Announcement.objects.order_by("-created_date"), PAGE_LENGTH ).get_page(request.GET.get("page")), "form": form, } diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index f62913a9..87e42b4e 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -88,7 +88,9 @@ class Followers(View): if is_api_request(request): return ActivitypubResponse(user.to_followers_activity(**request.GET)) - paginated = Paginator(user.followers.all(), PAGE_LENGTH) + paginated = Paginator( + user.followers.order_by("-created_date").all(), PAGE_LENGTH + ) data = { "user": user, "is_self": request.user.id == user.id, @@ -107,7 +109,9 @@ class Following(View): if is_api_request(request): return ActivitypubResponse(user.to_following_activity(**request.GET)) - paginated = Paginator(user.following.all(), PAGE_LENGTH) + paginated = Paginator( + user.following.order_by("-created_date").all(), PAGE_LENGTH + ) data = { "user": user, "is_self": request.user.id == user.id, From 8ef1d0d3ac4dde5e11bb80332722bbcaa7a3e6ec Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 18:55:08 -0700 Subject: [PATCH 2/9] Fixes image federation --- bookwyrm/models/fields.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index b59b6107..ca837e03 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -11,8 +11,10 @@ from django.core.exceptions import ValidationError from django.core.files.base import ContentFile from django.db import models from django.forms import ClearableFileInput, ImageField as DjangoImageField +from django.templatetags.static import static from django.utils import timezone from django.utils.translation import gettext_lazy as _ + from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser @@ -354,7 +356,7 @@ def image_serializer(value, alt): url = value.url else: return None - url = "https://%s%s" % (DOMAIN, url) + url = static(url) return activitypub.Document(url=url, name=alt) From 9e6f78c0f5dcea6d91733aeede9df68ab0b59f3c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:01:16 -0700 Subject: [PATCH 3/9] Fixes linting issue --- bookwyrm/models/fields.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index ca837e03..2849f00c 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -18,7 +18,6 @@ from django.utils.translation import gettext_lazy as _ from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser -from bookwyrm.settings import DOMAIN def validate_remote_id(value): From b7563ca221f577f90f24918a13c2373136ab21d9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:01:23 -0700 Subject: [PATCH 4/9] Adds test --- bookwyrm/tests/models/test_fields.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index a8a6096b..bf907bf4 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -426,6 +426,17 @@ class ActivitypubFields(TestCase): self.assertIsInstance(loaded_image, list) self.assertIsInstance(loaded_image[1], ContentFile) + def test_image_serialize(self): + """make sure we're creating sensible image paths""" + ValueMock = namedtuple( + "ValueMock", ("url") + ) + value_mock = ValueMock("images/fish.jpg") + result = fields.image_serializer(value_mock, "hello") + self.assertEqual(result.type, "Document") + self.assertEqual(result.url, "https://test.com/images/fish.jpg") + self.assertEqual(result.name, "hello") + def test_datetime_field(self): """this one is pretty simple, it just has to use isoformat""" instance = fields.DateTimeField() From 68f9b67fd97314e8ac4278dbad76cd06b831c8b9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:30:42 -0700 Subject: [PATCH 5/9] Updates test --- bookwyrm/models/fields.py | 3 +++ bookwyrm/tests/models/test_fields.py | 8 +++----- pytest.ini | 16 ++++++++++++++++ requirements.txt | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 2849f00c..11854dff 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -18,6 +18,7 @@ from django.utils.translation import gettext_lazy as _ from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser +from bookwyrm.settings import DOMAIN def validate_remote_id(value): @@ -356,6 +357,8 @@ def image_serializer(value, alt): else: return None url = static(url) + if not url[:4] == "http": + url = "https://{:s}{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index bf907bf4..65f9a1cc 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -17,7 +17,7 @@ from django.db import models from django.test import TestCase from django.utils import timezone -from bookwyrm import activitypub +from bookwyrm import activitypub, settings from bookwyrm.activitypub.base_activity import ActivityObject from bookwyrm.models import fields, User, Status from bookwyrm.models.base_model import BookWyrmModel @@ -428,13 +428,11 @@ class ActivitypubFields(TestCase): def test_image_serialize(self): """make sure we're creating sensible image paths""" - ValueMock = namedtuple( - "ValueMock", ("url") - ) + ValueMock = namedtuple("ValueMock", ("url")) value_mock = ValueMock("images/fish.jpg") result = fields.image_serializer(value_mock, "hello") self.assertEqual(result.type, "Document") - self.assertEqual(result.url, "https://test.com/images/fish.jpg") + self.assertEqual(result.url, "https://your.domain.here/static/images/fish.jpg") self.assertEqual(result.name, "hello") def test_datetime_field(self): diff --git a/pytest.ini b/pytest.ini index fa9dbc59..8539d116 100644 --- a/pytest.ini +++ b/pytest.ini @@ -4,3 +4,19 @@ python_files = tests.py test_*.py *_tests.py addopts = --cov=bookwyrm --cov-config=.coveragerc markers = integration: marks tests as requiring external resources (deselect with '-m "not integration"') + +env = + DEBUG = false + DOMAIN = your.domain.here + BOOKWYRM_DATABASE_BACKEND = postgres + MEDIA_ROOT = images/ + CELERY_BROKER = "" + REDIS_BROKER_PORT = 6379 + FLOWER_PORT = 8888 + EMAIL_HOST = "smtp.mailgun.org" + EMAIL_PORT = 587 + EMAIL_HOST_USER = "" + EMAIL_HOST_PASSWORD = "" + EMAIL_USE_TLS = true + ENABLE_PREVIEW_IMAGES = false + USE_S3 = false diff --git a/requirements.txt b/requirements.txt index 4a8c15aa..ec0eb1a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,5 @@ coverage==5.1 pytest-django==4.1.0 pytest==6.1.2 pytest-cov==2.10.1 +pytest-env==0.6.2 pytest-xdist==2.3.0 From 815dbc81e3a93c7950c270a25fee68f3e318d8eb Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:50:33 -0700 Subject: [PATCH 6/9] Remove static tempaletag from image_serializer --- bookwyrm/models/fields.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 11854dff..3b369e84 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -11,7 +11,6 @@ from django.core.exceptions import ValidationError from django.core.files.base import ContentFile from django.db import models from django.forms import ClearableFileInput, ImageField as DjangoImageField -from django.templatetags.static import static from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -356,7 +355,6 @@ def image_serializer(value, alt): url = value.url else: return None - url = static(url) if not url[:4] == "http": url = "https://{:s}{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) From a334970692f64b5bad2fb151073d9b9a328c1752 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 20:00:02 -0700 Subject: [PATCH 7/9] Fixes path for image field in test --- bookwyrm/tests/models/test_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 65f9a1cc..caf12b84 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -432,7 +432,7 @@ class ActivitypubFields(TestCase): value_mock = ValueMock("images/fish.jpg") result = fields.image_serializer(value_mock, "hello") self.assertEqual(result.type, "Document") - self.assertEqual(result.url, "https://your.domain.here/static/images/fish.jpg") + self.assertEqual(result.url, "https://your.domain.here/images/fish.jpg") self.assertEqual(result.name, "hello") def test_datetime_field(self): From 39b2f3794745a0a4b6d1576c808874489523f4b1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 20:07:46 -0700 Subject: [PATCH 8/9] One (hopefully) last fix --- bookwyrm/models/fields.py | 2 +- bookwyrm/tests/models/test_fields.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 3b369e84..e1c947f4 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -356,7 +356,7 @@ def image_serializer(value, alt): else: return None if not url[:4] == "http": - url = "https://{:s}{:s}".format(DOMAIN, url) + url = "https://{:s}/{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index caf12b84..0d93d99c 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -17,7 +17,7 @@ from django.db import models from django.test import TestCase from django.utils import timezone -from bookwyrm import activitypub, settings +from bookwyrm import activitypub from bookwyrm.activitypub.base_activity import ActivityObject from bookwyrm.models import fields, User, Status from bookwyrm.models.base_model import BookWyrmModel From 909317066036901e28960385a7933382e7d42037 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 20:16:17 -0700 Subject: [PATCH 9/9] Correct test structure --- bookwyrm/models/fields.py | 2 +- bookwyrm/tests/models/test_fields.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index e1c947f4..3b369e84 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -356,7 +356,7 @@ def image_serializer(value, alt): else: return None if not url[:4] == "http": - url = "https://{:s}/{:s}".format(DOMAIN, url) + url = "https://{:s}{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 0d93d99c..c234ffd0 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -429,7 +429,7 @@ class ActivitypubFields(TestCase): def test_image_serialize(self): """make sure we're creating sensible image paths""" ValueMock = namedtuple("ValueMock", ("url")) - value_mock = ValueMock("images/fish.jpg") + value_mock = ValueMock("/images/fish.jpg") result = fields.image_serializer(value_mock, "hello") self.assertEqual(result.type, "Document") self.assertEqual(result.url, "https://your.domain.here/images/fish.jpg")