1
from __future__ import absolute_import
3
from django.contrib.auth.decorators import login_required
4
from django.core.paginator import Paginator
5
from django.core.urlresolvers import reverse, reverse_lazy
6
from django.utils.decorators import method_decorator
7
from django.views import generic
9
from .forms import AuthorForm, ContactForm
10
from .models import Artist, Author, Book, Page, BookSigning
13
class CustomTemplateView(generic.TemplateView):
14
template_name = 'generic_views/about.html'
16
def get_context_data(self, **kwargs):
17
context = super(CustomTemplateView, self).get_context_data(**kwargs)
18
context.update({'key': 'value'})
22
class ObjectDetail(generic.DetailView):
23
template_name = 'generic_views/detail.html'
29
class ArtistDetail(generic.DetailView):
30
queryset = Artist.objects.all()
33
class AuthorDetail(generic.DetailView):
34
queryset = Author.objects.all()
37
class PageDetail(generic.DetailView):
38
queryset = Page.objects.all()
39
template_name_field = 'template'
42
class DictList(generic.ListView):
43
"""A ListView that doesn't use a model."""
45
{'first': 'John', 'last': 'Lennon'},
46
{'first': 'Yoko', 'last': 'Ono'}
48
template_name = 'generic_views/list.html'
51
class ArtistList(generic.ListView):
52
template_name = 'generic_views/list.html'
53
queryset = Artist.objects.all()
56
class AuthorList(generic.ListView):
57
queryset = Author.objects.all()
60
class CustomPaginator(Paginator):
61
def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
62
super(CustomPaginator, self).__init__(
66
allow_empty_first_page=allow_empty_first_page)
68
class AuthorListCustomPaginator(AuthorList):
71
def get_paginator(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
72
return super(AuthorListCustomPaginator, self).get_paginator(
76
allow_empty_first_page=allow_empty_first_page)
79
class ContactView(generic.FormView):
80
form_class = ContactForm
81
success_url = reverse_lazy('authors_list')
82
template_name = 'generic_views/form.html'
85
class ArtistCreate(generic.CreateView):
89
class NaiveAuthorCreate(generic.CreateView):
90
queryset = Author.objects.all()
93
class AuthorCreate(generic.CreateView):
95
success_url = '/list/authors/'
98
class SpecializedAuthorCreate(generic.CreateView):
100
form_class = AuthorForm
101
template_name = 'generic_views/form.html'
102
context_object_name = 'thingy'
104
def get_success_url(self):
105
return reverse('author_detail', args=[self.object.id,])
108
class AuthorCreateRestricted(AuthorCreate):
109
post = method_decorator(login_required)(AuthorCreate.post)
112
class ArtistUpdate(generic.UpdateView):
116
class NaiveAuthorUpdate(generic.UpdateView):
117
queryset = Author.objects.all()
120
class AuthorUpdate(generic.UpdateView):
122
success_url = '/list/authors/'
125
class OneAuthorUpdate(generic.UpdateView):
126
success_url = '/list/authors/'
128
def get_object(self):
129
return Author.objects.get(pk=1)
132
class SpecializedAuthorUpdate(generic.UpdateView):
134
form_class = AuthorForm
135
template_name = 'generic_views/form.html'
136
context_object_name = 'thingy'
138
def get_success_url(self):
139
return reverse('author_detail', args=[self.object.id,])
142
class NaiveAuthorDelete(generic.DeleteView):
143
queryset = Author.objects.all()
146
class AuthorDelete(generic.DeleteView):
148
success_url = '/list/authors/'
151
class SpecializedAuthorDelete(generic.DeleteView):
152
queryset = Author.objects.all()
153
template_name = 'generic_views/confirm_delete.html'
154
context_object_name = 'thingy'
156
def get_success_url(self):
157
return reverse('authors_list')
160
class BookConfig(object):
161
queryset = Book.objects.all()
162
date_field = 'pubdate'
164
class BookArchive(BookConfig, generic.ArchiveIndexView):
167
class BookYearArchive(BookConfig, generic.YearArchiveView):
170
class BookMonthArchive(BookConfig, generic.MonthArchiveView):
173
class BookWeekArchive(BookConfig, generic.WeekArchiveView):
176
class BookDayArchive(BookConfig, generic.DayArchiveView):
179
class BookTodayArchive(BookConfig, generic.TodayArchiveView):
182
class BookDetail(BookConfig, generic.DateDetailView):
185
class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
186
def get_queryset(self):
187
return Author.objects.all()
189
class BookDetailGetObjectCustomQueryset(BookDetail):
190
def get_object(self, queryset=None):
191
return super(BookDetailGetObjectCustomQueryset,self).get_object(
192
queryset=Book.objects.filter(pk=2))
194
class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
196
object = Book(name='dummy')
198
def get_object(self):
199
return Book(name="dummy")
201
def get_context_data(self, **kwargs):
202
context = {'custom_key': 'custom_value'}
203
context.update(kwargs)
204
return super(CustomContextView, self).get_context_data(**context)
206
def get_context_object_name(self, obj):
209
class BookSigningConfig(object):
211
date_field = 'event_date'
212
# use the same templates as for books
213
def get_template_names(self):
214
return ['generic_views/book%s.html' % self.template_name_suffix]
216
class BookSigningArchive(BookSigningConfig, generic.ArchiveIndexView):
219
class BookSigningYearArchive(BookSigningConfig, generic.YearArchiveView):
222
class BookSigningMonthArchive(BookSigningConfig, generic.MonthArchiveView):
225
class BookSigningWeekArchive(BookSigningConfig, generic.WeekArchiveView):
228
class BookSigningDayArchive(BookSigningConfig, generic.DayArchiveView):
231
class BookSigningTodayArchive(BookSigningConfig, generic.TodayArchiveView):
234
class BookSigningDetail(BookSigningConfig, generic.DateDetailView):
235
context_object_name = 'book'
238
class NonModel(object):
244
class NonModelDetail(generic.DetailView):
246
template_name = 'generic_views/detail.html'
249
def get_object(self, queryset=None):