mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 09:48:41 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a563bd15a7 |
No files matched your search
@@ -1330,11 +1330,6 @@ class SavedViewFilterRuleSerializer(serializers.ModelSerializer[SavedViewFilterR
|
||||
|
||||
class SavedViewSerializer(OwnedObjectSerializer):
|
||||
filter_rules = SavedViewFilterRuleSerializer(many=True)
|
||||
display_fields = serializers.ListField(
|
||||
child=serializers.CharField(),
|
||||
required=False,
|
||||
allow_null=True,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = SavedView
|
||||
@@ -1476,13 +1471,11 @@ class SavedViewSerializer(OwnedObjectSerializer):
|
||||
attrs = super().validate(attrs)
|
||||
if "display_fields" in attrs and attrs["display_fields"] is not None:
|
||||
for field in attrs["display_fields"]:
|
||||
# i.e. 'custom_field_' followed by the id of a custom field
|
||||
custom_field = re.fullmatch(
|
||||
rf"{re.escape(SavedView.DisplayFields.CUSTOM_FIELD[:-2])}(\d+)",
|
||||
field,
|
||||
)
|
||||
if custom_field:
|
||||
if not CustomField.objects.filter(id=custom_field[1]).exists():
|
||||
if (
|
||||
SavedView.DisplayFields.CUSTOM_FIELD[:-2] in field
|
||||
): # i.e. check for 'custom_field_' prefix
|
||||
field_id = int(re.search(r"\d+", field)[0])
|
||||
if not CustomField.objects.filter(id=field_id).exists():
|
||||
raise serializers.ValidationError(
|
||||
f"Invalid field: {field}",
|
||||
)
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import json
|
||||
import os
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.test import override_settings
|
||||
from PIL import Image
|
||||
@@ -507,6 +509,39 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
|
||||
|
||||
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:
|
||||
"""
|
||||
GIVEN:
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
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,7 +303,9 @@ class ApplicationConfigurationSerializer(
|
||||
|
||||
return InMemoryUploadedFile(
|
||||
file=data,
|
||||
field_name=file.field_name,
|
||||
# An upload past the in memory limit is a temporary file, which has no
|
||||
# field name of its own
|
||||
field_name="app_logo",
|
||||
name=file.name,
|
||||
content_type=file.content_type,
|
||||
size=data.getbuffer().nbytes,
|
||||
|
||||
Reference in new issue
Block a user