mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 19:17:13 -05:00 
			
		
		
		
	validation for regular expressions on matching models #605
This commit is contained in:
		
							parent
							
								
									8da85d3609
								
							
						
					
					
						commit
						f397c5472c
					
				@ -1,11 +1,13 @@
 | 
				
			|||||||
 | 
					import re
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import magic
 | 
					import magic
 | 
				
			||||||
from django.utils.text import slugify
 | 
					from django.utils.text import slugify
 | 
				
			||||||
from rest_framework import serializers
 | 
					from rest_framework import serializers
 | 
				
			||||||
from rest_framework.fields import SerializerMethodField
 | 
					from rest_framework.fields import SerializerMethodField
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from . import bulk_edit
 | 
					from . import bulk_edit
 | 
				
			||||||
from .models import Correspondent, Tag, Document, Log, DocumentType, \
 | 
					from .models import Correspondent, Tag, Document, DocumentType, \
 | 
				
			||||||
    SavedView, SavedViewFilterRule
 | 
					    SavedView, SavedViewFilterRule, MatchingModel
 | 
				
			||||||
from .parsers import is_mime_type_supported
 | 
					from .parsers import is_mime_type_supported
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.utils.translation import gettext as _
 | 
					from django.utils.translation import gettext as _
 | 
				
			||||||
