views: add compatibility with URLs with embedded date
authorFrédéric Péters <fpeters@0d.be>
Sun, 29 Dec 2019 17:04:14 +0000 (18:04 +0100)
committerFrédéric Péters <fpeters@0d.be>
Mon, 30 Dec 2019 08:35:50 +0000 (09:35 +0100)
chloro/phyll/urls.py
chloro/phyll/views.py

index 99d6da6..1418810 100644 (file)
@@ -39,6 +39,7 @@ urlpatterns = [
         staff_member_required(views.NoteDeleteView.as_view(), login_url='login'),
     ),
     url(r'^new-note/$', staff_member_required(views.NoteAddView.as_view(), login_url='login')),
+    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w:-]+)/$', views.NoteView.as_view()),
     url(r'^(?P<slug>[\w:-]+)/$', views.NoteView.as_view()),
     url(r'^$', views.HomeView.as_view()),
 ]
index d366b83..5387b70 100644 (file)
@@ -14,6 +14,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from django.http import Http404
 from django.views.generic import CreateView, DeleteView, DetailView, UpdateView, TemplateView
 
 from .models import Note
@@ -22,6 +23,19 @@ from .models import Note
 class NoteView(DetailView):
     model = Note
 
+    def get(self, request, *args, **kwargs):
+        if kwargs.get('year'):
+            # check date does match
+            note = self.get_object()
+            creation = self.get_object().creation_timestamp
+            if (creation.year, creation.month, creation.day) != (
+                int(kwargs['year']),
+                int(kwargs['month']),
+                int(kwargs['day']),
+            ):
+                raise Http404()
+        return super(NoteView, self).get(request, *args, **kwargs)
+
 
 class NoteEditView(UpdateView):
     model = Note