]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/views.py
bde19cc3498e541c6e1ed75a0ca6aea9d5bdb931
[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             if not request.user.is_authenticated():
31                 doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__exact=folder)
32             else:
33
34                 doc = Document.objects.get(slug__exact=slug, folder__exact=folder)
35         except:
36             try:
37                 folder = Folder.objects.get(slug__exact=slug, parent__exact=folder)
38                 if not request.user.isauthenticated():
39                     doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
40                 else:
41                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
42             except:
43                 raise Http404
44     else:
45         try:
46             if not request.user.is_authenticated():
47                 doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__isnull=True)
48             else:
49                 doc = Document.objects.get(slug__exact=slug, folder__isnull=True)
50         except:
51             try:
52                 folder = Folder.objects.get(slug__exact=slug, parent__isnull=True)
53                 if not request.user.is_authenticated():
54                     doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
55                 else:
56                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
57             except:
58                 raise Http404
59     template_name = "bpcms/%s.html" %(doc.format,)
60     t = loader.get_template(template_name)
61     c = RequestContext(request,
62         {
63             "content"   : doc.content,
64             "title"     : doc.title,
65         })
66     return HttpResponse(t.render(c))