PageRenderTime 256ms CodeModel.GetById 221ms app.highlight 14ms RepoModel.GetById 17ms app.codeStats 0ms

/tests/regressiontests/generic_views/views.py

https://code.google.com/p/mango-py/
Python | 179 lines | 164 code | 15 blank | 0 comment | 0 complexity | 1c364110554234ef5d6f96b2bb032964 MD5 | raw file
  1from django.contrib.auth.decorators import login_required
  2from django.core.paginator import Paginator
  3from django.core.urlresolvers import reverse
  4from django.utils.decorators import method_decorator
  5from django.views import generic
  6
  7from regressiontests.generic_views.models import Artist, Author, Book, Page
  8from regressiontests.generic_views.forms import AuthorForm
  9
 10
 11class CustomTemplateView(generic.TemplateView):
 12    template_name = 'generic_views/about.html'
 13
 14    def get_context_data(self, **kwargs):
 15        return {
 16            'params': kwargs,
 17            'key': 'value'
 18        }
 19
 20
 21class ObjectDetail(generic.DetailView):
 22    template_name = 'generic_views/detail.html'
 23
 24    def get_object(self):
 25        return {'foo': 'bar'}
 26
 27
 28class ArtistDetail(generic.DetailView):
 29    queryset = Artist.objects.all()
 30
 31
 32class AuthorDetail(generic.DetailView):
 33    queryset = Author.objects.all()
 34
 35
 36class PageDetail(generic.DetailView):
 37    queryset = Page.objects.all()
 38    template_name_field = 'template'
 39
 40
 41class DictList(generic.ListView):
 42    """A ListView that doesn't use a model."""
 43    queryset = [
 44        {'first': 'John', 'last': 'Lennon'},
 45        {'last': 'Yoko',  'last': 'Ono'}
 46    ]
 47    template_name = 'generic_views/list.html'
 48
 49
 50class ArtistList(generic.ListView):
 51    template_name = 'generic_views/list.html'
 52    queryset = Artist.objects.all()
 53
 54
 55class AuthorList(generic.ListView):
 56    queryset = Author.objects.all()
 57
 58
 59class CustomPaginator(Paginator):
 60    def __init__(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
 61        super(CustomPaginator, self).__init__(
 62            queryset,
 63            page_size,
 64            orphans=2,
 65            allow_empty_first_page=allow_empty_first_page)
 66
 67class AuthorListCustomPaginator(AuthorList):
 68    paginate_by = 5;
 69
 70    def get_paginator(self, queryset, page_size, orphans=0, allow_empty_first_page=True):
 71        return super(AuthorListCustomPaginator, self).get_paginator(
 72            queryset,
 73            page_size,
 74            orphans=2,
 75            allow_empty_first_page=allow_empty_first_page)
 76
 77class ArtistCreate(generic.CreateView):
 78    model = Artist
 79
 80
 81class NaiveAuthorCreate(generic.CreateView):
 82    queryset = Author.objects.all()
 83
 84
 85class AuthorCreate(generic.CreateView):
 86    model = Author
 87    success_url = '/list/authors/'
 88
 89
 90class SpecializedAuthorCreate(generic.CreateView):
 91    model = Author
 92    form_class = AuthorForm
 93    template_name = 'generic_views/form.html'
 94    context_object_name = 'thingy'
 95
 96    def get_success_url(self):
 97        return reverse('author_detail', args=[self.object.id,])
 98
 99
100class AuthorCreateRestricted(AuthorCreate):
101    post = method_decorator(login_required)(AuthorCreate.post)
102
103
104class ArtistUpdate(generic.UpdateView):
105    model = Artist
106
107
108class NaiveAuthorUpdate(generic.UpdateView):
109    queryset = Author.objects.all()
110
111
112class AuthorUpdate(generic.UpdateView):
113    model = Author
114    success_url = '/list/authors/'
115
116
117class OneAuthorUpdate(generic.UpdateView):
118    success_url = '/list/authors/'
119
120    def get_object(self):
121        return Author.objects.get(pk=1)
122
123
124class SpecializedAuthorUpdate(generic.UpdateView):
125    model = Author
126    form_class = AuthorForm
127    template_name = 'generic_views/form.html'
128    context_object_name = 'thingy'
129
130    def get_success_url(self):
131        return reverse('author_detail', args=[self.object.id,])
132
133
134class NaiveAuthorDelete(generic.DeleteView):
135    queryset = Author.objects.all()
136
137
138class AuthorDelete(generic.DeleteView):
139    model = Author
140    success_url = '/list/authors/'
141
142
143class SpecializedAuthorDelete(generic.DeleteView):
144    queryset = Author.objects.all()
145    template_name = 'generic_views/confirm_delete.html'
146    context_object_name = 'thingy'
147
148    def get_success_url(self):
149        return reverse('authors_list')
150
151
152class BookConfig(object):
153    queryset = Book.objects.all()
154    date_field = 'pubdate'
155
156class BookArchive(BookConfig, generic.ArchiveIndexView):
157    pass
158
159class BookYearArchive(BookConfig, generic.YearArchiveView):
160    pass
161
162class BookMonthArchive(BookConfig, generic.MonthArchiveView):
163    pass
164
165class BookWeekArchive(BookConfig, generic.WeekArchiveView):
166    pass
167
168class BookDayArchive(BookConfig, generic.DayArchiveView):
169    pass
170
171class BookTodayArchive(BookConfig, generic.TodayArchiveView):
172    pass
173
174class BookDetail(BookConfig, generic.DateDetailView):
175    pass
176
177class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
178    def get_queryset(self):
179        return Author.objects.all()