views.py 6.6 KB

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