annotate weblinks/views.py @ 1102:1a4f4a69e244

Membermap now uses new post box.
author Brian Neal <bgneal@gmail.com>
date Wed, 06 Jul 2016 20:03:59 -0500
parents e932f2ecd4a7
children 176d1550bf25
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@1032 40 })
gremmie@1 41
gremmie@1 42 #######################################################################
gremmie@1 43
gremmie@1 44 def new_links(request):
bgneal@242 45 links = Link.public_objects.order_by('-date_added')
bgneal@242 46 paginator = create_paginator(links)
bgneal@242 47 page = get_page(request.GET)
bgneal@242 48 try:
bgneal@242 49 the_page = paginator.page(page)
bgneal@242 50 except InvalidPage:
bgneal@242 51 raise Http404
bgneal@242 52
bgneal@1032 53 return render(request, 'weblinks/link_summary.html', {
bgneal@675 54 'page': the_page,
gremmie@1 55 'title': 'Newest Links',
bgneal@1032 56 })
gremmie@1 57
gremmie@1 58 #######################################################################
gremmie@1 59
gremmie@1 60 def popular_links(request):
bgneal@242 61 links = Link.public_objects.order_by('-hits')
bgneal@242 62 paginator = create_paginator(links)
bgneal@242 63 page = get_page(request.GET)
bgneal@242 64 try:
bgneal@242 65 the_page = paginator.page(page)
bgneal@242 66 except InvalidPage:
bgneal@242 67 raise Http404
bgneal@1032 68 return render(request, 'weblinks/link_summary.html', {
bgneal@675 69 'page': the_page,
gremmie@1 70 'title': 'Popular Links',
bgneal@1032 71 })
gremmie@1 72
gremmie@1 73 #######################################################################
gremmie@1 74
gremmie@1 75 @login_required
gremmie@1 76 def add_link(request):
gremmie@1 77 if request.method == 'POST':
gremmie@1 78 add_form = AddLinkForm(request.POST)
gremmie@1 79 if add_form.is_valid():
gremmie@1 80 new_link = add_form.save(commit=False)
gremmie@1 81 new_link.user = request.user
gremmie@1 82 new_link.save()
gremmie@1 83 email_admins('New link for approval', """Hello,
gremmie@1 84
gremmie@1 85 A user has added a new link for your approval.
gremmie@1 86 """)
bgneal@242 87 return HttpResponseRedirect(reverse('weblinks-add_thanks'))
gremmie@1 88 else:
gremmie@1 89 add_form = AddLinkForm()
gremmie@1 90
bgneal@1032 91 return render(request, 'weblinks/add_link.html', {
gremmie@1 92 'add_form': add_form,
bgneal@1032 93 })
gremmie@1 94
gremmie@1 95 #######################################################################
gremmie@1 96
gremmie@1 97 @login_required
gremmie@1 98 def add_thanks(request):
bgneal@1032 99 return render(request, 'weblinks/add_link.html')
gremmie@1 100
gremmie@1 101 #######################################################################
gremmie@1 102
gremmie@1 103 # Maps URL component to database field name for the links table:
gremmie@1 104
gremmie@1 105 LINK_FIELD_MAP = {
bgneal@675 106 'title': 'title',
gremmie@1 107 'date': '-date_added',
gremmie@1 108 'hits': '-hits'
gremmie@1 109 }
gremmie@1 110
bgneal@242 111 def view_links(request, slug, sort='title'):
gremmie@1 112 try:
bgneal@242 113 cat = Category.objects.get(slug=slug)
gremmie@1 114 except Category.DoesNotExist:
gremmie@1 115 raise Http404
gremmie@1 116
gremmie@1 117 if sort in LINK_FIELD_MAP:
gremmie@1 118 order_by = LINK_FIELD_MAP[sort]
gremmie@1 119 else:
gremmie@1 120 sort = 'title'
gremmie@1 121 order_by = LINK_FIELD_MAP['title']
gremmie@1 122
bgneal@242 123 links = Link.public_objects.filter(category=cat).order_by(order_by)
gremmie@1 124 paginator = create_paginator(links)
bgneal@242 125 page = get_page(request.GET)
gremmie@1 126 try:
bgneal@242 127 the_page = paginator.page(page)
gremmie@1 128 except InvalidPage:
gremmie@1 129 raise Http404
gremmie@1 130
bgneal@1032 131 return render(request, 'weblinks/view_links.html', {
gremmie@1 132 's' : sort,
gremmie@1 133 'category' : cat,
bgneal@675 134 'page' : the_page,
bgneal@1032 135 })
gremmie@1 136
gremmie@1 137 #######################################################################
gremmie@1 138
gremmie@1 139 def _visit_link(request, link):
gremmie@1 140 link.hits += 1
gremmie@1 141 link.save()
gremmie@1 142 return HttpResponseRedirect(link.url)
gremmie@1 143
gremmie@1 144 #######################################################################
gremmie@1 145
bgneal@208 146 @require_POST
gremmie@1 147 def visit(request, link_id):
gremmie@1 148 link = get_object_or_404(Link, pk = link_id)
gremmie@1 149 return _visit_link(request, link)
gremmie@1 150
gremmie@1 151 #######################################################################
gremmie@1 152
bgneal@208 153 @require_POST
gremmie@1 154 def random_link(request):
gremmie@1 155 ids = Link.public_objects.values_list('id', flat=True)
gremmie@1 156 if not ids:
gremmie@1 157 raise Http404
gremmie@1 158 id = random.choice(ids)
gremmie@1 159 random_link = Link.public_objects.get(pk=id)
gremmie@1 160 return _visit_link(request, random_link)
gremmie@1 161
gremmie@1 162 #######################################################################
gremmie@1 163
bgneal@165 164 @require_POST
gremmie@1 165 def report_link(request, link_id):
bgneal@165 166 """
bgneal@165 167 This function is the target of an AJAX POST to report a link as dead.
bgneal@165 168 """
bgneal@165 169 if not request.user.is_authenticated():
bgneal@165 170 return HttpResponse('Please login or register to report a broken link.')
gremmie@1 171
bgneal@165 172 try:
bgneal@165 173 link = Link.objects.get(pk=link_id)
bgneal@165 174 except Link.DoesNotExist:
bgneal@165 175 return HttpResponseBadRequest("That link doesn't exist.")
gremmie@1 176
bgneal@165 177 FlaggedLink.objects.create(link, request.user)
bgneal@675 178 return HttpResponse("The link was reported. A moderator will review the "
bgneal@675 179 "link shortly. Thanks for helping to improve the content on "
bgneal@165 180 "this site.")
gremmie@1 181
gremmie@1 182 #######################################################################
gremmie@1 183
bgneal@20 184 def link_detail(request, id):
bgneal@20 185 link = get_object_or_404(Link, pk=id)
bgneal@845 186 if not link.is_public:
bgneal@845 187 raise Http404
bgneal@1032 188 return render(request, 'weblinks/link_detail.html', {
bgneal@675 189 'link': link,
bgneal@1032 190 })