2010-10-18 13:34:47 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2010-12-05 04:32:36 +00:00
|
|
|
from django.core.paginator import Paginator
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2010-10-18 13:34:47 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import generic
|
|
|
|
|
2015-11-07 15:47:36 +00:00
|
|
|
from .forms import AuthorForm, ContactForm
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Artist, Author, Book, BookSigning, Page
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomTemplateView(generic.TemplateView):
|
|
|
|
template_name = 'generic_views/about.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2012-04-06 21:24:33 +00:00
|
|
|
context.update({'key': 'value'})
|
|
|
|
return context
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectDetail(generic.DetailView):
|
|
|
|
template_name = 'generic_views/detail.html'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return {'foo': 'bar'}
|
|
|
|
|
|
|
|
|
|
|
|
class ArtistDetail(generic.DetailView):
|
|
|
|
queryset = Artist.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class AuthorDetail(generic.DetailView):
|
|
|
|
queryset = Author.objects.all()
|
|
|
|
|
|
|
|
|
2015-12-29 18:58:43 +00:00
|
|
|
class AuthorCustomDetail(generic.DetailView):
|
|
|
|
template_name = 'generic_views/author_detail.html'
|
|
|
|
queryset = Author.objects.all()
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
# Ensures get_context_object_name() doesn't reference self.object.
|
|
|
|
author = self.get_object()
|
|
|
|
context = {'custom_' + self.get_context_object_name(author): author}
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class PageDetail(generic.DetailView):
|
|
|
|
queryset = Page.objects.all()
|
|
|
|
template_name_field = 'template'
|
|
|
|
|
|
|
|
|
|
|
|
class DictList(generic.ListView):
|
|
|
|
"""A ListView that doesn't use a model."""
|
|
|
|
queryset = [
|
|
|
|
{'first': 'John', 'last': 'Lennon'},
|
2013-11-02 19:37:48 +00:00
|
|
|
{'first': 'Yoko', 'last': 'Ono'}
|
2010-10-18 13:34:47 +00:00
|
|
|
]
|
|
|
|
template_name = 'generic_views/list.html'
|
|
|
|
|
|
|
|
|
2011-01-03 13:15:58 +00:00
|
|
|
class ArtistList(generic.ListView):
|
|
|
|
template_name = 'generic_views/list.html'
|
|
|
|
queryset = Artist.objects.all()
|
|
|
|
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class AuthorList(generic.ListView):
|
|
|
|
queryset = Author.objects.all()
|
|
|
|
|
|
|
|
|
2017-10-17 13:22:45 +00:00
|
|
|
class AuthorListGetQuerysetReturnsNone(AuthorList):
|
|
|
|
def get_queryset(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-05-19 21:27:09 +00:00
|
|
|
class BookList(generic.ListView):
|
|
|
|
model = Book
|
|
|
|
|
|
|
|
|
2010-12-05 04:32:36 +00:00
|
|
|
class CustomPaginator(Paginator):
|
|
|
|
def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(queryset, page_size, orphans=2, allow_empty_first_page=allow_empty_first_page)
|
2010-12-05 04:32:36 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-12-05 04:32:36 +00:00
|
|
|
class AuthorListCustomPaginator(AuthorList):
|
2011-06-10 10:18:06 +00:00
|
|
|
paginate_by = 5
|
2010-12-05 04:32:36 +00:00
|
|
|
|
|
|
|
def get_paginator(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().get_paginator(queryset, page_size, orphans=2, allow_empty_first_page=allow_empty_first_page)
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2012-12-04 12:18:57 +00:00
|
|
|
|
|
|
|
class ContactView(generic.FormView):
|
|
|
|
form_class = ContactForm
|
|
|
|
success_url = reverse_lazy('authors_list')
|
|
|
|
template_name = 'generic_views/form.html'
|
|
|
|
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class ArtistCreate(generic.CreateView):
|
|
|
|
model = Artist
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NaiveAuthorCreate(generic.CreateView):
|
|
|
|
queryset = Author.objects.all()
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
2013-09-06 21:25:34 +00:00
|
|
|
class TemplateResponseWithoutTemplate(generic.detail.SingleObjectTemplateResponseMixin, generic.View):
|
|
|
|
# we don't define the usual template_name here
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# Dummy object, but attr is required by get_template_name()
|
|
|
|
self.object = None
|
|
|
|
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class AuthorCreate(generic.CreateView):
|
|
|
|
model = Author
|
|
|
|
success_url = '/list/authors/'
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SpecializedAuthorCreate(generic.CreateView):
|
|
|
|
model = Author
|
|
|
|
form_class = AuthorForm
|
|
|
|
template_name = 'generic_views/form.html'
|
|
|
|
context_object_name = 'thingy'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2013-10-26 19:15:03 +00:00
|
|
|
return reverse('author_detail', args=[self.object.id])
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthorCreateRestricted(AuthorCreate):
|
|
|
|
post = method_decorator(login_required)(AuthorCreate.post)
|
|
|
|
|
|
|
|
|
|
|
|
class ArtistUpdate(generic.UpdateView):
|
|
|
|
model = Artist
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NaiveAuthorUpdate(generic.UpdateView):
|
|
|
|
queryset = Author.objects.all()
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthorUpdate(generic.UpdateView):
|
2015-12-30 21:22:58 +00:00
|
|
|
get_form_called_count = 0 # Used to ensure get_form() is called once.
|
2010-10-18 13:34:47 +00:00
|
|
|
model = Author
|
|
|
|
success_url = '/list/authors/'
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2015-12-30 21:22:58 +00:00
|
|
|
def get_form(self, *args, **kwargs):
|
|
|
|
self.get_form_called_count += 1
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().get_form(*args, **kwargs)
|
2015-12-30 21:22:58 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2011-02-15 08:12:29 +00:00
|
|
|
class OneAuthorUpdate(generic.UpdateView):
|
|
|
|
success_url = '/list/authors/'
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
2011-02-15 08:12:29 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return Author.objects.get(pk=1)
|
|
|
|
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class SpecializedAuthorUpdate(generic.UpdateView):
|
|
|
|
model = Author
|
|
|
|
form_class = AuthorForm
|
|
|
|
template_name = 'generic_views/form.html'
|
|
|
|
context_object_name = 'thingy'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2013-10-26 19:15:03 +00:00
|
|
|
return reverse('author_detail', args=[self.object.id])
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NaiveAuthorDelete(generic.DeleteView):
|
|
|
|
queryset = Author.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class AuthorDelete(generic.DeleteView):
|
|
|
|
model = Author
|
|
|
|
success_url = '/list/authors/'
|
|
|
|
|
|
|
|
|
|
|
|
class SpecializedAuthorDelete(generic.DeleteView):
|
|
|
|
queryset = Author.objects.all()
|
|
|
|
template_name = 'generic_views/confirm_delete.html'
|
|
|
|
context_object_name = 'thingy'
|
2015-01-26 10:09:50 +00:00
|
|
|
success_url = reverse_lazy('authors_list')
|
2010-10-18 13:34:47 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class BookConfig:
|
2010-10-18 13:34:47 +00:00
|
|
|
queryset = Book.objects.all()
|
|
|
|
date_field = 'pubdate'
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookArchive(BookConfig, generic.ArchiveIndexView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookYearArchive(BookConfig, generic.YearArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookMonthArchive(BookConfig, generic.MonthArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookWeekArchive(BookConfig, generic.WeekArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookDayArchive(BookConfig, generic.DayArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookTodayArchive(BookConfig, generic.TodayArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
class BookDetail(BookConfig, generic.DateDetailView):
|
|
|
|
pass
|
2010-12-04 11:20:52 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-12-04 11:20:52 +00:00
|
|
|
class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
|
2013-02-21 21:56:55 +00:00
|
|
|
fields = '__all__'
|
|
|
|
|
2010-12-04 11:20:52 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return Author.objects.all()
|
2011-10-13 13:38:38 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2011-10-13 13:38:38 +00:00
|
|
|
class BookDetailGetObjectCustomQueryset(BookDetail):
|
|
|
|
def get_object(self, queryset=None):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().get_object(queryset=Book.objects.filter(pk=self.kwargs['pk']))
|
2012-04-06 21:24:33 +00:00
|
|
|
|
2013-04-10 10:27:28 +00:00
|
|
|
|
|
|
|
class CustomMultipleObjectMixinView(generic.list.MultipleObjectMixin, generic.View):
|
|
|
|
queryset = [
|
|
|
|
{'name': 'John'},
|
|
|
|
{'name': 'Yoko'},
|
|
|
|
]
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
self.object_list = self.get_queryset()
|
|
|
|
|
|
|
|
|
2012-04-06 21:24:33 +00:00
|
|
|
class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
|
|
|
|
model = Book
|
|
|
|
object = Book(name='dummy')
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return Book(name="dummy")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {'custom_key': 'custom_value'}
|
|
|
|
context.update(kwargs)
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().get_context_data(**context)
|
2012-04-06 21:24:33 +00:00
|
|
|
|
|
|
|
def get_context_object_name(self, obj):
|
|
|
|
return "test_name"
|
2012-04-30 18:41:38 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-05-18 10:42:18 +00:00
|
|
|
class CustomSingleObjectView(generic.detail.SingleObjectMixin, generic.View):
|
|
|
|
model = Book
|
|
|
|
object = Book(name="dummy")
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class BookSigningConfig:
|
2012-04-30 18:41:38 +00:00
|
|
|
model = BookSigning
|
|
|
|
date_field = 'event_date'
|
|
|
|
# use the same templates as for books
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
def get_template_names(self):
|
|
|
|
return ['generic_views/book%s.html' % self.template_name_suffix]
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningArchive(BookSigningConfig, generic.ArchiveIndexView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningYearArchive(BookSigningConfig, generic.YearArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningMonthArchive(BookSigningConfig, generic.MonthArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningWeekArchive(BookSigningConfig, generic.WeekArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningDayArchive(BookSigningConfig, generic.DayArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningTodayArchive(BookSigningConfig, generic.TodayArchiveView):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2018-10-30 11:44:25 +00:00
|
|
|
class BookArchiveWithoutDateField(generic.ArchiveIndexView):
|
|
|
|
queryset = Book.objects.all()
|
|
|
|
|
|
|
|
|
2012-04-30 18:41:38 +00:00
|
|
|
class BookSigningDetail(BookSigningConfig, generic.DateDetailView):
|
|
|
|
context_object_name = 'book'
|
2012-06-08 22:12:14 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class NonModel:
|
2012-06-08 22:12:14 +00:00
|
|
|
id = "non_model_1"
|
|
|
|
|
|
|
|
_meta = None
|
|
|
|
|
|
|
|
|
|
|
|
class NonModelDetail(generic.DetailView):
|
|
|
|
|
|
|
|
template_name = 'generic_views/detail.html'
|
|
|
|
model = NonModel
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
return NonModel()
|
2013-12-14 23:15:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectDoesNotExistDetail(generic.DetailView):
|
|
|
|
def get_queryset(self):
|
|
|
|
return Book.does_not_exist.all()
|
2015-11-10 13:06:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LateValidationView(generic.FormView):
|
|
|
|
form_class = ContactForm
|
|
|
|
success_url = reverse_lazy('authors_list')
|
|
|
|
template_name = 'generic_views/form.html'
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.add_error(None, 'There is an error')
|
|
|
|
return self.form_invalid(form)
|