@ -33,16 +35,27 @@ class DynamicFieldsModelSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
                self.fields.pop(field_name)
 | 
					                self.fields.pop(field_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CorrespondentSerializer(serializers.ModelSerializer):
 | 
					class MatchingModelSerializer(serializers.ModelSerializer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    document_count = serializers.IntegerField(read_only=True)
 | 
					    document_count = serializers.IntegerField(read_only=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    last_correspondence = serializers.DateTimeField(read_only=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def get_slug(self, obj):
 | 
					    def get_slug(self, obj):
 | 
				
			||||||
        return slugify(obj.name)
 | 
					        return slugify(obj.name)
 | 
				
			||||||
    slug = SerializerMethodField()
 | 
					    slug = SerializerMethodField()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def validate_match(self, match):
 | 
				
			||||||
 | 
					        if 'matching_algorithm' in self.initial_data and self.initial_data['matching_algorithm'] == MatchingModel.MATCH_REGEX:  # NOQA: E501
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                re.compile(match)
 | 
				
			||||||
 | 
					            except Exception as e:
 | 
				
			||||||
 | 
					                raise serializers.ValidationError(_("Invalid regular expresssion: ") + str(e))
 | 
				
			||||||
 | 
					        return match
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CorrespondentSerializer(MatchingModelSerializer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    last_correspondence = serializers.DateTimeField(read_only=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = Correspondent
 | 
					        model = Correspondent
 | 
				
			||||||
        fields = (
 | 
					        fields = (
 | 
				
			||||||
@ -57,13 +70,7 @@ class CorrespondentSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class DocumentTypeSerializer(serializers.ModelSerializer):
 | 
					class DocumentTypeSerializer(MatchingModelSerializer):
 | 
				
			||||||
 | 
					 | 
				
			||||||
    document_count = serializers.IntegerField(read_only=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def get_slug(self, obj):
 | 
					 | 
				
			||||||
        return slugify(obj.name)
 | 
					 | 
				
			||||||
    slug = SerializerMethodField()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = DocumentType
 | 
					        model = DocumentType
 | 
				
			||||||
@ -78,13 +85,7 @@ class DocumentTypeSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TagSerializer(serializers.ModelSerializer):
 | 
					class TagSerializer(MatchingModelSerializer):
 | 
				
			||||||
 | 
					 | 
				
			||||||
    document_count = serializers.IntegerField(read_only=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def get_slug(self, obj):
 | 
					 | 
				
			||||||
        return slugify(obj.name)
 | 
					 | 
				
			||||||
    slug = SerializerMethodField()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = Tag
 | 
					        model = Tag
 | 
				
			||||||
 | 
				
			|||||||
@ -14,7 +14,7 @@ from rest_framework.test import APITestCase
 | 
				
			|||||||
from whoosh.writing import AsyncWriter
 | 
					from whoosh.writing import AsyncWriter
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from documents import index, bulk_edit
 | 
					from documents import index, bulk_edit
 | 
				
			||||||
from documents.models import Document, Correspondent, DocumentType, Tag, SavedView
 | 
					from documents.models import Document, Correspondent, DocumentType, Tag, SavedView, MatchingModel
 | 
				
			||||||
from documents.tests.utils import DirectoriesMixin
 | 
					from documents.tests.utils import DirectoriesMixin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -772,6 +772,41 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
 | 
				
			|||||||
        self.assertEqual(response.status_code, 200)
 | 
					        self.assertEqual(response.status_code, 200)
 | 
				
			||||||
        self.assertListEqual(response.data, ["test", "test2"])
 | 
					        self.assertListEqual(response.data, ["test", "test2"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_invalid_regex_other_algorithm(self):
 | 
				
			||||||
 | 
					        for endpoint in ['correspondents', 'tags', 'document_types']:
 | 
				
			||||||
 | 
					            response = self.client.post(f"/api/{endpoint}/", {
 | 
				
			||||||
 | 
					                "name": "test",
 | 
				
			||||||
 | 
					                "matching_algorithm": MatchingModel.MATCH_ANY,
 | 
				
			||||||
 | 
					                "match": "["
 | 
				
			||||||
 | 
					            }, format='json')
 | 
				
			||||||
 | 
					            self.assertEqual(response.status_code, 201, endpoint)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_invalid_regex(self):
 | 
				
			||||||
 | 
					        for endpoint in ['correspondents', 'tags', 'document_types']:
 | 
				
			||||||
 | 
					            response = self.client.post(f"/api/{endpoint}/", {
 | 
				
			||||||
 | 
					                "name": "test",
 | 
				
			||||||
 | 
					                "matching_algorithm": MatchingModel.MATCH_REGEX,
 | 
				
			||||||
 | 
					                "match": "["
 | 
				
			||||||
 | 
					            }, format='json')
 | 
				
			||||||
 | 
					            self.assertEqual(response.status_code, 400, endpoint)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_valid_regex(self):
 | 
				
			||||||
 | 
					        for endpoint in ['correspondents', 'tags', 'document_types']:
 | 
				
			||||||
 | 
					            response = self.client.post(f"/api/{endpoint}/", {
 | 
				
			||||||
 | 
					                "name": "test",
 | 
				
			||||||
 | 
					                "matching_algorithm": MatchingModel.MATCH_REGEX,
 | 
				
			||||||
 | 
					                "match": "[0-9]"
 | 
				
			||||||
 | 
					            }, format='json')
 | 
				
			||||||
 | 
					            self.assertEqual(response.status_code, 201, endpoint)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_regex_no_algorithm(self):
 | 
				
			||||||
 | 
					        for endpoint in ['correspondents', 'tags', 'document_types']:
 | 
				
			||||||
 | 
					            response = self.client.post(f"/api/{endpoint}/", {
 | 
				
			||||||
 | 
					                "name": "test",
 | 
				
			||||||
 | 
					                "match": "[0-9]"
 | 
				
			||||||
 | 
					            }, format='json')
 | 
				
			||||||
 | 
					            self.assertEqual(response.status_code, 201, endpoint)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestBulkEdit(DirectoriesMixin, APITestCase):
 | 
					class TestBulkEdit(DirectoriesMixin, APITestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,7 @@ msgid ""
 | 
				
			|||||||
msgstr ""
 | 
					msgstr ""
 | 
				
			||||||
"Project-Id-Version: PACKAGE VERSION\n"
 | 
					"Project-Id-Version: PACKAGE VERSION\n"
 | 
				
			||||||
"Report-Msgid-Bugs-To: \n"
 | 
					"Report-Msgid-Bugs-To: \n"
 | 
				
			||||||
"POT-Creation-Date: 2021-02-16 14:52+0100\n"
 | 
					"POT-Creation-Date: 2021-02-23 12:30+0100\n"
 | 
				
			||||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 | 
					"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 | 
				
			||||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 | 
					"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 | 
				
			||||||
"Language-Team: LANGUAGE <LL@li.org>\n"
 | 
					"Language-Team: LANGUAGE <LL@li.org>\n"
 | 
				
			||||||
@ -346,7 +346,11 @@ msgstr ""
 | 
				
			|||||||
msgid "filter rules"
 | 
					msgid "filter rules"
 | 
				
			||||||
msgstr ""
 | 
					msgstr ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#: documents/serialisers.py:370
 | 
					#: documents/serialisers.py:52
 | 
				
			||||||
 | 
					msgid "Invalid regular expresssion: "
 | 
				
			||||||
 | 
					msgstr ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#: documents/serialisers.py:376
 | 
				
			||||||
#, python-format
 | 
					#, python-format
 | 
				
			||||||
msgid "File type %(type)s not supported"
 | 
					msgid "File type %(type)s not supported"
 | 
				
			||||||
msgstr ""
 | 
					msgstr ""
 | 
				
			||||||
@ -411,7 +415,7 @@ msgstr ""
 | 
				
			|||||||
msgid "French"
 | 
					msgid "French"
 | 
				
			||||||
msgstr ""
 | 
					msgstr ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#: paperless/urls.py:114
 | 
					#: paperless/urls.py:118
 | 
				
			||||||
msgid "Paperless-ng administration"
 | 
					msgid "Paperless-ng administration"
 | 
				
			||||||
msgstr ""
 | 
					msgstr ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user