annotate weblinks/views.py @ 975:8c3d52b7cbd1

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