Skip to content

Commit

Permalink
adding file is PDF and ZIP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tommorris committed Dec 18, 2015
1 parent dc6835a commit ec47565
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dmutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

import flask_featureflags

__version__ = '15.9.1'
__version__ = '15.10.0'
15 changes: 15 additions & 0 deletions dmutils/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ def file_is_open_document_format(file_object):
]


def file_is_pdf(file_object):
"""Checks file extension as being PDF."""
return get_extension(file_object.filename) in [
".pdf", ".pda"
]


def file_is_zip(file_object):
"""Checks file extension as being ZIP."""
return get_extension(file_object.filename) in [
".zip"
]


def generate_file_name(framework_slug, bucket_short_name, supplier_id, service_id, field, filename, suffix=None):
if suffix is None:
suffix = default_file_suffix()
Expand Down Expand Up @@ -191,6 +205,7 @@ def get_agreement_document_path(framework_slug, supplier_id, supplier_name, docu


def sanitise_supplier_name(supplier_name):
"""Replace ampersands with 'and' and spaces with a single underscore."""
sanitised_supplier_name = supplier_name.strip().replace(' ', '_').replace('&', 'and')
for bad_char in BAD_SUPPLIER_NAME_CHARACTERS:
sanitised_supplier_name = sanitised_supplier_name.replace(bad_char, '')
Expand Down
10 changes: 9 additions & 1 deletion tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
validate_documents,
upload_document, upload_service_documents,
get_signed_url, get_agreement_document_path,
sanitise_supplier_name)
sanitise_supplier_name, file_is_pdf, file_is_zip)


class TestGenerateFilename(unittest.TestCase):
Expand Down Expand Up @@ -71,6 +71,14 @@ def test_file_is_open_document_format(self):
def test_file_is_not_open_document_format(self):
self.assertFalse(file_is_open_document_format(mock_file('file1.doc', 1)))

def test_file_is_pdf(self):
self.assertTrue(file_is_pdf(mock_file('file.pdf', 1)))
self.assertFalse(file_is_pdf(mock_file('file.doc', 1)))

def test_file_is_zip(self):
self.assertTrue(file_is_zip(mock_file('file.zip', 1)))
self.assertFalse(file_is_zip(mock_file('file.sit', 1)))

def test_validate_documents(self):
self.assertEqual(
validate_documents({'file1': mock_file('file1.pdf', 1)}),
Expand Down

0 comments on commit ec47565

Please sign in to comment.