From 4cd35fa31d031a7b521aa7cf14ad7b4fa76153f9 Mon Sep 17 00:00:00 2001 From: Patrick Altman Date: Tue, 17 Feb 2015 06:00:22 -0600 Subject: [PATCH] Add tagging support --- ppb/__init__.py | 1 + ppb/admin.py | 66 +++++++++++++++++++ ppb/apps.py | 4 ++ ppb/forms.py | 16 +++++ ppb/management/commands/make_release_notes.py | 1 + ppb/receivers.py | 0 ppb/settings.py | 1 + requirements.txt | 1 + 8 files changed, 90 insertions(+) create mode 100644 ppb/admin.py create mode 100644 ppb/forms.py create mode 100644 ppb/receivers.py diff --git a/ppb/__init__.py b/ppb/__init__.py index e69de29..99a5613 100644 --- a/ppb/__init__.py +++ b/ppb/__init__.py @@ -0,0 +1 @@ +default_app_config = "ppb.apps.AppConfig" diff --git a/ppb/admin.py b/ppb/admin.py new file mode 100644 index 0000000..3d3a991 --- /dev/null +++ b/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) diff --git a/ppb/apps.py b/ppb/apps.py index 28c0721..b046d3b 100644 --- a/ppb/apps.py +++ b/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") diff --git a/ppb/forms.py b/ppb/forms.py new file mode 100644 index 0000000..f69c6b7 --- /dev/null +++ b/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 diff --git a/ppb/management/commands/make_release_notes.py b/ppb/management/commands/make_release_notes.py index c0ffb8e..7fd8440 100644 --- a/ppb/management/commands/make_release_notes.py +++ b/ppb/management/commands/make_release_notes.py @@ -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, diff --git a/ppb/receivers.py b/ppb/receivers.py new file mode 100644 index 0000000..e69de29 diff --git a/ppb/settings.py b/ppb/settings.py index d583f9c..2223eda 100644 --- a/ppb/settings.py +++ b/ppb/settings.py @@ -127,6 +127,7 @@ # external "pinax.blog", + "taggit", # project "ppb", diff --git a/requirements.txt b/requirements.txt index c2becdf..8fc3e5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ pytz==2014.10 psycopg2==2.6 requests==2.5.1 +django-taggit==0.12.2