]>
git.p6c8.net - pcdenotes.git/blob - notes/views.py
d2c8bb00334db7baabefbc3861802dd3616c66dc
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
5 from pcdenotes
.settings
import NOTES_PER_PAGE
6 from .models
import Note
8 # Create your views here.
10 def note_list(request
):
11 notes
= Note
.objects
.all() if request
.user
.is_staff
else Note
.objects
.filter(status
=1)
13 notes_count
= Note
.objects
.filter(status
=1).count()
14 paginator
= Paginator(notes
, NOTES_PER_PAGE
)
19 page_number
= int(request
.GET
.get('page'))
23 page_count
= paginator
.num_pages
25 notes_page
= paginator
.get_page(page_number
)
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
})
29 def note_redirect(request
):
30 return redirect('notes:note_list', permanent
=True)
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
})
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
})
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
})
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