views.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. from __future__ import unicode_literals
  2. from django.contrib.auth.decorators import login_required
  3. from django.core.paginator import Paginator
  4. from django.core.urlresolvers import reverse, reverse_lazy
  5. from django.utils.decorators import method_decorator
  6. from django.views import generic
  7. from .test_forms import AuthorForm, ContactForm
  8. from .models import Artist, Author, Book, Page, BookSigning
  9. class CustomTemplateView(generic.TemplateView):
  10. template_name = 'generic_views/about.html'
  11. def get_context_data(self, **kwargs):
  12. context = super(CustomTemplateView, self).get_context_data(**kwargs)
  13. context.update({'key': 'value'})
  14. return context
  15. class ObjectDetail(generic.DetailView):
  16. template_name = 'generic_views/detail.html'
  17. def get_object(self):
  18. return {'foo': 'bar'}
  19. class ArtistDetail(generic.DetailView):
  20. queryset = Artist.objects.all()
  21. class AuthorDetail(generic.DetailView):
  22. queryset = Author.objects.all()
  23. class PageDetail(generic.DetailView):
  24. queryset = Page.objects.all()
  25. template_name_field = 'template'
  26. class DictList(generic.ListView):
  27. """A ListView that doesn't use a model."""
  28. queryset = [
  29. {'first': 'John', 'last': 'Lennon'},
  30. {'first': 'Yoko', 'last': 'Ono'}
  31. ]
  32. template_name = 'generic_views/list.html'
  33. class ArtistList(generic.ListView):
  34. template_name = 'generic_views/list.html'
  35. queryset = Artist.objects.all()
  36. class AuthorList(generic.ListView):
  37. queryset = Author.objects.all()
  38. class CustomPaginator(Paginator):
  39. def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
  40. super(CustomPaginator, self).__init__(
  41. queryset,
  42. page_size,
  43. orphans=2,
  44. allow_empty_first_page=allow_empty_first_page)
  45. class AuthorListCustomPaginator(AuthorList):
  46. paginate_by = 5
  47. def get_paginator(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
  48. return super(AuthorListCustomPaginator, self).get_paginator(
  49. queryset,
  50. page_size,
  51. orphans=2,
  52. allow_empty_first_page=allow_empty_first_page)
  53. class ContactView(generic.FormView):
  54. form_class = ContactForm
  55. success_url = reverse_lazy('authors_list')
  56. template_name = 'generic_views/form.html'
  57. class ArtistCreate(generic.CreateView):
  58. model = Artist
  59. fields = '__all__'
  60. class NaiveAuthorCreate(generic.CreateView):
  61. queryset = Author.objects.all()
  62. fields = '__all__'
  63. class TemplateResponseWithoutTemplate(generic.detail.SingleObjectTemplateResponseMixin, generic.View):
  64. # we don't define the usual template_name here
  65. def __init__(self):
  66. # Dummy object, but attr is required by get_template_name()
  67. self.object = None
  68. class AuthorCreate(generic.CreateView):
  69. model = Author
  70. success_url = '/list/authors/'
  71. fields = '__all__'
  72. class SpecializedAuthorCreate(generic.CreateView):
  73. model = Author
  74. form_class = AuthorForm
  75. template_name = 'generic_views/form.html'
  76. context_object_name = 'thingy'
  77. def get_success_url(self):
  78. return reverse('author_detail', args=[self.object.id])
  79. class AuthorCreateRestricted(AuthorCreate):
  80. post = method_decorator(login_required)(AuthorCreate.post)
  81. class ArtistUpdate(generic.UpdateView):
  82. model = Artist
  83. fields = '__all__'
  84. class NaiveAuthorUpdate(generic.UpdateView):
  85. queryset = Author.objects.all()
  86. fields = '__all__'
  87. class AuthorUpdate(generic.UpdateView):
  88. model = Author
  89. success_url = '/list/authors/'
  90. fields = '__all__'
  91. class OneAuthorUpdate(generic.UpdateView):
  92. success_url = '/list/authors/'
  93. fields = '__all__'
  94. def get_object(self):
  95. return Author.objects.get(pk=1)
  96. class SpecializedAuthorUpdate(generic.UpdateView):
  97. model = Author
  98. form_class = AuthorForm
  99. template_name = 'generic_views/form.html'
  100. context_object_name = 'thingy'
  101. def get_success_url(self):
  102. return reverse('author_detail', args=[self.object.id])
  103. class NaiveAuthorDelete(generic.DeleteView):
  104. queryset = Author.objects.all()
  105. class AuthorDelete(generic.DeleteView):
  106. model = Author
  107. success_url = '/list/authors/'
  108. class SpecializedAuthorDelete(generic.DeleteView):
  109. queryset = Author.objects.all()
  110. template_name = 'generic_views/confirm_delete.html'
  111. context_object_name = 'thingy'
  112. def get_success_url(self):
  113. return reverse('authors_list')
  114. class BookConfig(object):
  115. queryset = Book.objects.all()
  116. date_field = 'pubdate'
  117. class BookArchive(BookConfig, generic.ArchiveIndexView):
  118. pass
  119. class BookYearArchive(BookConfig, generic.YearArchiveView):
  120. pass
  121. class BookMonthArchive(BookConfig, generic.MonthArchiveView):
  122. pass
  123. class BookWeekArchive(BookConfig, generic.WeekArchiveView):
  124. pass
  125. class BookDayArchive(BookConfig, generic.DayArchiveView):
  126. pass
  127. class BookTodayArchive(BookConfig, generic.TodayArchiveView):
  128. pass
  129. class BookDetail(BookConfig, generic.DateDetailView):
  130. pass
  131. class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
  132. fields = '__all__'
  133. def get_queryset(self):
  134. return Author.objects.all()
  135. class BookDetailGetObjectCustomQueryset(BookDetail):
  136. def get_object(self, queryset=None):
  137. return super(BookDetailGetObjectCustomQueryset, self).get_object(
  138. queryset=Book.objects.filter(pk=2))
  139. class CustomMultipleObjectMixinView(generic.list.MultipleObjectMixin, generic.View):
  140. queryset = [
  141. {'name': 'John'},
  142. {'name': 'Yoko'},
  143. ]
  144. def get(self, request):
  145. self.object_list = self.get_queryset()
  146. class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
  147. model = Book
  148. object = Book(name='dummy')
  149. def get_object(self):
  150. return Book(name="dummy")
  151. def get_context_data(self, **kwargs):
  152. context = {'custom_key': 'custom_value'}
  153. context.update(kwargs)
  154. return super(CustomContextView, self).get_context_data(**context)
  155. def get_context_object_name(self, obj):
  156. return "test_name"
  157. class CustomSingleObjectView(generic.detail.SingleObjectMixin, generic.View):
  158. model = Book
  159. object = Book(name="dummy")
  160. class BookSigningConfig(object):
  161. model = BookSigning
  162. date_field = 'event_date'
  163. # use the same templates as for books
  164. def get_template_names(self):
  165. return ['generic_views/book%s.html' % self.template_name_suffix]
  166. class BookSigningArchive(BookSigningConfig, generic.ArchiveIndexView):
  167. pass
  168. class BookSigningYearArchive(BookSigningConfig, generic.YearArchiveView):
  169. pass
  170. class BookSigningMonthArchive(BookSigningConfig, generic.MonthArchiveView):
  171. pass
  172. class BookSigningWeekArchive(BookSigningConfig, generic.WeekArchiveView):
  173. pass
  174. class BookSigningDayArchive(BookSigningConfig, generic.DayArchiveView):
  175. pass
  176. class BookSigningTodayArchive(BookSigningConfig, generic.TodayArchiveView):
  177. pass
  178. class BookSigningDetail(BookSigningConfig, generic.DateDetailView):
  179. context_object_name = 'book'
  180. class NonModel(object):
  181. id = "non_model_1"
  182. _meta = None
  183. class NonModelDetail(generic.DetailView):
  184. template_name = 'generic_views/detail.html'
  185. model = NonModel
  186. def get_object(self, queryset=None):
  187. return NonModel()