Skip to content

Commit

Permalink
Add tagging support
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Feb 17, 2015
1 parent 09d48d1 commit 4cd35fa
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions ppb/__init__.py
@@ -0,0 +1 @@
default_app_config = "ppb.apps.AppConfig"
66 changes: 66 additions & 0 deletions ppb/admin.py
@@ -0,0 +1,66 @@
from django.contrib import admin
from django.contrib.admin import SimpleListFilter

from pinax.blog.admin import PostAdmin
from pinax.blog.models import Post
from taggit.models import TaggedItem

from .forms import AdminPostTagsForm


class TaggitListFilter(SimpleListFilter):
"""
A custom filter class that can be used to filter by taggit tags in the admin.
From: https://djangosnippets.org/snippets/2807/
"""

# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = "tags"

# Parameter for the filter that will be used in the URL query.
parameter_name = "tag"

def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each tuple is the coded value
for the option that will appear in the URL query. The second element is the
human-readable name for the option that will appear in the right sidebar.
"""
tag_list = []
tags = TaggedItem.tags_for(model_admin.model)
for tag in tags:
tag_list.append((tag.name, tag.name))
return tag_list

def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value provided in the query
string and retrievable via `self.value()`.
"""
if self.value():
return queryset.filter(tags__name__in=[self.value()])


class PostTagsAdmin(PostAdmin):
form = AdminPostTagsForm
fields = [
"section",
"title",
"slug",
"author",
"markup",
"teaser",
"content",
"description",
"primary_image",
"sharable_url",
"publish",
"tags"
]
list_filter = ["section", TaggitListFilter]


admin.site.unregister(Post)
admin.site.register(Post, PostTagsAdmin)
4 changes: 4 additions & 0 deletions ppb/apps.py
@@ -1,10 +1,14 @@
from django.apps import AppConfig as BaseAppConfig
from django.utils.importlib import import_module

from pinax.blog.models import Post
from taggit.managers import TaggableManager


class AppConfig(BaseAppConfig):

name = "ppb"

def ready(self):
Post.add_to_class("tags", TaggableManager())
import_module("ppb.receivers")
16 changes: 16 additions & 0 deletions ppb/forms.py
@@ -0,0 +1,16 @@
from pinax.blog.forms import FIELDS, AdminPostForm
from pinax.blog.models import Post

from taggit.forms import TagField


FIELDS.append("tags")


class AdminPostTagsForm(AdminPostForm):

tags = TagField()

class Meta:
model = Post
fields = FIELDS
1 change: 1 addition & 0 deletions ppb/management/commands/make_release_notes.py
Expand Up @@ -144,6 +144,7 @@ def create_post(self, name, release_url, version, date, commits):
updated=date,
published=date
)
post.tags.add(name)
Revision.objects.create(
post=post,
title=post.title,
Expand Down
Empty file added ppb/receivers.py
Empty file.
1 change: 1 addition & 0 deletions ppb/settings.py
Expand Up @@ -127,6 +127,7 @@

# external
"pinax.blog",
"taggit",

# project
"ppb",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -5,3 +5,4 @@ pytz==2014.10
psycopg2==2.6

requests==2.5.1
django-taggit==0.12.2

0 comments on commit 4cd35fa

Please sign in to comment.