views.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 BookList(generic.ListView):
  39. model = Book
  40. class CustomPaginator(Paginator):
  41. def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
  42. super(CustomPaginator, self).__init__(
  43. queryset,
  44. page_size,
  45. orphans=2,
  46. allow_empty_first_page=allow_empty_first_page)
  47. class AuthorListCustomPaginator(AuthorList):
  48. paginate_by = 5
  49. def get_paginator(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
  50. return super(AuthorListCustomPaginator, self).get_paginator(
  51. queryset,
  52. page_size,
  53. orphans=2,
  54. allow_empty_first_page=allow_empty_first_page)
  55. class ContactView(generic.FormView):
  56. form_class = ContactForm
  57. success_url = reverse_lazy('authors_list')
  58. template_name = 'generic_views/form.html'
  59. class ArtistCreate(generic.CreateView):
  60. model = Artist
  61. fields = '__all__'
  62. class NaiveAuthorCreate(generic.CreateView):
  63. queryset = Author.objects.all()
  64. fields = '__all__'
  65. class TemplateResponseWithoutTemplate(generic.detail.SingleObjectTemplateResponseMixin, generic.View):
  66. # we don't define the usual template_name here
  67. def __init__(self):
  68. # Dummy object, but attr is required by get_template_name()
  69. self.object = None
  70. class AuthorCreate(generic.CreateView):
  71. model = Author
  72. success_url = '/list/authors/'
  73. fields = '__all__'
  74. class SpecializedAuthorCreate(generic.CreateView):
  75. model = Author
  76. form_class = AuthorForm
  77. template_name = 'generic_views/form.html'
  78. context_object_name = 'thingy'
  79. def get_success_url(self):
  80. return reverse('author_detail', args=[self.object.id])
  81. class AuthorCreateRestricted(AuthorCreate):
  82. post = method_decorator(login_required)(AuthorCreate.post)
  83. class ArtistUpdate(generic.UpdateView):
  84. model = Artist
  85. fields = '__all__'
  86. class NaiveAuthorUpdate(generic.UpdateView):
  87. queryset = Author.objects.all()
  88. fields = '__all__'
  89. class AuthorUpdate(generic.UpdateView):
  90. model = Author
  91. success_url = '/list/authors/'
  92. fields = '__all__'
  93. class OneAuthorUpdate(generic.UpdateView):
  94. success_url = '/list/authors/'
  95. fields = '__all__'
  96. def get_object(self):
  97. return Author.objects.get(pk=1)
  98. class SpecializedAuthorUpdate(generic.UpdateView):
  99. model = Author
  100. form_class = AuthorForm
  101. template_name = 'generic_views/form.html'
  102. context_object_name = 'thingy'
  103. def get_success_url(self):
  104. return reverse('author_detail', args=[self.object.id])
  105. class NaiveAuthorDelete(generic.DeleteView):
  106. queryset = Author.objects.all()
  107. class AuthorDelete(generic.DeleteView):
  108. model = Author
  109. success_url = '/list/authors/'
  110. class SpecializedAuthorDelete(generic.DeleteView):
  111. queryset = Author.objects.all()
  112. template_name = 'generic_views/confirm_delete.html'
  113. context_object_name = 'thingy'
  114. def get_success_url(self):
  115. return reverse('authors_list')
  116. class BookConfig(object):
  117. queryset = Book.objects.all()
  118. date_field = 'pubdate'
  119. class BookArchive(BookConfig, generic.ArchiveIndexView):
  120. pass
  121. class BookYearArchive(BookConfig, generic.YearArchiveView):
  122. pass
  123. class BookMonthArchive(BookConfig, generic.MonthArchiveView):
  124. pass
  125. class BookWeekArchive(BookConfig, generic.WeekArchiveView):
  126. pass
  127. class BookDayArchive(BookConfig, generic.DayArchiveView):
  128. pass
  129. class BookTodayArchive(BookConfig, generic.TodayArchiveView):
  130. pass
  131. class BookDetail(BookConfig, generic.DateDetailView):
  132. pass
  133. class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
  134. fields = '__all__'
  135. def get_queryset(self):
  136. return Author.objects.all()
  137. class BookDetailGetObjectCustomQueryset(BookDetail):
  138. def get_object(self, queryset=None):
  139. return super(BookDetailGetObjectCustomQueryset, self).get_object(
  140. queryset=Book.objects.filter(pk=2))
  141. class CustomMultipleObjectMixinView(generic.list.MultipleObjectMixin, generic.View):
  142. queryset = [
  143. {'name': 'John'},
  144. {'name': 'Yoko'},
  145. ]
  146. def get(self, request):
  147. self.object_list = self.get_queryset()
  148. class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
  149. model = Book
  150. object = Book(name='dummy')
  151. def get_object(self):
  152. return Book(name="dummy")
  153. def get_context_data(self, **kwargs):
  154. context = {'custom_key': 'custom_value'}
  155. context.update(kwargs)
  156. return super(CustomContextView, self).get_context_data(**context)
  157. def get_context_object_name(self, obj):
  158. return "test_name"
  159. class CustomSingleObjectView(generic.detail.SingleObjectMixin, generic.View):
  160. model = Book
  161. object = Book(name="dummy")
  162. class BookSigningConfig(object):
  163. model = BookSigning
  164. date_field = 'event_date'
  165. # use the same templates as for books
  166. def get_template_names(self):
  167. return ['generic_views/book%s.html' % self.template_name_suffix]
  168. class BookSigningArchive(BookSigningConfig, generic.ArchiveIndexView):
  169. pass
  170. class BookSigningYearArchive(BookSigningConfig, generic.YearArchiveView):
  171. pass
  172. class BookSigningMonthArchive(BookSigningConfig, generic.MonthArchiveView):
  173. pass
  174. class BookSigningWeekArchive(BookSigningConfig, generic.WeekArchiveView):
  175. pass
  176. class BookSigningDayArchive(BookSigningConfig, generic.DayArchiveView):
  177. pass
  178. class BookSigningTodayArchive(BookSigningConfig, generic.TodayArchiveView):
  179. pass
  180. class BookSigningDetail(BookSigningConfig, generic.DateDetailView):
  181. context_object_name = 'book'
  182. class NonModel(object):
  183. id = "non_model_1"
  184. _meta = None
  185. class NonModelDetail(generic.DetailView):
  186. template_name = 'generic_views/detail.html'
  187. model = NonModel
  188. def get_object(self, queryset=None):
  189. return NonModel()
  190. class ObjectDoesNotExistDetail(generic.DetailView):
  191. def get_queryset(self):
  192. return Book.does_not_exist.all()