# -*- coding: utf-8 -*-
#
# Copyright (c) 2007-2012 Peter Kuma

import os

from django.http import Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils import translation
from django.http import HttpResponseRedirect
from django.conf import settings
from django.shortcuts import get_object_or_404

from main.models import Page


def page(request, category_name, name):
	p = get_object_or_404(Page, category__name=category_name, name=name)
	return render_to_response('main/' + p.category.template_name, {
					'page': p,
					'category': p.category,
					'style': p.style
	}, RequestContext(request))


def attachment(request, category_name, page_name, name):
	p = get_object_or_404(Page,
		category__name=category_name,
		name=page_name
	)
	for a in p.attachments.all() | p.category.attachments.all():
		if os.path.basename(a.file.name) == name:
			return HttpResponseRedirect(a.file.url)
	raise Http404

@@ -377,16 +377,26 @@
 s import
+ Category,
  Page%0A%0A%0A
@@ -717,32 +717,229 @@
 ge_name, name):%0A
+%09if page_name == '':%0A%09%09c = get_object_or_404(Category, name=category_name)%0A%09%09for a in c.attachments.all():%0A%09%09%09if os.path.basename(a.file.name) == name:%0A%09%09%09%09return HttpResponseRedirect(a.file.url)%0A%0A
 %09p = get_object_
@@ -951,16 +951,16 @@
 4(Page,%0A
-
 %09%09catego
@@ -1003,16 +1003,17 @@
 name%0A%09)%0A
+%0A
 %09for a i
@@ -1037,39 +1037,8 @@
 ll()
- %7C p.category.attachments.all()
 :%0A%09%09

Fix category attachments