# HG changeset patch # User Brian Neal # Date 1262492114 0 # Node ID 952e05cb3d8004e9e347956ca6df28bf1a14dff2 # Parent f7a6b8fe45569afc1d8b6c149de496156852901d Implement #49; use POST for updating link hit counts. Also refactored a bit and use javascript to report broken links. diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/core/templatetags/custom_admin_tags.py --- a/gpp/core/templatetags/custom_admin_tags.py Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/core/templatetags/custom_admin_tags.py Sun Jan 03 04:15:14 2010 +0000 @@ -10,7 +10,7 @@ from forums.models import FlaggedPost from gcalendar.models import Event from news.models import PendingStory -from weblinks.models import Link +from weblinks.models import Link, FlaggedLink from shoutbox.models import ShoutFlag @@ -33,6 +33,7 @@ Q(status=Event.DEL_REQ)).count() new_stories = PendingStory.objects.count() new_links = Link.objects.filter(is_public=False).count() + broken_links = FlaggedLink.objects.count() flagged_shouts = ShoutFlag.objects.count() return { @@ -44,5 +45,6 @@ 'event_requests': event_requests, 'new_stories': new_stories, 'new_links': new_links, + 'broken_links': broken_links, 'flagged_shouts': flagged_shouts, } diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/templates/core/admin_dashboard.html --- a/gpp/templates/core/admin_dashboard.html Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/templates/core/admin_dashboard.html Sun Jan 03 04:15:14 2010 +0000 @@ -1,5 +1,5 @@ {% if user.is_staff %} -{% if flagged_posts or flagged_comments or flagged_profiles or event_requests or new_stories or new_downloads or new_links or flagged_shouts %} +{% if flagged_posts or flagged_comments or flagged_profiles or event_requests or new_stories or new_downloads or new_links or flagged_shouts or broken_links %} {% endif %} diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/templates/weblinks/base.html --- a/gpp/templates/weblinks/base.html Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/templates/weblinks/base.html Sun Jan 03 04:15:14 2010 +0000 @@ -3,6 +3,7 @@ {% block custom_css %} {% block weblinks_css %}{% endblock %} +{% block weblinks_js %}{% endblock %} {% endblock %} {% block content %}

Web Links

diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/templates/weblinks/link.html --- a/gpp/templates/weblinks/link.html Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/templates/weblinks/link.html Sun Jan 03 04:15:14 2010 +0000 @@ -1,17 +1,19 @@
-{{ link.title }} +

{{ link.title }}

{{ link.description }}

+
- + + +

diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/templates/weblinks/navigation.html --- a/gpp/templates/weblinks/navigation.html Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/templates/weblinks/navigation.html Sun Jan 03 04:15:14 2010 +0000 @@ -11,8 +11,12 @@
  • Categories
  • New
  • Popular
  • -
  • Random
  • {% if user.is_authenticated %} -
  • Add
  • +
  • Add Link
  • {% endif %} +
    +
    + +
    +
    diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/templates/weblinks/report_link.html --- a/gpp/templates/weblinks/report_link.html Mon Dec 28 16:52:42 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -{% extends 'weblinks/base.html' %} -{% block title %}Web Links: Report Broken Link{% endblock %} -{% block weblinks_content %} -

    Report Broken Link

    - {% if report_thanks %} -

    Thank you for helping to keep our links database current. Your report has - been sent to the site staff for review.

    - {% else %} -

    - Do you wish to report {{ link.title }} - as a broken link? This will notify the site staff that the link is now dead and may need to be deleted or - revised.

    -
    - - -
    -
    - {% endif %} -{% endblock %} diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/templates/weblinks/view_links.html --- a/gpp/templates/weblinks/view_links.html Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/templates/weblinks/view_links.html Sun Jan 03 04:15:14 2010 +0000 @@ -4,6 +4,9 @@ {% endblock %} +{% block weblinks_js %} + +{% endblock %} {% block weblinks_content %}

    Category: {{ category.title }}

    diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/weblinks/admin.py --- a/gpp/weblinks/admin.py Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/weblinks/admin.py Sun Jan 03 04:15:14 2010 +0000 @@ -15,7 +15,7 @@ save_on_top = True class FlaggedLinkAdmin(admin.ModelAdmin): - list_display = ('__unicode__', 'url', 'date_flagged') + list_display = ('__unicode__', 'url', 'get_link_url', 'user', 'date_flagged') date_hierarchy = 'date_flagged' raw_id_fields = ('user', ) diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/weblinks/models.py --- a/gpp/weblinks/models.py Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/weblinks/models.py Sun Jan 03 04:15:14 2010 +0000 @@ -81,6 +81,12 @@ def url(self): return self.link.url + def get_link_url(self): + return 'Link #%d' % (self.link.get_absolute_url(), + self.link.id) + get_link_url.allow_tags = True + get_link_url.short_description = "View Link on Site" + def __unicode__(self): return self.link.title diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/weblinks/urls.py --- a/gpp/weblinks/urls.py Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/weblinks/urls.py Sun Jan 03 04:15:14 2010 +0000 @@ -15,9 +15,8 @@ (r'^popular/$', 'popular_links'), (r'^random/$', 'random_link'), (r'^report/(\d+)/$', 'report_link'), - (r'^report/thanks/(\d+)$', 'report_thanks'), url(r'^search/page/(?P\d+)/$', 'search_links', name="weblinks-search"), - (r'^visit/(\d+)/$', 'visit'), + url(r'^visit/(\d+)/$', 'visit', name="weblinks-visit"), ) diff -r f7a6b8fe4556 -r 952e05cb3d80 gpp/weblinks/views.py --- a/gpp/weblinks/views.py Mon Dec 28 16:52:42 2009 +0000 +++ b/gpp/weblinks/views.py Sun Jan 03 04:15:14 2010 +0000 @@ -8,12 +8,15 @@ from django.template import RequestContext from django.contrib import auth from django.core.paginator import InvalidPage +from django.http import HttpResponse +from django.http import HttpResponseBadRequest from django.http import HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404 from django.core.urlresolvers import reverse from django.db.models import Q from django.http import Http404 +from django.views.decorators.http import require_POST from core.paginator import DiggPaginator from core.functions import email_admins @@ -154,34 +157,23 @@ ####################################################################### -@login_required +@require_POST def report_link(request, link_id): - link = get_object_or_404(Link, pk = link_id) - if request.method == "POST": - FlaggedLink.objects.create(link, request.user) - email_admins('A Link Has Been Flagged as Broken', """Hello, + """ + This function is the target of an AJAX POST to report a link as dead. + """ + if not request.user.is_authenticated(): + return HttpResponse('Please login or register to report a broken link.') -A user has flagged a link as broken. -""") - return HttpResponseRedirect(reverse('weblinks.views.report_thanks', args = (link_id, ))) + try: + link = Link.objects.get(pk=link_id) + except Link.DoesNotExist: + return HttpResponseBadRequest("That link doesn't exist.") - return render_to_response('weblinks/report_link.html', { - 'link': link, - 'report_thanks': False, - }, - context_instance = RequestContext(request)) - - -####################################################################### - -@login_required -def report_thanks(request, link_id): - link = get_object_or_404(Link, pk = link_id) - return render_to_response('weblinks/report_link.html', { - 'link': link, - 'report_thanks': True, - }, - context_instance = RequestContext(request)) + FlaggedLink.objects.create(link, request.user) + return HttpResponse("The link was reported. A moderator will review the " \ + "link shortly. Thanks for helping to improve the content on " \ + "this site.") ####################################################################### diff -r f7a6b8fe4556 -r 952e05cb3d80 media/js/weblinks.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/media/js/weblinks.js Sun Jan 03 04:15:14 2010 +0000 @@ -0,0 +1,24 @@ +$(document).ready(function() { + $('a.weblinks-broken').click(function () { + var id = this.id; + if (id.match(/^link-(\d+)$/)) { + id = RegExp.$1; + if (confirm('Do you really want to report this link as broken? ' + + 'This will notify the site staff that the link is dead and that ' + + 'it may need to be deleted or revised.')) { + $.ajax({ + url: '/links/report/' + id + '/', + type: 'POST', + dataType: 'text', + success: function (response, textStatus) { + alert(response); + }, + error: function (xhr, textStatus, ex) { + alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + xhr.responseText); + } + }); + } + } + return false; + }); +});