comparison weblinks/views.py @ 1032:e932f2ecd4a7

Django 1.8 warnings / tech debt cleanup.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Dec 2015 15:10:55 -0600
parents 41d0389fc85a
children 176d1550bf25
comparison
equal deleted inserted replaced
1031:e1c03da72818 1032:e932f2ecd4a7
1 """ 1 """
2 Views for the weblinks application. 2 Views for the weblinks application.
3 3
4 """ 4 """
5 import random 5 import random
6 from django.shortcuts import render_to_response 6 from django.shortcuts import render
7 from django.template import RequestContext
8 from django.core.paginator import InvalidPage 7 from django.core.paginator import InvalidPage
9 from django.http import HttpResponse 8 from django.http import HttpResponse
10 from django.http import HttpResponseBadRequest 9 from django.http import HttpResponseBadRequest
11 from django.http import HttpResponseRedirect 10 from django.http import HttpResponseRedirect
12 from django.contrib.auth.decorators import login_required 11 from django.contrib.auth.decorators import login_required
33 ####################################################################### 32 #######################################################################
34 33
35 def link_index(request): 34 def link_index(request):
36 categories = Category.objects.all() 35 categories = Category.objects.all()
37 total_links = Link.public_objects.all().count() 36 total_links = Link.public_objects.all().count()
38 return render_to_response('weblinks/index.html', { 37 return render(request, 'weblinks/index.html', {
39 'categories': categories, 38 'categories': categories,
40 'total_links': total_links, 39 'total_links': total_links,
41 }, 40 })
42 context_instance = RequestContext(request))
43 41
44 ####################################################################### 42 #######################################################################
45 43
46 def new_links(request): 44 def new_links(request):
47 links = Link.public_objects.order_by('-date_added') 45 links = Link.public_objects.order_by('-date_added')
50 try: 48 try:
51 the_page = paginator.page(page) 49 the_page = paginator.page(page)
52 except InvalidPage: 50 except InvalidPage:
53 raise Http404 51 raise Http404
54 52
55 return render_to_response('weblinks/link_summary.html', { 53 return render(request, 'weblinks/link_summary.html', {
56 'page': the_page, 54 'page': the_page,
57 'title': 'Newest Links', 55 'title': 'Newest Links',
58 }, 56 })
59 context_instance = RequestContext(request))
60 57
61 ####################################################################### 58 #######################################################################
62 59
63 def popular_links(request): 60 def popular_links(request):
64 links = Link.public_objects.order_by('-hits') 61 links = Link.public_objects.order_by('-hits')
66 page = get_page(request.GET) 63 page = get_page(request.GET)
67 try: 64 try:
68 the_page = paginator.page(page) 65 the_page = paginator.page(page)
69 except InvalidPage: 66 except InvalidPage:
70 raise Http404 67 raise Http404
71 return render_to_response('weblinks/link_summary.html', { 68 return render(request, 'weblinks/link_summary.html', {
72 'page': the_page, 69 'page': the_page,
73 'title': 'Popular Links', 70 'title': 'Popular Links',
74 }, 71 })
75 context_instance = RequestContext(request))
76 72
77 ####################################################################### 73 #######################################################################
78 74
79 @login_required 75 @login_required
80 def add_link(request): 76 def add_link(request):
90 """) 86 """)
91 return HttpResponseRedirect(reverse('weblinks-add_thanks')) 87 return HttpResponseRedirect(reverse('weblinks-add_thanks'))
92 else: 88 else:
93 add_form = AddLinkForm() 89 add_form = AddLinkForm()
94 90
95 return render_to_response('weblinks/add_link.html', { 91 return render(request, 'weblinks/add_link.html', {
96 'add_form': add_form, 92 'add_form': add_form,
97 }, 93 })
98 context_instance = RequestContext(request))
99 94
100 ####################################################################### 95 #######################################################################
101 96
102 @login_required 97 @login_required
103 def add_thanks(request): 98 def add_thanks(request):
104 return render_to_response('weblinks/add_link.html', { 99 return render(request, 'weblinks/add_link.html')
105 },
106 context_instance = RequestContext(request))
107 100
108 ####################################################################### 101 #######################################################################
109 102
110 # Maps URL component to database field name for the links table: 103 # Maps URL component to database field name for the links table:
111 104
133 try: 126 try:
134 the_page = paginator.page(page) 127 the_page = paginator.page(page)
135 except InvalidPage: 128 except InvalidPage:
136 raise Http404 129 raise Http404
137 130
138 return render_to_response('weblinks/view_links.html', { 131 return render(request, 'weblinks/view_links.html', {
139 's' : sort, 132 's' : sort,
140 'category' : cat, 133 'category' : cat,
141 'page' : the_page, 134 'page' : the_page,
142 }, 135 })
143 context_instance = RequestContext(request))
144 136
145 ####################################################################### 137 #######################################################################
146 138
147 def _visit_link(request, link): 139 def _visit_link(request, link):
148 link.hits += 1 140 link.hits += 1
191 183
192 def link_detail(request, id): 184 def link_detail(request, id):
193 link = get_object_or_404(Link, pk=id) 185 link = get_object_or_404(Link, pk=id)
194 if not link.is_public: 186 if not link.is_public:
195 raise Http404 187 raise Http404
196 return render_to_response('weblinks/link_detail.html', { 188 return render(request, 'weblinks/link_detail.html', {
197 'link': link, 189 'link': link,
198 }, 190 })
199 context_instance = RequestContext(request))