mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-09 09:58:20 -05:00
I'm not sure how this would have worked before
This commit is contained in:
parent
3827c52194
commit
14375e19ef
@ -34,42 +34,64 @@ class UploadForm(forms.Form):
|
|||||||
document = forms.FileField()
|
document = forms.FileField()
|
||||||
signature = forms.CharField(max_length=256)
|
signature = forms.CharField(max_length=256)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
forms.Form.__init__(*args, **kwargs)
|
||||||
|
self._file_type = None
|
||||||
|
|
||||||
def clean_correspondent(self):
|
def clean_correspondent(self):
|
||||||
"""
|
"""
|
||||||
I suppose it might look cleaner to use .get_or_create() here, but that
|
I suppose it might look cleaner to use .get_or_create() here, but that
|
||||||
would also allow someone to fill up the db with bogus correspondents
|
would also allow someone to fill up the db with bogus correspondents
|
||||||
before all validation was met.
|
before all validation was met.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
corresp = self.cleaned_data.get("correspondent")
|
corresp = self.cleaned_data.get("correspondent")
|
||||||
|
|
||||||
if not corresp:
|
if not corresp:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not Correspondent.SAFE_REGEX.match(corresp) or " - " in corresp:
|
if not Correspondent.SAFE_REGEX.match(corresp) or " - " in corresp:
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
"That correspondent name is suspicious.")
|
"That correspondent name is suspicious.")
|
||||||
|
|
||||||
return corresp
|
return corresp
|
||||||
|
|
||||||
def clean_title(self):
|
def clean_title(self):
|
||||||
|
|
||||||
title = self.cleaned_data.get("title")
|
title = self.cleaned_data.get("title")
|
||||||
|
|
||||||
if not title:
|
if not title:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not Correspondent.SAFE_REGEX.match(title) or " - " in title:
|
if not Correspondent.SAFE_REGEX.match(title) or " - " in title:
|
||||||
raise forms.ValidationError("That title is suspicious.")
|
raise forms.ValidationError("That title is suspicious.")
|
||||||
|
|
||||||
|
return title
|
||||||
|
|
||||||
def clean_document(self):
|
def clean_document(self):
|
||||||
|
|
||||||
document = self.cleaned_data.get("document").read()
|
document = self.cleaned_data.get("document").read()
|
||||||
|
|
||||||
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
|
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
|
||||||
file_type = m.id_buffer(document)
|
file_type = m.id_buffer(document)
|
||||||
|
|
||||||
if file_type not in self.TYPE_LOOKUP:
|
if file_type not in self.TYPE_LOOKUP:
|
||||||
raise forms.ValidationError("The file type is invalid.")
|
raise forms.ValidationError("The file type is invalid.")
|
||||||
return document, self.TYPE_LOOKUP[file_type]
|
|
||||||
|
self._file_type = self.TYPE_LOOKUP[file_type]
|
||||||
|
|
||||||
|
return document
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
corresp = self.clened_data("correspondent")
|
|
||||||
title = self.cleaned_data("title")
|
corresp = self.clened_data.get("correspondent")
|
||||||
signature = self.cleaned_data("signature")
|
title = self.cleaned_data.get("title")
|
||||||
|
signature = self.cleaned_data.get("signature")
|
||||||
|
|
||||||
if sha256(corresp + title + self.SECRET).hexdigest() == signature:
|
if sha256(corresp + title + self.SECRET).hexdigest() == signature:
|
||||||
return True
|
return self.cleaned_data
|
||||||
return False
|
|
||||||
|
raise forms.ValidationError("The signature provided did not validate")
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""
|
"""
|
||||||
@ -78,14 +100,14 @@ class UploadForm(forms.Form):
|
|||||||
form do that as well. Think of it as a poor-man's queue server.
|
form do that as well. Think of it as a poor-man's queue server.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
correspondent = self.clened_data("correspondent")
|
correspondent = self.clened_data.get("correspondent")
|
||||||
title = self.cleaned_data("title")
|
title = self.cleaned_data.get("title")
|
||||||
document, file_type = self.cleaned_data.get("document")
|
document = self.cleaned_data.get("document")
|
||||||
|
|
||||||
t = int(mktime(datetime.now()))
|
t = int(mktime(datetime.now()))
|
||||||
file_name = os.path.join(
|
file_name = os.path.join(
|
||||||
Consumer.CONSUME,
|
Consumer.CONSUME,
|
||||||
"{} - {}.{}".format(correspondent, title, file_type)
|
"{} - {}.{}".format(correspondent, title, self._file_type)
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(file_name, "wb") as f:
|
with open(file_name, "wb") as f:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user