journal/views.py
author David Larlet <larlet@gmail.com>
Thu Apr 24 16:11:11 2008 +0200 (4 months ago)
changeset 11 fa5f8ccedebe
parent 8901522d71da1
child 124bccf4550b43
permissions -rw-r--r--
RDFa, ftw.
     1 # -*- coding: utf-8 -*-
     2 import time
     3 from datetime import datetime
     4 from StringIO import StringIO
     5 
     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
    13 
    14 from journal.models import Post
    15 from bistrot.models import Thought
    16 from tagging.models import Tag, TaggedItem
    17 
    18 
    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())
    25     else:
    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,
    29             queryset=post_list,
    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')
    33 
    34 def add_tag(request, tags, tag):
    35     """ Allow tag addition with a plus in url, useful for chained tags.
    36 
    37     Usage::
    38 
    39         /django/+python redirect to /django,python/
    40     """
    41     if tag not in tags and tag.startswith('+'):
    42         tags = tags.split(',') + [tag[1:].lower()]
    43         tags.sort()
    44         tags = ','.join(tags)
    45     return HttpResponseRedirect('/%s/' % (tags,))
    46 
    47 def redirect_to_archives(request, year):
    48     return HttpResponseRedirect('/archives/?highlight=%s' % year)
    49 
    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',
    54         {
    55             'post_list':queryset,
    56             'thought_list':Thought.published.all(),
    57             'date_list': date_list,
    58 
    59         }
    60     )
    61 
    62 def archive_month(request, year, month):
    63     try:
    64         import datetime
    65         date = datetime.date(*time.strptime(year+month, '%Y%m')[:3])
    66 
    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)
    71         else:
    72             last_day = first_day.replace(month=first_day.month + 1)
    73         thought_list = Thought.published.filter(publication_date__range=(first_day, last_day))
    74     except ValueError:
    75         thought_list = None
    76 
    77     return django_archive_month(request, year, month, Post.published.all(),
    78         'publication_date',
    79         template_object_name = 'post',
    80         month_format = '%m',
    81         allow_empty = True,
    82         extra_context={ 'thought_list': thought_list }
    83     )
    84 
    85 def post_detail(request, queryset, template_object_name, slug_field, tags, slug):
    86     return object_detail(request,
    87         queryset=queryset,
    88         slug=slug,
    89         slug_field=slug_field,
    90         template_object_name=template_object_name)
    91 
    92 @cache_page(60*30)
    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)
    95 
    96     try:
    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)
   100         
   101         # tmp fix, I need to find how to install rdflib on debian etch...
   102         import urllib
   103         result = urllib.urlopen('http://www.w3.org/2007/08/pyRdfa/extract?uri=%s' % post.get_complete_url()).read()
   104         
   105         return HttpResponse(result)
   106     except: # TODO: find a better way to handle that
   107         return response