From e1d82b5634014cf1ac69d97b5f25dbc277486f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Mart=C3=ADn?= Date: Thu, 7 Apr 2016 22:20:15 +0200 Subject: [PATCH] Added attachment URL shortcuts --- fmartingrcom/apps/blog/urls.py | 12 ++++++++++-- fmartingrcom/apps/blog/views.py | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/fmartingrcom/apps/blog/urls.py b/fmartingrcom/apps/blog/urls.py index d9b3d41..8c78cb7 100644 --- a/fmartingrcom/apps/blog/urls.py +++ b/fmartingrcom/apps/blog/urls.py @@ -1,6 +1,9 @@ from django.conf.urls import url -from .views import ListView, EntryView, EntryLiveEditView, SearchView, RSSView +from .views import ( + ListView, + EntryView, EntryAttachmentView, EntryLiveEditView, + SearchView, RSSView) urlpatterns = [ @@ -28,7 +31,12 @@ urlpatterns = [ EntryLiveEditView.as_view(), name='item-liveedit' ), - + # Attachment + url( + r'^(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[\w\-]+)/attachment/(?P.*)$', + EntryAttachmentView.as_view(), + name='item' + ), # RSS url( r'^rss\.xml$', diff --git a/fmartingrcom/apps/blog/views.py b/fmartingrcom/apps/blog/views.py index 56a4f6d..5c77555 100644 --- a/fmartingrcom/apps/blog/views.py +++ b/fmartingrcom/apps/blog/views.py @@ -39,7 +39,7 @@ class EntryView(View): section = 'blog' template = 'blog/entry.jinja' - def get(self, request, year, month, day, slug): + def get_object(self, year, month, day, slug): try: filters = { 'slug': slug, @@ -48,10 +48,12 @@ class EntryView(View): 'date__day': int(day), } - item = Entry.objects.get(**filters) + return Entry.objects.get(**filters) except Entry.DoesNotExist: raise Http404 + def get(self, request, year, month, day, slug): + item = self.get_object(year, month, day, slug) paginator, page = blog_utils.get_paginator(request, item=item) self.data['page'] = page @@ -61,6 +63,15 @@ class EntryView(View): return render(request, self.template, self.data) +class EntryAttachmentView(EntryView): + def get(self, request, year, month, day, slug, filename): + item = self.get_object(year, month, day, slug) + attachment = item.attachments.get(filename=filename) + if attachment: + return HttpResponseRedirect(attachment.url) + raise Http404 + + class EntryLiveEditView(View): @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs):