gremmie@1: """ gremmie@1: Views for the weblinks application. bgneal@675: gremmie@1: """ gremmie@1: import random gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext bgneal@92: from django.core.paginator import InvalidPage bgneal@165: from django.http import HttpResponse bgneal@165: from django.http import HttpResponseBadRequest gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.contrib.auth.decorators import login_required gremmie@1: from django.shortcuts import get_object_or_404 gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.http import Http404 bgneal@165: from django.views.decorators.http import require_POST gremmie@1: gremmie@1: from core.paginator import DiggPaginator gremmie@1: from core.functions import email_admins bgneal@242: from core.functions import get_page gremmie@1: from weblinks.models import Category gremmie@1: from weblinks.models import Link gremmie@1: from weblinks.models import FlaggedLink gremmie@1: from weblinks.forms import AddLinkForm gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: LINKS_PER_PAGE = 10 gremmie@1: gremmie@1: def create_paginator(links): gremmie@1: return DiggPaginator(links, LINKS_PER_PAGE, body=5, tail=3, margin=3, padding=2) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def link_index(request): gremmie@1: categories = Category.objects.all() gremmie@1: total_links = Link.public_objects.all().count() gremmie@1: return render_to_response('weblinks/index.html', { bgneal@675: 'categories': categories, gremmie@1: 'total_links': total_links, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def new_links(request): bgneal@242: links = Link.public_objects.order_by('-date_added') bgneal@242: paginator = create_paginator(links) bgneal@242: page = get_page(request.GET) bgneal@242: try: bgneal@242: the_page = paginator.page(page) bgneal@242: except InvalidPage: bgneal@242: raise Http404 bgneal@242: gremmie@1: return render_to_response('weblinks/link_summary.html', { bgneal@675: 'page': the_page, gremmie@1: 'title': 'Newest Links', gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def popular_links(request): bgneal@242: links = Link.public_objects.order_by('-hits') bgneal@242: paginator = create_paginator(links) bgneal@242: page = get_page(request.GET) bgneal@242: try: bgneal@242: the_page = paginator.page(page) bgneal@242: except InvalidPage: bgneal@242: raise Http404 gremmie@1: return render_to_response('weblinks/link_summary.html', { bgneal@675: 'page': the_page, gremmie@1: 'title': 'Popular Links', gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def add_link(request): gremmie@1: if request.method == 'POST': gremmie@1: add_form = AddLinkForm(request.POST) gremmie@1: if add_form.is_valid(): gremmie@1: new_link = add_form.save(commit=False) gremmie@1: new_link.user = request.user gremmie@1: new_link.save() gremmie@1: email_admins('New link for approval', """Hello, gremmie@1: gremmie@1: A user has added a new link for your approval. gremmie@1: """) bgneal@242: return HttpResponseRedirect(reverse('weblinks-add_thanks')) gremmie@1: else: gremmie@1: add_form = AddLinkForm() gremmie@1: gremmie@1: return render_to_response('weblinks/add_link.html', { gremmie@1: 'add_form': add_form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def add_thanks(request): gremmie@1: return render_to_response('weblinks/add_link.html', { gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: # Maps URL component to database field name for the links table: gremmie@1: gremmie@1: LINK_FIELD_MAP = { bgneal@675: 'title': 'title', gremmie@1: 'date': '-date_added', gremmie@1: 'hits': '-hits' gremmie@1: } gremmie@1: bgneal@242: def view_links(request, slug, sort='title'): gremmie@1: try: bgneal@242: cat = Category.objects.get(slug=slug) gremmie@1: except Category.DoesNotExist: gremmie@1: raise Http404 gremmie@1: gremmie@1: if sort in LINK_FIELD_MAP: gremmie@1: order_by = LINK_FIELD_MAP[sort] gremmie@1: else: gremmie@1: sort = 'title' gremmie@1: order_by = LINK_FIELD_MAP['title'] gremmie@1: bgneal@242: links = Link.public_objects.filter(category=cat).order_by(order_by) gremmie@1: paginator = create_paginator(links) bgneal@242: page = get_page(request.GET) gremmie@1: try: bgneal@242: the_page = paginator.page(page) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: gremmie@1: return render_to_response('weblinks/view_links.html', { gremmie@1: 's' : sort, gremmie@1: 'category' : cat, bgneal@675: 'page' : the_page, bgneal@675: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def _visit_link(request, link): gremmie@1: link.hits += 1 gremmie@1: link.save() gremmie@1: return HttpResponseRedirect(link.url) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@208: @require_POST gremmie@1: def visit(request, link_id): gremmie@1: link = get_object_or_404(Link, pk = link_id) gremmie@1: return _visit_link(request, link) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@208: @require_POST gremmie@1: def random_link(request): gremmie@1: ids = Link.public_objects.values_list('id', flat=True) gremmie@1: if not ids: gremmie@1: raise Http404 gremmie@1: id = random.choice(ids) gremmie@1: random_link = Link.public_objects.get(pk=id) gremmie@1: return _visit_link(request, random_link) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@165: @require_POST gremmie@1: def report_link(request, link_id): bgneal@165: """ bgneal@165: This function is the target of an AJAX POST to report a link as dead. bgneal@165: """ bgneal@165: if not request.user.is_authenticated(): bgneal@165: return HttpResponse('Please login or register to report a broken link.') gremmie@1: bgneal@165: try: bgneal@165: link = Link.objects.get(pk=link_id) bgneal@165: except Link.DoesNotExist: bgneal@165: return HttpResponseBadRequest("That link doesn't exist.") gremmie@1: bgneal@165: FlaggedLink.objects.create(link, request.user) bgneal@675: return HttpResponse("The link was reported. A moderator will review the " bgneal@675: "link shortly. Thanks for helping to improve the content on " bgneal@165: "this site.") gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@20: def link_detail(request, id): bgneal@20: link = get_object_or_404(Link, pk=id) bgneal@20: return render_to_response('weblinks/link_detail.html', { bgneal@675: 'link': link, bgneal@20: }, bgneal@20: context_instance = RequestContext(request))