views.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 .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. class NaiveAuthorCreate(generic.CreateView):
  61. queryset = Author.objects.all()
  62. class AuthorCreate(generic.CreateView):
  63. model = Author
  64. success_url = '/list/authors/'
  65. class SpecializedAuthorCreate(generic.CreateView):
  66. model = Author
  67. form_class = AuthorForm
  68. template_name = 'generic_views/form.html'
  69. context_object_name = 'thingy'
  70. def get_success_url(self):
  71. return reverse('author_detail', args=[self.object.id,])
  72. class AuthorCreateRestricted(AuthorCreate):
  73. post = method_decorator(login_required)(AuthorCreate.post)
  74. class ArtistUpdate(generic.UpdateView):
  75. model = Artist
  76. class NaiveAuthorUpdate(generic.UpdateView):
  77. queryset = Author.objects.all()
  78. class AuthorUpdate(generic.UpdateView):
  79. model = Author
  80. success_url = '/list/authors/'
  81. class OneAuthorUpdate(generic.UpdateView):
  82. success_url = '/list/authors/'
  83. def get_object(self):
  84. return Author.objects.get(pk=1)
  85. class SpecializedAuthorUpdate(generic.UpdateView):
  86. model = Author
  87. form_class = AuthorForm
  88. template_name = 'generic_views/form.html'
  89. context_object_name = 'thingy'
  90. def get_success_url(self):
  91. return reverse('author_detail', args=[self.object.id,])
  92. class NaiveAuthorDelete(generic.DeleteView):
  93. queryset = Author.objects.all()
  94. class AuthorDelete(generic.DeleteView):
  95. model = Author
  96. success_url = '/list/authors/'
  97. class SpecializedAuthorDelete(generic.DeleteView):
  98. queryset = Author.objects.all()
  99. template_name = 'generic_views/confirm_delete.html'
  100. context_object_name = 'thingy'
  101. def get_success_url(self):
  102. return reverse('authors_list')
  103. class BookConfig(object):
  104. queryset = Book.objects.all()
  105. date_field = 'pubdate'
  106. class BookArchive(BookConfig, generic.ArchiveIndexView):
  107. pass
  108. class BookYearArchive(BookConfig, generic.YearArchiveView):
  109. pass
  110. class BookMonthArchive(BookConfig, generic.MonthArchiveView):
  111. pass
  112. class BookWeekArchive(BookConfig, generic.WeekArchiveView):
  113. pass
  114. class BookDayArchive(BookConfig, generic.DayArchiveView):
  115. pass
  116. class BookTodayArchive(BookConfig, generic.TodayArchiveView):
  117. pass
  118. class BookDetail(BookConfig, generic.DateDetailView):
  119. pass
  120. class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
  121. def get_queryset(self):
  122. return Author.objects.all()
  123. class BookDetailGetObjectCustomQueryset(BookDetail):
  124. def get_object(self, queryset=None):
  125. return super(BookDetailGetObjectCustomQueryset,self).get_object(
  126. queryset=Book.objects.filter(pk=2))
  127. class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
  128. model = Book
  129. object = Book(name='dummy')
  130. def get_object(self):
  131. return Book(name="dummy")
  132. def get_context_data(self, **kwargs):
  133. context = {'custom_key': 'custom_value'}
  134. context.update(kwargs)
  135. return super(CustomContextView, self).get_context_data(**context)
  136. def get_context_object_name(self, obj):
  137. return "test_name"
  138. class BookSigningConfig(object):
  139. model = BookSigning
  140. date_field = 'event_date'
  141. # use the same templates as for books
  142. def get_template_names(self):
  143. return ['generic_views/book%s.html' % self.template_name_suffix]
  144. class BookSigningArchive(BookSigningConfig, generic.ArchiveIndexView):
  145. pass
  146. class BookSigningYearArchive(BookSigningConfig, generic.YearArchiveView):
  147. pass
  148. class BookSigningMonthArchive(BookSigningConfig, generic.MonthArchiveView):
  149. pass
  150. class BookSigningWeekArchive(BookSigningConfig, generic.WeekArchiveView):
  151. pass
  152. class BookSigningDayArchive(BookSigningConfig, generic.DayArchiveView):
  153. pass
  154. class BookSigningTodayArchive(BookSigningConfig, generic.TodayArchiveView):
  155. pass
  156. class BookSigningDetail(BookSigningConfig, generic.DateDetailView):
  157. context_object_name = 'book'
  158. class NonModel(object):
  159. id = "non_model_1"
  160. _meta = None
  161. class NonModelDetail(generic.DetailView):
  162. template_name = 'generic_views/detail.html'
  163. model = NonModel
  164. def get_object(self, queryset=None):
  165. return NonModel()