changeset 675:6e6492468bb8

Add admin action to acknowledge flagged links. Fix bug where weblinks js wasn't being included on detail page.
author Brian Neal <bgneal@gmail.com>
date Sat, 03 Aug 2013 22:12:03 -0500 (2013-08-04)
parents 92101013d5ac
children afb17af7948f
files sg101/templates/weblinks/link_detail.html weblinks/admin.py weblinks/models.py weblinks/views.py
diffstat 4 files changed, 44 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/sg101/templates/weblinks/link_detail.html	Mon May 27 15:04:52 2013 -0500
+++ b/sg101/templates/weblinks/link_detail.html	Sat Aug 03 22:12:03 2013 -0500
@@ -1,5 +1,8 @@
 {% extends 'weblinks/base.html' %}
 {% block title %}Web Links: {{ link.title }}{% endblock %}
+{% block weblinks_js %}
+<script type="text/javascript" src="{{ STATIC_URL }}js/weblinks.js"></script>
+{% endblock %}
 {% block weblinks_content %}
 <h3>Link Details: {{ link.title }}</h3>
 <dl>
--- a/weblinks/admin.py	Mon May 27 15:04:52 2013 -0500
+++ b/weblinks/admin.py	Sat Aug 03 22:12:03 2013 -0500
@@ -9,16 +9,16 @@
 
 
 class CategoryAdmin(admin.ModelAdmin):
-    list_display = ('title', 'slug', 'description', 'count')
-    prepopulated_fields = {'slug': ('title', )}
-    readonly_fields = ('count', )
+    list_display = ['title', 'slug', 'description', 'count']
+    prepopulated_fields = {'slug': ['title', ]}
+    readonly_fields = ['count']
 
 
 class PendingLinkAdmin(admin.ModelAdmin):
-    list_display = ('title', 'url', 'user', 'category', 'date_added')
-    raw_id_fields = ('user', )
-    actions = ('approve_links', )
-    readonly_fields = ('update_date', )
+    list_display = ['title', 'url', 'user', 'category', 'date_added']
+    raw_id_fields = ['user']
+    actions = ['approve_links']
+    readonly_fields = ['update_date']
 
     def approve_links(self, request, qs):
         for pending_link in qs:
@@ -41,20 +41,35 @@
 
 
 class LinkAdmin(admin.ModelAdmin):
-    list_display = ('title', 'url', 'category', 'date_added', 'hits', 'is_public')
-    list_filter = ('date_added', 'is_public', 'category')
+    list_display = ['title', 'url', 'category', 'date_added', 'hits', 'is_public']
+    list_filter = ['date_added', 'is_public', 'category']
     date_hierarchy = 'date_added'
-    ordering = ('-date_added', )
-    search_fields = ('title', 'description', 'url', 'user__username')
-    raw_id_fields = ('user', )
-    readonly_fields = ('update_date', )
+    ordering = ['-date_added']
+    search_fields = ['title', 'description', 'url', 'user__username']
+    raw_id_fields = ['user']
+    readonly_fields = ['update_date']
     save_on_top = True
 
 
 class FlaggedLinkAdmin(admin.ModelAdmin):
-    list_display = ('__unicode__', 'url', 'get_link_url', 'user', 'date_flagged')
+    list_display = ['__unicode__', 'url', 'get_link_url', 'user', 'date_flagged']
     date_hierarchy = 'date_flagged'
-    raw_id_fields = ('user', )
+    raw_id_fields = ['user']
+
+    actions = ['hide_links']
+
+    def hide_links(self, request, qs):
+        for flagged_link in qs:
+            flagged_link.link.is_public = False
+            flagged_link.link.save()
+
+        count = len(qs)
+        qs.delete()
+        msg = "1 link" if count == 1 else "%d links" % count
+        self.message_user(request, "%s hidden." % msg)
+
+    hide_links.short_description = "Accept flags & hide links"
+
 
 admin.site.register(Category, CategoryAdmin)
 admin.site.register(PendingLink, PendingLinkAdmin)
--- a/weblinks/models.py	Mon May 27 15:04:52 2013 -0500
+++ b/weblinks/models.py	Sat Aug 03 22:12:03 2013 -0500
@@ -111,7 +111,7 @@
     link = models.ForeignKey(Link)
     user = models.ForeignKey(User)
     date_flagged = models.DateField(auto_now_add = True)
-    approved = models.BooleanField(default = False, 
+    approved = models.BooleanField(default = False,
         help_text = 'Check this and save to remove the referenced link from the database')
 
     objects = FlaggedLinkManager()
--- a/weblinks/views.py	Mon May 27 15:04:52 2013 -0500
+++ b/weblinks/views.py	Sat Aug 03 22:12:03 2013 -0500
@@ -1,8 +1,7 @@
 """
 Views for the weblinks application.
+
 """
-
-import datetime
 import random
 from django.shortcuts import render_to_response
 from django.template import RequestContext
@@ -13,7 +12,6 @@
 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
 
@@ -38,7 +36,7 @@
    categories = Category.objects.all()
    total_links = Link.public_objects.all().count()
    return render_to_response('weblinks/index.html', {
-      'categories': categories, 
+      'categories': categories,
       'total_links': total_links,
       },
       context_instance = RequestContext(request))
@@ -55,7 +53,7 @@
       raise Http404
 
    return render_to_response('weblinks/link_summary.html', {
-      'page': the_page, 
+      'page': the_page,
       'title': 'Newest Links',
       },
       context_instance = RequestContext(request))
@@ -71,7 +69,7 @@
    except InvalidPage:
       raise Http404
    return render_to_response('weblinks/link_summary.html', {
-      'page': the_page, 
+      'page': the_page,
       'title': 'Popular Links',
       },
       context_instance = RequestContext(request))
@@ -112,7 +110,7 @@
 # Maps URL component to database field name for the links table:
 
 LINK_FIELD_MAP = {
-   'title': 'title', 
+   'title': 'title',
    'date': '-date_added',
    'hits': '-hits'
 }
@@ -140,8 +138,8 @@
    return render_to_response('weblinks/view_links.html', {
       's' : sort,
       'category' : cat,
-      'page' : the_page, 
-      }, 
+      'page' : the_page,
+      },
       context_instance = RequestContext(request))
 
 #######################################################################
@@ -185,8 +183,8 @@
         return HttpResponseBadRequest("That link doesn't exist.")
 
     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 " \
+    return HttpResponse("The link was reported. A moderator will review the "
+            "link shortly. Thanks for helping to improve the content on "
             "this site.")
 
 #######################################################################
@@ -194,6 +192,6 @@
 def link_detail(request, id):
     link = get_object_or_404(Link, pk=id)
     return render_to_response('weblinks/link_detail.html', {
-        'link': link, 
+        'link': link,
         },
         context_instance = RequestContext(request))