]> git.p6c8.net - pcdenotes.git/blob - notes/views.py
Changed <div id="content"> to <main>
[pcdenotes.git] / notes / views.py
1 from django.shortcuts import render, get_object_or_404, redirect
2 from django.core.paginator import Paginator
3 from django.db.models.functions import ExtractYear, ExtractMonth
4
5 from pcdenotes.settings import NOTES_PER_PAGE
6 from .models import Note
7
8 # Create your views here.
9
10 def note_list(request):
11 notes = Note.objects.all() if request.user.is_staff else Note.objects.filter(status=1)
12
13 notes_count = Note.objects.filter(status=1).count()
14 paginator = Paginator(notes, NOTES_PER_PAGE)
15
16 page_number = 1
17
18 try:
19 page_number = int(request.GET.get('page'))
20 except:
21 pass
22
23 page_count = paginator.num_pages
24
25 notes_page = paginator.get_page(page_number)
26
27 return render(request, 'note_list.html', {'notes_page': notes_page, 'notes_count': notes_count, 'pages': paginator, 'page_number': page_number, 'page_count': page_count})
28
29 def note_redirect(request):
30 return redirect('notes:note_list', permanent=True)
31
32 def note_detail(request, note_slug):
33 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)
34 return render(request, 'note_detail.html', {'note': note})
35
36 def archive_main(request):
37 notes_years = Note.objects.filter(status=1).annotate(created_year=ExtractYear('created_at')).values_list('created_year', flat=True).distinct().order_by('created_year')
38 return render(request, 'archive_main.html', {'years': notes_years})
39
40 def archive_year(request, archive_year):
41 notes_months = Note.objects.filter(status=1, created_at__year=archive_year).annotate(created_month=ExtractMonth('created_at')).values_list('created_month', flat=True).distinct().order_by('created_month')
42 return render(request, 'archive_year.html', {'year': archive_year, 'months': notes_months})
43
44 def archive_month(request, archive_year, archive_month):
45 notes = Note.objects.filter(status=1, created_at__year=archive_year, created_at__month=archive_month)
46 return render(request, 'archive_month.html', {'year': archive_year, 'month': archive_month, 'notes': notes})

patrick-canterino.de