From 0a24b216d0f8ab601f9c1e2a10ddadc3e8949c0d Mon Sep 17 00:00:00 2001 From: Charles Lehner Date: Thu, 17 Sep 2015 00:17:30 -0400 Subject: [PATCH] Add feed for recipes by tag --- feed/urls.py | 5 +++-- feed/views.py | 31 ++++++++++++++++++++++++++++++- tags/views.py | 5 ++++- templates/tags/recipe_tags.html | 1 + 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/feed/urls.py b/feed/urls.py index e23dbf20..eb264cd3 100644 --- a/feed/urls.py +++ b/feed/urls.py @@ -1,7 +1,8 @@ from django.conf.urls import * -from views import RecentRecipesFeed,TopRecipesFeed +from views import RecentRecipesFeed,TopRecipesFeed,TaggedRecipesFeed urlpatterns = patterns('', (r'^recent/$', RecentRecipesFeed()), (r'^top/$', TopRecipesFeed()), -) \ No newline at end of file + (r'^tag/(?P[-\w]+)/$', TaggedRecipesFeed()), +) diff --git a/feed/views.py b/feed/views.py index 8b244535..40ae367b 100644 --- a/feed/views.py +++ b/feed/views.py @@ -2,6 +2,7 @@ from django.conf import settings from django.utils.translation import ugettext as _ from recipe.models import Recipe +from taggit.models import Tag, TaggedItem class RecentRecipesFeed(Feed): @@ -32,4 +33,32 @@ def item_title(self, item): return item.title def item_description(self, item): - return item.info \ No newline at end of file + return item.info + +class TaggedRecipesFeed(Feed): + + def get_object(self, request, tag): + return tag + + def title(self, tag): + return settings.OETITLE + _(" Recipes tagged %s") % tag + + def link(self, tag): + return "/tags/recipe/" + tag + "/" + + def description(self, tag): + return _("New recipes tagged %s added to %s") % (tag, settings.OETITLE) + + def items(self, tag): + try: + recipe_tag = Tag.objects.get(slug=tag) + except Tag.DoesNotExist: + return [] + recipes_tagged = TaggedItem.objects.filter(tag=recipe_tag).order_by('-id')[:10] + return [recipe.content_object for recipe in recipes_tagged] + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.info diff --git a/tags/views.py b/tags/views.py index 88d8be84..9ccd340b 100644 --- a/tags/views.py +++ b/tags/views.py @@ -11,4 +11,7 @@ def recipeTags(request, tag): for recipe in recipes_tagged: recipe_list.append(recipe.content_object) - return render_to_response('tags/recipe_tags.html', {'recipe_list': recipe_list}, context_instance=RequestContext(request)) \ No newline at end of file + return render_to_response('tags/recipe_tags.html', { + 'recipe_list': recipe_list, + 'recipe_tag': tag + }, context_instance=RequestContext(request)) diff --git a/templates/tags/recipe_tags.html b/templates/tags/recipe_tags.html index 467441e2..861c22c2 100644 --- a/templates/tags/recipe_tags.html +++ b/templates/tags/recipe_tags.html @@ -5,6 +5,7 @@ {% endblock %} +{% block feeds %}{% endblock %} {% block content %} {% autopaginate recipe_list %}