RDFa, ftw.
1 # -*- coding: utf-8 -*-
3 from datetime import datetime
4 from StringIO import StringIO
6 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound
7 from django.views.generic.list_detail import object_list, object_detail
8 from django.views.generic.date_based import archive_month as django_archive_month
9 from django.views.generic.simple import direct_to_template
10 from django.contrib.contenttypes.models import ContentType
11 from django.shortcuts import get_object_or_404
12 from django.views.decorators.cache import cache_page
14 from journal.models import Post
15 from bistrot.models import Thought
16 from tagging.models import Tag, TaggedItem
19 def view_tag(request, tags):
20 tag_list = [get_object_or_404(Tag, name=tag) for tag in tags.split(',')] 21 post_list = TaggedItem.objects.get_by_model(Post, tag_list)\
22 .filter(is_online=True, publication_date__lte=datetime.now())
23 if len(post_list) == 1:
24 return HttpResponseRedirect(post_list[0].get_absolute_url())
26 ctype = ContentType.objects.get_for_model(Post)
27 related_tags = Tag.objects.filter(items__object_id__in=[s.id for s in post_list]).filter(items__content_type__pk=ctype.id).exclude(items__tag__in=tag_list).distinct()
28 return object_list(request,
30 template_name='journal/post_list_for_tag.html',
31 extra_context={ 'tag_list': tag_list, 'related_tags': related_tags }, 32 template_object_name='post')
34 def add_tag(request, tags, tag):
35 """ Allow tag addition with a plus in url, useful for chained tags.
39 /django/+python redirect to /django,python/
41 if tag not in tags and tag.startswith('+'): 42 tags = tags.split(',') + [tag[1:].lower()] 45 return HttpResponseRedirect('/%s/' % (tags,)) 47 def redirect_to_archives(request, year):
48 return HttpResponseRedirect('/archives/?highlight=%s' % year) 50 def archives(request):
51 queryset = Post.published.all()
52 date_list = queryset.dates('publication_date', 'year')[::-1] 53 return direct_to_template(request, 'static/archives.html',
56 'thought_list':Thought.published.all(),
57 'date_list': date_list,
62 def archive_month(request, year, month):
65 date = datetime.date(*time.strptime(year+month, '%Y%m')[:3])
67 # Calculate first and last day of month, for use in a date-range lookup.
68 first_day = date.replace(day=1)
69 if first_day.month == 12:
70 last_day = first_day.replace(year=first_day.year + 1, month=1)
72 last_day = first_day.replace(month=first_day.month + 1)
73 thought_list = Thought.published.filter(publication_date__range=(first_day, last_day))
77 return django_archive_month(request, year, month, Post.published.all(),
79 template_object_name = 'post',
82 extra_context={ 'thought_list': thought_list } 85 def post_detail(request, queryset, template_object_name, slug_field, tags, slug):
86 return object_detail(request,
89 slug_field=slug_field,
90 template_object_name=template_object_name)
93 def post_data(request, queryset, template_object_name, slug_field, tags, slug):
94 response = post_detail(request, queryset, template_object_name, slug_field, tags, slug)
97 post = Post.objects.get(slug=slug)
98 #from pyRdfa import _process, Options
99 #result = _process(StringIO(response.content), post.get_complete_url(), "pretty-xml", Options(), local=True)
101 # tmp fix, I need to find how to install rdflib on debian etch...
103 result = urllib.urlopen('http://www.w3.org/2007/08/pyRdfa/extract?uri=%s' % post.get_complete_url()).read() 105 return HttpResponse(result)
106 except: # TODO: find a better way to handle that