~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/regressiontests/generic_views/views.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

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