annotate weblinks/views.py @ 1172:b957e4829a03

Add reCAPTCHA to contact form
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Apr 2018 13:53:05 -0500
parents 176d1550bf25
children
rev   line source
gremmie@1 1 """
gremmie@1 2 Views for the weblinks application.
bgneal@675 3
gremmie@1 4 """
gremmie@1 5 import random
bgneal@1032 6 from django.shortcuts import render
bgneal@92 7 from django.core.paginator import InvalidPage
bgneal@165 8 from django.http import HttpResponse
bgneal@165 9 from django.http import HttpResponseBadRequest
gremmie@1 10 from django.http import HttpResponseRedirect
gremmie@1 11 from django.contrib.auth.decorators import login_required
gremmie@1 12 from django.shortcuts import get_object_or_404
gremmie@1 13 from django.core.urlresolvers import reverse
gremmie@1 14 from django.http import Http404
bgneal@165 15 from django.views.decorators.http import require_POST
gremmie@1 16
gremmie@1 17 from core.paginator import DiggPaginator
gremmie@1 18 from core.functions import email_admins
bgneal@242 19 from core.functions import get_page
gremmie@1 20 from weblinks.models import Category
gremmie@1 21 from weblinks.models import Link
gremmie@1 22 from weblinks.models import FlaggedLink
gremmie@1 23 from weblinks.forms import AddLinkForm
gremmie@1 24
gremmie@1 25 #######################################################################
gremmie@1 26
gremmie@1 27 LINKS_PER_PAGE = 10
gremmie@1 28
gremmie@1 29 def create_paginator(links):
gremmie@1 30 return DiggPaginator(links, LINKS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 31
gremmie@1 32 #######################################################################
gremmie@1 33
gremmie@1 34 def link_index(request):
gremmie@1 35 categories = Category.objects.all()
gremmie@1 36 total_links = Link.public_objects.all().count()
bgneal@1032 37 return render(request, 'weblinks/index.html', {
bgneal@675 38 'categories': categories,
gremmie@1 39 'total_links': total_links,
bgneal@1148 40 'top_menu': 'categories',
bgneal@1148 41 'V3_DESIGN': True,
bgneal@1032 42 })
gremmie@1 43
gremmie@1 44 #######################################################################
gremmie@1 45
gremmie@1 46 def new_links(request):
bgneal@242 47 links = Link.public_objects.order_by('-date_added')
bgneal@242 48 paginator = create_paginator(links)
bgneal@242 49 page = get_page(request.GET)
bgneal@242 50 try:
bgneal@242 51 the_page = paginator.page(page)
bgneal@242 52 except InvalidPage:
bgneal@242 53 raise Http404
bgneal@242 54
bgneal@1032 55 return render(request, 'weblinks/link_summary.html', {
bgneal@675 56 'page': the_page,
gremmie@1 57 'title': 'Newest Links',
bgneal@1148 58 'top_menu': 'new',
bgneal@1148 59 'V3_DESIGN': True,
bgneal@1032 60 })
gremmie@1 61
gremmie@1 62 #######################################################################
gremmie@1 63
gremmie@1 64 def popular_links(request):
bgneal@242 65 links = Link.public_objects.order_by('-hits')
bgneal@242 66 paginator = create_paginator(links)
bgneal@242 67 page = get_page(request.GET)
bgneal@242 68 try:
bgneal@242 69 the_page = paginator.page(page)
bgneal@242 70 except InvalidPage:
bgneal@242 71 raise Http404
bgneal@1032 72 return render(request, 'weblinks/link_summary.html', {
bgneal@675 73 'page': the_page,
gremmie@1 74 'title': 'Popular Links',
bgneal@1148 75 'top_menu': 'popular',
bgneal@1148 76 'V3_DESIGN': True,
bgneal@1032 77 })
gremmie@1 78
gremmie@1 79 #######################################################################
gremmie@1 80
gremmie@1 81 @login_required
gremmie@1 82 def add_link(request):
gremmie@1 83 if request.method == 'POST':
gremmie@1 84 add_form = AddLinkForm(request.POST)
gremmie@1 85 if add_form.is_valid():
gremmie@1 86 new_link = add_form.save(commit=False)
gremmie@1 87 new_link.user = request.user
gremmie@1 88 new_link.save()
gremmie@1 89 email_admins('New link for approval', """Hello,
gremmie@1 90
gremmie@1 91 A user has added a new link for your approval.
gremmie@1 92 """)
bgneal@242 93 return HttpResponseRedirect(reverse('weblinks-add_thanks'))
gremmie@1 94 else:
gremmie@1 95 add_form = AddLinkForm()
gremmie@1 96
bgneal@1032 97 return render(request, 'weblinks/add_link.html', {
gremmie@1 98 'add_form': add_form,
bgneal@1148 99 'top_menu': 'add',
bgneal@1148 100 'V3_DESIGN': True,
bgneal@1032 101 })
gremmie@1 102
gremmie@1 103 #######################################################################
gremmie@1 104
gremmie@1 105 @login_required
gremmie@1 106 def add_thanks(request):
bgneal@1148 107 return render(request, 'weblinks/add_link.html', {
bgneal@1148 108 'V3_DESIGN': True,
bgneal@1148 109 'top_menu': 'add',
bgneal@1148 110 })
gremmie@1 111
gremmie@1 112 #######################################################################
gremmie@1 113
gremmie@1 114 # Maps URL component to database field name for the links table:
gremmie@1 115
gremmie@1 116 LINK_FIELD_MAP = {
bgneal@675 117 'title': 'title',
gremmie@1 118 'date': '-date_added',
gremmie@1 119 'hits': '-hits'
gremmie@1 120 }
gremmie@1 121
bgneal@242 122 def view_links(request, slug, sort='title'):
gremmie@1 123 try:
bgneal@242 124 cat = Category.objects.get(slug=slug)
gremmie@1 125 except Category.DoesNotExist:
gremmie@1 126 raise Http404
gremmie@1 127
gremmie@1 128 if sort in LINK_FIELD_MAP:
gremmie@1 129 order_by = LINK_FIELD_MAP[sort]
gremmie@1 130 else:
gremmie@1 131 sort = 'title'
gremmie@1 132 order_by = LINK_FIELD_MAP['title']
gremmie@1 133
bgneal@242 134 links = Link.public_objects.filter(category=cat).order_by(order_by)
gremmie@1 135 paginator = create_paginator(links)
bgneal@242 136 page = get_page(request.GET)
gremmie@1 137 try:
bgneal@242 138 the_page = paginator.page(page)
gremmie@1 139 except InvalidPage:
gremmie@1 140 raise Http404
gremmie@1 141
bgneal@1032 142 return render(request, 'weblinks/view_links.html', {
gremmie@1 143 's' : sort,
gremmie@1 144 'category' : cat,
bgneal@675 145 'page' : the_page,
bgneal@1148 146 'V3_DESIGN': True,
bgneal@1032 147 })
gremmie@1 148
gremmie@1 149 #######################################################################
gremmie@1 150
gremmie@1 151 def _visit_link(request, link):
gremmie@1 152 link.hits += 1
gremmie@1 153 link.save()
gremmie@1 154 return HttpResponseRedirect(link.url)
gremmie@1 155
gremmie@1 156 #######################################################################
gremmie@1 157
bgneal@208 158 @require_POST
gremmie@1 159 def visit(request, link_id):
gremmie@1 160 link = get_object_or_404(Link, pk = link_id)
gremmie@1 161 return _visit_link(request, link)
gremmie@1 162
gremmie@1 163 #######################################################################
gremmie@1 164
gremmie@1 165 def random_link(request):
bgneal@1148 166 if request.method == 'POST':
bgneal@1148 167 ids = Link.public_objects.values_list('id', flat=True)
bgneal@1148 168 if not ids:
bgneal@1148 169 raise Http404
bgneal@1148 170 the_id = random.choice(ids)
bgneal@1148 171 random_link = Link.public_objects.get(pk=the_id)
bgneal@1148 172 return _visit_link(request, random_link)
bgneal@1148 173
bgneal@1148 174 return render(request, 'weblinks/random_link.html', {
bgneal@1148 175 'top_menu': 'random',
bgneal@1148 176 'V3_DESIGN': True,
bgneal@1148 177 })
gremmie@1 178
gremmie@1 179 #######################################################################
gremmie@1 180
bgneal@165 181 @require_POST
gremmie@1 182 def report_link(request, link_id):
bgneal@165 183 """
bgneal@165 184 This function is the target of an AJAX POST to report a link as dead.
bgneal@165 185 """
bgneal@165 186 if not request.user.is_authenticated():
bgneal@165 187 return HttpResponse('Please login or register to report a broken link.')
gremmie@1 188
bgneal@165 189 try:
bgneal@165 190 link = Link.objects.get(pk=link_id)
bgneal@165 191 except Link.DoesNotExist:
bgneal@165 192 return HttpResponseBadRequest("That link doesn't exist.")
gremmie@1 193
bgneal@165 194 FlaggedLink.objects.create(link, request.user)
bgneal@675 195 return HttpResponse("The link was reported. A moderator will review the "
bgneal@675 196 "link shortly. Thanks for helping to improve the content on "
bgneal@165 197 "this site.")
gremmie@1 198
gremmie@1 199 #######################################################################
gremmie@1 200
bgneal@20 201 def link_detail(request, id):
bgneal@20 202 link = get_object_or_404(Link, pk=id)
bgneal@845 203 if not link.is_public:
bgneal@845 204 raise Http404
bgneal@1032 205 return render(request, 'weblinks/link_detail.html', {
bgneal@675 206 'link': link,
bgneal@1148 207 'V3_DESIGN': True,
bgneal@1032 208 })