changeset 165:952e05cb3d80

Implement #49; use POST for updating link hit counts. Also refactored a bit and use javascript to report broken links.
author Brian Neal <bgneal@gmail.com>
date Sun, 03 Jan 2010 04:15:14 +0000
parents f7a6b8fe4556
children 8acf5be27f18
files gpp/core/templatetags/custom_admin_tags.py gpp/templates/core/admin_dashboard.html gpp/templates/weblinks/base.html gpp/templates/weblinks/link.html gpp/templates/weblinks/navigation.html gpp/templates/weblinks/report_link.html gpp/templates/weblinks/view_links.html gpp/weblinks/admin.py gpp/weblinks/models.py gpp/weblinks/urls.py gpp/weblinks/views.py media/js/weblinks.js
diffstat 12 files changed, 73 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- 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,
         }
--- 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 %}
 <ul id="dashboard-list">
 {% if flagged_posts %}
 <li><a href="/admin/forums/flaggedpost/">Posts</a>: {{ flagged_posts }}</li>
@@ -23,7 +23,10 @@
 <li><a href="/admin/downloads/download/">Downloads</a>: {{ new_downloads }}</li>
 {% endif %}
 {% if new_links %}
-<li><a href="/admin/weblinks/link/">Links</a>: {{ new_links }}</li>
+<li><a href="/admin/weblinks/link/">New Links</a>: {{ new_links }}</li>
+{% endif %}
+{% if broken_links %}
+<li><a href="/admin/weblinks/flaggedlink/">Broken Links</a>: {{ broken_links }}</li>
 {% endif %}
 </ul>
 {% endif %}
--- 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 %}
 <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/weblinks.css" />
 {% block weblinks_css %}{% endblock %}
+{% block weblinks_js %}{% endblock %}
 {% endblock %}
 {% block content %}
 <h2>Web Links</h2>
--- 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 @@
 <dt>
-<a href="{% url weblinks.views.visit link.id %}" target="_blank">{{ link.title }}</a>
+<h4>{{ link.title }}</h4>
 </dt>
 <dd>
 <p>{{ link.description }}</p>
+<form action="{% url weblinks-visit link.id %}" method="post">
 <table class="link-stats">
    <tr>
-      <th>Date:</th><td>{{ link.date_added|date:"M d, Y" }}</td>
+      <th>Date Added:</th><td>{{ link.date_added|date:"M d, Y" }}</td>
       <th>Hits:</th><td>{{ link.hits }}</td>
       <td>
-<a href="{% url weblinks.views.report_link link.id %}"><img src="{{ MEDIA_URL }}icons/link_break.png" alt="Broken Link" 
-   title="Report broken link" /></a>
+         <a href="#" class="weblinks-broken" id="link-{{ link.id }}"><img src="{{ MEDIA_URL }}icons/link_break.png" alt="Broken Link" title="Report this link as broken" /></a>
       </td>
    </tr>
+   <tr><td><input type="submit" value="Visit Site" /></td></tr>
 </table>
+</form>
 <br />
 </dd>
--- 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 @@
 <li><a href="{% url weblinks.views.link_index %}">Categories</a></li>
 <li><a href="{% url weblinks.views.new_links %}">New</a></li>
 <li><a href="{% url weblinks.views.popular_links %}">Popular</a></li>
-<li><a href="{% url weblinks.views.random_link %}" target="_blank">Random</a></li>
 {% if user.is_authenticated %}
-<li><a href="{% url weblinks.views.add_link %}">Add</a></li>
+<li><a href="{% url weblinks.views.add_link %}">Add Link</a></li>
 {% endif %}
 </ul>
+<center>
+   <form action="{% url weblinks.views.random_link %}" method="post">
+      <input type="submit" value="Visit a Random Link" />
+   </form>
+</center>
--- 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 %}
-   <h3>Report Broken Link</h3>
-   {% if report_thanks %}
-   <p><strong>Thank you for helping to keep our links database current</strong>. Your report has
-   been sent to the site staff for review.</p>
-   {% else %}
-   <p>
-   Do you wish to report <a href="{% url weblinks.views.visit link.id %}" target="_blank">{{ link.title }}</a>
-   as a broken link? This will notify the site staff that the link is now dead and may need to be deleted or
-   revised.</p>
-   <form action="." method="post">
-      <input type="submit" value="Yes, this link is broken" />
-      <input type="button" value="Oops, nevermind" onclick="history.back(); return true;" />
-   </form>
-   <br />
-   {% endif %}
-{% endblock %}
--- 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 @@
 <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/tab-nav.css" />
 <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/pagination.css" />
 {% endblock %}
+{% block weblinks_js %}
+<script type="text/javascript" src="{{ MEDIA_URL }}js/weblinks.js"></script>
+{% endblock %}
 {% block weblinks_content %}
 <h3>Category: {{ category.title }}</h3>
 
--- 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', )
 
--- 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 '<a href="%s">Link #%d</a>' % (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
 
--- 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<page>\d+)/$', 
       'search_links',
       name="weblinks-search"),
-   (r'^visit/(\d+)/$', 'visit'),
+   url(r'^visit/(\d+)/$', 'visit', name="weblinks-visit"),
 )
--- 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.")
 
 #######################################################################
 
--- /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;
+   });
+});