Merge pull request #62 from danielquinn/issue/53

Compensate for case and format of jpg vs. jpeg
This commit is contained in:
Daniel Quinn 2016-02-23 20:24:39 +00:00
commit 966d5cdd08
4 changed files with 27 additions and 8 deletions

View File

@ -264,6 +264,12 @@ class Consumer(object):
Tag.objects.get_or_create(slug=t, defaults={"name": t})[0]) Tag.objects.get_or_create(slug=t, defaults={"name": t})[0])
return tuple(r) return tuple(r)
def get_suffix(suffix):
suffix = suffix.lower()
if suffix == "jpeg":
return "jpg"
return suffix
# First attempt: "<sender> - <title> - <tags>.<suffix>" # First attempt: "<sender> - <title> - <tags>.<suffix>"
m = re.match(self.REGEX_SENDER_TITLE_TAGS, parseable) m = re.match(self.REGEX_SENDER_TITLE_TAGS, parseable)
if m: if m:
@ -271,17 +277,22 @@ class Consumer(object):
get_sender(m.group(1)), get_sender(m.group(1)),
m.group(2), m.group(2),
get_tags(m.group(3)), get_tags(m.group(3)),
m.group(4) get_suffix(m.group(4))
) )
# Second attempt: "<sender> - <title>.<suffix>" # Second attempt: "<sender> - <title>.<suffix>"
m = re.match(self.REGEX_SENDER_TITLE, parseable) m = re.match(self.REGEX_SENDER_TITLE, parseable)
if m: if m:
return get_sender(m.group(1)), m.group(2), (), m.group(3) return (
get_sender(m.group(1)),
m.group(2),
(),
get_suffix(m.group(3))
)
# That didn't work, so we assume sender and tags are None # That didn't work, so we assume sender and tags are None
m = re.match(self.REGEX_TITLE, parseable) m = re.match(self.REGEX_TITLE, parseable)
return None, m.group(1), (), m.group(2) return None, m.group(1), (), get_suffix(m.group(2))
def _store(self, text, doc): def _store(self, text, doc):

View File

@ -7,15 +7,23 @@ class TestAttachment(TestCase):
TAGS = ("tag1", "tag2", "tag3") TAGS = ("tag1", "tag2", "tag3")
CONSUMER = Consumer() CONSUMER = Consumer()
SUFFIXES = (
"pdf", "png", "jpg", "jpeg", "gif",
"PDF", "PNG", "JPG", "JPEG", "GIF",
"PdF", "PnG", "JpG", "JPeG", "GiF",
)
def _test_guess_attributes_from_name(self, path, sender, title, tags): def _test_guess_attributes_from_name(self, path, sender, title, tags):
for suffix in ("pdf", "png", "jpg", "jpeg", "gif"): for suffix in self.SUFFIXES:
f = path.format(suffix) f = path.format(suffix)
results = self.CONSUMER._guess_attributes_from_name(f) results = self.CONSUMER._guess_attributes_from_name(f)
self.assertEqual(results[0].name, sender, f) self.assertEqual(results[0].name, sender, f)
self.assertEqual(results[1], title, f) self.assertEqual(results[1], title, f)
self.assertEqual(tuple([t.slug for t in results[2]]), tags, f) self.assertEqual(tuple([t.slug for t in results[2]]), tags, f)
self.assertEqual(results[3], suffix, f) if suffix.lower() == "jpeg":
self.assertEqual(results[3], "jpg", f)
else:
self.assertEqual(results[3], suffix.lower(), f)
def test_guess_attributes_from_name0(self): def test_guess_attributes_from_name0(self):
self._test_guess_attributes_from_name( self._test_guess_attributes_from_name(

View File

@ -13,7 +13,7 @@ from .models import Sender, Tag, Document
from .serialisers import SenderSerializer, TagSerializer, DocumentSerializer from .serialisers import SenderSerializer, TagSerializer, DocumentSerializer
class PdfView(DetailView): class FetchView(DetailView):
model = Document model = Document

View File

@ -21,7 +21,7 @@ from django.contrib import admin
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from documents.views import ( from documents.views import (
PdfView, PushView, SenderViewSet, TagViewSet, DocumentViewSet) FetchView, PushView, SenderViewSet, TagViewSet, DocumentViewSet)
router = DefaultRouter() router = DefaultRouter()
router.register(r'senders', SenderViewSet) router.register(r'senders', SenderViewSet)
@ -38,7 +38,7 @@ urlpatterns = [
url(r"^api/", include(router.urls, namespace="drf")), url(r"^api/", include(router.urls, namespace="drf")),
# File downloads # File downloads
url(r"^fetch/(?P<pk>\d+)$", PdfView.as_view(), name="fetch"), url(r"^fetch/(?P<pk>\d+)$", FetchView.as_view(), name="fetch"),
# The Django admin # The Django admin
url(r"", admin.site.urls), url(r"", admin.site.urls),