From 96213c617d77f89ec12e8e6349b60dd6c5bbef1d Mon Sep 17 00:00:00 2001 From: Patrick Canterino Date: Sun, 30 Jan 2022 14:08:31 +0100 Subject: [PATCH] Began to implement archive view You can already list the years --- .gitignore | 2 +- notes/templates/archive_main.html | 17 +++++++++++++++++ notes/templates/archive_month.html | 9 +++++++++ notes/templates/archive_year.html | 9 +++++++++ notes/urls.py | 3 +++ notes/views.py | 13 ++++++++++++- 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 notes/templates/archive_main.html create mode 100644 notes/templates/archive_month.html create mode 100644 notes/templates/archive_year.html diff --git a/.gitignore b/.gitignore index fcc7e9b..7941172 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.env +.env* .venv __pycache__ db.sqlite3 diff --git a/notes/templates/archive_main.html b/notes/templates/archive_main.html new file mode 100644 index 0000000..268e950 --- /dev/null +++ b/notes/templates/archive_main.html @@ -0,0 +1,17 @@ +{% extends "note_base.html" %} + +{% block title %}Archive – Notes{% endblock %} + +{% block content %} +

Archive

+ +{% if years %} + +{% else %} +

No notes

+{% endif %} +{% endblock %} \ No newline at end of file diff --git a/notes/templates/archive_month.html b/notes/templates/archive_month.html new file mode 100644 index 0000000..ced8896 --- /dev/null +++ b/notes/templates/archive_month.html @@ -0,0 +1,9 @@ +{% extends "note_base.html" %} + +{% block title %}Archive {{ month }} / {{ year }} – Notes{% endblock %} + +{% block content %} +

Archive {{ month }} / {{ year }}

+ + +{% endblock %} \ No newline at end of file diff --git a/notes/templates/archive_year.html b/notes/templates/archive_year.html new file mode 100644 index 0000000..ec64328 --- /dev/null +++ b/notes/templates/archive_year.html @@ -0,0 +1,9 @@ +{% extends "note_base.html" %} + +{% block title %}Archive {{ year }} – Notes{% endblock %} + +{% block content %} +

Archive {{ year }}

+ + +{% endblock %} \ No newline at end of file diff --git a/notes/urls.py b/notes/urls.py index 560e8f5..9a20053 100644 --- a/notes/urls.py +++ b/notes/urls.py @@ -7,4 +7,7 @@ urlpatterns=[ path('', views.note_list, name='note_list'), re_path('^notes/?$', views.note_redirect, name='note_redirect'), path('notes/', views.note_detail, name='note_detail'), + path('archive/', views.archive_main, name='archive_main'), + path('archive//', views.archive_year, name='archive_year'), + path('archive///', views.archive_month, name='archive_month'), ] \ No newline at end of file diff --git a/notes/views.py b/notes/views.py index bd2fd82..18946bf 100644 --- a/notes/views.py +++ b/notes/views.py @@ -1,5 +1,6 @@ from django.shortcuts import render, get_object_or_404, redirect from django.core.paginator import Paginator +from django.db.models.functions import ExtractYear from pcdenotes.settings import NOTES_PER_PAGE from .models import Note @@ -30,4 +31,14 @@ def note_redirect(request): def note_detail(request, note_slug): note = get_object_or_404(Note, slug=note_slug) if request.user.is_staff else get_object_or_404(Note, slug=note_slug, status=1) - return render(request, 'note_detail.html', {'note': note}) \ No newline at end of file + return render(request, 'note_detail.html', {'note': note}) + +def archive_main(request): + notes_years = Note.objects.all().annotate(created_year=ExtractYear('created_at')).values_list('created_year', flat=True).distinct().order_by('created_year') + return render(request, 'archive_main.html', {'years': notes_years}) + +def archive_year(request, archive_year): + return render(request, 'archive_year.html', {'year': archive_year}) + +def archive_month(request, archive_year, archive_month): + return render(request, 'archive_month.html', {'year': archive_year, 'month': archive_month}) \ No newline at end of file -- 2.34.1