]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/content/views.py
initial code for www.sommitrealweird.co.uk
[sommitrealweird.git] / sommitrealweird / content / views.py
1 from content.models import Document, Folder
2 from django.http import Http404, HttpResponse
3 from django.template import RequestContext, loader
4 from settings import MEDIA_URL
5
6 def document_view(request, slug=None, folders=None):
7     if folders == None and slug == None:
8         raise Http404()
9
10     folder = None
11
12     if folders != None:
13         folders = folders.split('/')
14         resfolder = None
15         for folder in folders:
16             try:
17                 if resfolder:
18                     resfolder = Folder.objects.get(slug__exact=folder, parent__exact=resfolder)
19                 else:
20                     resfolder = Folder.objects.get(slug__exact=folder, parent__isnull=True)
21                 resfolder = resfolder.id
22             except:
23                 raise Http404
24         try:
25             folder = Folder.objects.get(pk=resfolder)
26         except:
27             raise Http404
28
29     if folder != None:
30         try:
31             doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__exact=folder)
32         except:
33             try:
34                 folder = Folder.objects.get(slug__exact=slug, parent__exact=folder)
35                 doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
36             except:
37                 raise Http404
38     else:
39         try:
40             doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__isnull=True)
41         except:
42             try:
43                 folder = Folder.objects.get(slug__exact=slug, parent__isnull=True)
44                 doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
45             except:
46                 raise Http404
47     template_name = "%s.html" %(doc.format,)
48     t = loader.get_template(template_name)
49     c = RequestContext(request,
50         {
51             "content"   : doc.content,
52             "title"     : doc.title,
53             "media_url" : MEDIA_URL,
54         })
55     return HttpResponse(t.render(c))