diff --git a/pinry/core/feeds.py b/pinry/core/feeds.py new file mode 100644 index 0000000..0340373 --- /dev/null +++ b/pinry/core/feeds.py @@ -0,0 +1,55 @@ +from __future__ import unicode_literals + +from django.contrib.syndication.views import Feed +from django.contrib.sites.models import get_current_site +from django.shortcuts import get_object_or_404 + +from django_images.models import Thumbnail + +from .models import Pin + + +def filter_generator_for(size): + def wrapped_func(obj): + return Thumbnail.objects.get_or_create_at_size(obj.pk, size) + return wrapped_func + + +class LatestPins(Feed): + title = 'Latest Pins' + link = '/' + description = 'The latest pins from around the internet.' + + domain_name = None + item_enclosure_mime_type = 'image/jpeg' + + def get_object(self, request): + """ + Doing this as a fix for Django's not including the domain name in enclosure urls. + """ + request_type = 'http' + if request.is_secure(): request_type = 'https' + self.domain_name = ''.join([request_type, '://', get_current_site(request).domain]) + return get_object_or_404(Pin) + + def items(self): + return Pin.objects.order_by('-published')[:15] + + def item_pubdate(self, item): + return item.published + + def item_link(self, item): + return item.url + + def item_title(self, item): + return item.url + + def item_description(self, item): + tags = ', '.join(tag.name for tag in item.tags.all()) + return ''.join(['Description: ', item.description or 'None', ' | Tags: ', tags or 'None']) + + def item_enclosure_url(self, item): + return self.domain_name + unicode(filter_generator_for('standard')(item.image).image.url) + + def item_enclosure_length(self, item): + return filter_generator_for('standard')(item.image).image.size diff --git a/pinry/core/urls.py b/pinry/core/urls.py index ec1d29c..a428b49 100644 --- a/pinry/core/urls.py +++ b/pinry/core/urls.py @@ -4,6 +4,7 @@ from django.views.generic import TemplateView from tastypie.api import Api from .api import ImageResource, ThumbnailResource, PinResource, UserResource +from .feeds import LatestPins from .views import CreateImage @@ -17,6 +18,8 @@ v1_api.register(UserResource()) urlpatterns = patterns('', url(r'^api/', include(v1_api.urls, namespace='api')), + url(r'feeds/latest-pins/', LatestPins()), + url(r'^pins/pin-form/$', TemplateView.as_view(template_name='core/pin_form.html'), name='pin-form'), url(r'^pins/create-image/$', CreateImage.as_view(), name='create-image'), diff --git a/pinry/templates/base.html b/pinry/templates/base.html index 329994b..770d7bc 100644 --- a/pinry/templates/base.html +++ b/pinry/templates/base.html @@ -5,6 +5,7 @@ +