mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-06 11:11:56 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e0a4e5bdb |
No files matched your search
@@ -1330,6 +1330,11 @@ class SavedViewFilterRuleSerializer(serializers.ModelSerializer[SavedViewFilterR
|
|||||||
|
|
||||||
class SavedViewSerializer(OwnedObjectSerializer):
|
class SavedViewSerializer(OwnedObjectSerializer):
|
||||||
filter_rules = SavedViewFilterRuleSerializer(many=True)
|
filter_rules = SavedViewFilterRuleSerializer(many=True)
|
||||||
|
display_fields = serializers.ListField(
|
||||||
|
child=serializers.CharField(),
|
||||||
|
required=False,
|
||||||
|
allow_null=True,
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SavedView
|
model = SavedView
|
||||||
@@ -1471,11 +1476,13 @@ class SavedViewSerializer(OwnedObjectSerializer):
|
|||||||
attrs = super().validate(attrs)
|
attrs = super().validate(attrs)
|
||||||
if "display_fields" in attrs and attrs["display_fields"] is not None:
|
if "display_fields" in attrs and attrs["display_fields"] is not None:
|
||||||
for field in attrs["display_fields"]:
|
for field in attrs["display_fields"]:
|
||||||
if (
|
# i.e. 'custom_field_' followed by the id of a custom field
|
||||||
SavedView.DisplayFields.CUSTOM_FIELD[:-2] in field
|
custom_field = re.fullmatch(
|
||||||
): # i.e. check for 'custom_field_' prefix
|
rf"{re.escape(SavedView.DisplayFields.CUSTOM_FIELD[:-2])}(\d+)",
|
||||||
field_id = int(re.search(r"\d+", field)[0])
|
field,
|
||||||
if not CustomField.objects.filter(id=field_id).exists():
|
)
|
||||||
|
if custom_field:
|
||||||
|
if not CustomField.objects.filter(id=custom_field[1]).exists():
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
f"Invalid field: {field}",
|
f"Invalid field: {field}",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
@@ -509,39 +507,6 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
|
|||||||
|
|
||||||
self.assertEqual(stored_text, {})
|
self.assertEqual(stored_text, {})
|
||||||
|
|
||||||
def test_api_accepts_a_logo_too_large_to_hold_in_memory(self) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- A PNG logo larger than Django's in memory upload limit, which is
|
|
||||||
stored in a temporary file while it is received
|
|
||||||
WHEN:
|
|
||||||
- Uploaded via PATCH to app config
|
|
||||||
THEN:
|
|
||||||
- The upload is accepted and the stored logo is a valid image
|
|
||||||
"""
|
|
||||||
# Random pixels do not compress, so this is a few megabytes as a PNG
|
|
||||||
image = Image.frombytes("RGB", (1100, 1100), os.urandom(1100 * 1100 * 3))
|
|
||||||
logo = BytesIO()
|
|
||||||
image.save(logo, format="PNG")
|
|
||||||
assert logo.getbuffer().nbytes > settings.FILE_UPLOAD_MAX_MEMORY_SIZE
|
|
||||||
|
|
||||||
response = self.client.patch(
|
|
||||||
f"{self.ENDPOINT}1/",
|
|
||||||
{
|
|
||||||
"app_logo": SimpleUploadedFile(
|
|
||||||
name="large-logo.png",
|
|
||||||
content=logo.getvalue(),
|
|
||||||
content_type="image/png",
|
|
||||||
),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
||||||
config = ApplicationConfiguration.objects.first()
|
|
||||||
assert config is not None
|
|
||||||
with Image.open(config.app_logo.path) as stored_logo:
|
|
||||||
self.assertEqual(stored_logo.size, (1100, 1100))
|
|
||||||
|
|
||||||
def test_api_accepts_valid_gif_logo(self) -> None:
|
def test_api_accepts_valid_gif_logo(self) -> None:
|
||||||
"""
|
"""
|
||||||
GIVEN:
|
GIVEN:
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import pytest
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.test import APIClient
|
||||||
|
|
||||||
|
from documents.models import CustomField
|
||||||
|
from documents.models import SavedView
|
||||||
|
|
||||||
|
|
||||||
|
class TestSavedViewDisplayFields:
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value",
|
||||||
|
[
|
||||||
|
pytest.param(["custom_field_abc"], id="no-id"),
|
||||||
|
pytest.param(["custom_field_"], id="empty-id"),
|
||||||
|
pytest.param(["xcustom_field_{id}"], id="prefixed"),
|
||||||
|
pytest.param(["custom_field_{id}x"], id="suffixed"),
|
||||||
|
pytest.param([1], id="not-a-string"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_malformed_display_fields_are_a_400(
|
||||||
|
self,
|
||||||
|
admin_client: APIClient,
|
||||||
|
admin_user: User,
|
||||||
|
value: object,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A saved view and a custom field
|
||||||
|
WHEN:
|
||||||
|
- Its display fields are patched to something that names no field
|
||||||
|
THEN:
|
||||||
|
- The response is a 400 and the stored display fields are unchanged
|
||||||
|
"""
|
||||||
|
custom_field = CustomField.objects.create(
|
||||||
|
name="cf",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
view = SavedView.objects.create(
|
||||||
|
name="view",
|
||||||
|
owner=admin_user,
|
||||||
|
sort_field="created",
|
||||||
|
display_fields=["title"],
|
||||||
|
)
|
||||||
|
if isinstance(value, list):
|
||||||
|
value = [
|
||||||
|
item.format(id=custom_field.pk) if isinstance(item, str) else item
|
||||||
|
for item in value
|
||||||
|
]
|
||||||
|
|
||||||
|
response = admin_client.patch(
|
||||||
|
f"/api/saved_views/{view.pk}/",
|
||||||
|
{"display_fields": value},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||||
|
view.refresh_from_db()
|
||||||
|
assert view.display_fields == ["title"]
|
||||||
|
|
||||||
|
def test_a_custom_field_display_name_is_accepted(
|
||||||
|
self,
|
||||||
|
admin_client: APIClient,
|
||||||
|
admin_user: User,
|
||||||
|
) -> None:
|
||||||
|
custom_field = CustomField.objects.create(
|
||||||
|
name="cf",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
view = SavedView.objects.create(
|
||||||
|
name="view",
|
||||||
|
owner=admin_user,
|
||||||
|
sort_field="created",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = admin_client.patch(
|
||||||
|
f"/api/saved_views/{view.pk}/",
|
||||||
|
{"display_fields": ["title", f"custom_field_{custom_field.pk}"]},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_200_OK
|
||||||
@@ -303,9 +303,7 @@ class ApplicationConfigurationSerializer(
|
|||||||
|
|
||||||
return InMemoryUploadedFile(
|
return InMemoryUploadedFile(
|
||||||
file=data,
|
file=data,
|
||||||
# An upload past the in memory limit is a temporary file, which has no
|
field_name=file.field_name,
|
||||||
# field name of its own
|
|
||||||
field_name="app_logo",
|
|
||||||
name=file.name,
|
name=file.name,
|
||||||
content_type=file.content_type,
|
content_type=file.content_type,
|
||||||
size=data.getbuffer().nbytes,
|
size=data.getbuffer().nbytes,
|
||||||
|
|||||||
Reference in new issue
Block a user