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