Updated the get_page_count logic to let the converter raise UnknownFileFormat
and let the document model handle the exception, defaulting to one page count and saving a comment on the document description
This commit is contained in:
@@ -119,12 +119,7 @@ def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, **
|
|||||||
|
|
||||||
|
|
||||||
def get_page_count(input_filepath):
|
def get_page_count(input_filepath):
|
||||||
try:
|
return backend.get_page_count(input_filepath)
|
||||||
return backend.get_page_count(input_filepath)
|
|
||||||
except UnknownFileFormat:
|
|
||||||
# If converter backend doesn't understand the format return
|
|
||||||
# 1 as the total page count
|
|
||||||
return 1
|
|
||||||
|
|
||||||
|
|
||||||
def get_document_dimensions(document, *args, **kwargs):
|
def get_document_dimensions(document, *args, **kwargs):
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from ast import literal_eval
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.utils.translation import ugettext
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.contenttypes import generic
|
from django.contrib.contenttypes import generic
|
||||||
from django.contrib.comments.models import Comment
|
from django.contrib.comments.models import Comment
|
||||||
@@ -166,9 +167,18 @@ class Document(models.Model):
|
|||||||
|
|
||||||
def update_page_count(self, save=True):
|
def update_page_count(self, save=True):
|
||||||
handle, filepath = tempfile.mkstemp()
|
handle, filepath = tempfile.mkstemp()
|
||||||
|
# Just need the filepath, close the file description
|
||||||
|
os.close(handle)
|
||||||
|
|
||||||
self.save_to_file(filepath)
|
self.save_to_file(filepath)
|
||||||
detected_pages = get_page_count(filepath)
|
try:
|
||||||
os.close(handle)
|
detected_pages = get_page_count(filepath)
|
||||||
|
except UnknownFileFormat:
|
||||||
|
# If converter backend doesn't understand the format,
|
||||||
|
# use 1 as the total page count
|
||||||
|
detected_pages = 1
|
||||||
|
self.description = ugettext(u'This document\'s file format is not known, the page count has therefore defaulted to 1.')
|
||||||
|
self.save()
|
||||||
try:
|
try:
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|||||||
Reference in New Issue
Block a user