mirror of
https://github.com/lemeow125/DocManagerBackend.git
synced 2025-01-18 17:13:00 +08:00
Add fix for file uploads with . symbols inside the filename
This commit is contained in:
parent
e0eba6ca21
commit
7584f09b62
3 changed files with 25 additions and 14 deletions
|
@ -37,6 +37,7 @@ class PDFHandler(FileSystemEventHandler):
|
||||||
def process_pdf(self, file_path):
|
def process_pdf(self, file_path):
|
||||||
try:
|
try:
|
||||||
filename = os.path.basename(file_path)
|
filename = os.path.basename(file_path)
|
||||||
|
filename = str(filename).replace(" ", "")
|
||||||
metadata = ""
|
metadata = ""
|
||||||
document_type = ""
|
document_type = ""
|
||||||
|
|
||||||
|
@ -65,10 +66,7 @@ class PDFHandler(FileSystemEventHandler):
|
||||||
if line.strip():
|
if line.strip():
|
||||||
document_type = line.strip().lower()
|
document_type = line.strip().lower()
|
||||||
break
|
break
|
||||||
if (
|
if not document_type:
|
||||||
not document_type
|
|
||||||
or document_type not in Document.DOCUMENT_TYPE_CHOICES
|
|
||||||
):
|
|
||||||
document_type = "other"
|
document_type = "other"
|
||||||
|
|
||||||
metadata += text
|
metadata += text
|
||||||
|
@ -84,9 +82,11 @@ class PDFHandler(FileSystemEventHandler):
|
||||||
)
|
)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
DOCUMENT.file.save(name=filename, content=File(open(file_path, "rb")))
|
DOCUMENT.file.save(
|
||||||
|
name=filename, content=File(open(file_path, "rb")))
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
f"Document '{filename}' created successfully with type '{document_type}'."
|
f"Document '{filename}' created successfully with type '{
|
||||||
|
document_type}'."
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -6,20 +6,14 @@ import uuid
|
||||||
class Document(models.Model):
|
class Document(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
DOCUMENT_TYPE_CHOICES = (
|
|
||||||
("memorandum", "Memorandum"),
|
|
||||||
("hoa", "HOA"),
|
|
||||||
("documented procedures manual", "Documented Procedures Manual"),
|
|
||||||
("other", "Other"),
|
|
||||||
)
|
|
||||||
document_type = models.CharField(
|
document_type = models.CharField(
|
||||||
max_length=32, choices=DOCUMENT_TYPE_CHOICES, null=False, blank=False
|
max_length=32, null=False, blank=False
|
||||||
)
|
)
|
||||||
number_pages = models.IntegerField(null=False, blank=False)
|
number_pages = models.IntegerField(null=False, blank=False)
|
||||||
ocr_metadata = models.TextField(null=True, blank=True)
|
ocr_metadata = models.TextField(null=True, blank=True)
|
||||||
|
|
||||||
def upload_to(instance, filename):
|
def upload_to(instance, filename):
|
||||||
_, extension = filename.split(".")
|
_, extension = filename.rsplit(".", 1)
|
||||||
return "documents/%s_%s.%s" % (now(), str(uuid.uuid4()), extension)
|
return "documents/%s_%s.%s" % (now(), str(uuid.uuid4()), extension)
|
||||||
|
|
||||||
file = models.FileField(upload_to=upload_to)
|
file = models.FileField(upload_to=upload_to)
|
||||||
|
|
|
@ -2,11 +2,28 @@ from rest_framework import serializers
|
||||||
from .models import Document
|
from .models import Document
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
# For Head to edit document info
|
||||||
|
file = serializers.FileField(required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Document
|
||||||
|
fields = [
|
||||||
|
"name",
|
||||||
|
"file",
|
||||||
|
"document_type",
|
||||||
|
"number_pages",
|
||||||
|
"date_uploaded",
|
||||||
|
]
|
||||||
|
read_only_fields = ["id"]
|
||||||
|
|
||||||
|
|
||||||
class DocumentUploadSerializer(serializers.ModelSerializer):
|
class DocumentUploadSerializer(serializers.ModelSerializer):
|
||||||
# For staff document uploads
|
# For staff document uploads
|
||||||
date_uploaded = serializers.DateTimeField(
|
date_uploaded = serializers.DateTimeField(
|
||||||
format="%m-%d-%Y %I:%M %p", read_only=True
|
format="%m-%d-%Y %I:%M %p", read_only=True
|
||||||
)
|
)
|
||||||
|
file = serializers.FileField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Document
|
model = Document
|
||||||
|
|
Loading…
Reference in a new issue