]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/views.py
Update for newer django
[sommitrealweird.git] / sommitrealweird / bpcms / views.py
1 from bpcms.models import CSSDocument, Document, Folder
2 from django.http import Http404, HttpResponse
3 from django.shortcuts import render
4 from django.template import RequestContext, loader
5 import sys
6
7 def document_view(request, path = None):
8
9     if path == None:
10         raise Http404()
11
12     folders = path.split("/")
13     slug = None
14     folder = None
15
16     if folders != None:
17         resfolder = None
18         prevfolder = None
19         for i,folder_name in enumerate(folders):
20             try:
21                 if resfolder:
22                     resfolder = Folder.objects.get(slug__exact=folder_name, parent__exact=resfolder)
23                 else:
24                     resfolder = Folder.objects.get(slug__exact=folder_name, parent__isnull=True)
25                 resfolder = resfolder.id
26             except:
27                 slug = folders[i]
28                 folders = folders[:i]
29                 break
30         try:
31             if resfolder != None:
32                 folder = Folder.objects.get(pk=resfolder)
33         except:
34             raise Http404("Badgers")
35
36     if slug == None:
37         slug = "index"
38
39     if folder != None:
40         try:
41             if not request.user.is_authenticated:
42                 doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__exact=folder)
43             else:
44
45                 doc = Document.objects.get(slug__exact=slug, folder__exact=folder)
46         except:
47             try:
48                 folder = Folder.objects.get(slug__exact=slug, parent__exact=folder)
49                 if not request.user.is_authenticated():
50                     doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
51                 else:
52                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
53             except:
54                 raise Http404(folder + "/" + slug)
55     else:
56         try:
57             if not request.user.is_authenticated:
58                 doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__isnull=True)
59             else:
60                 doc = Document.objects.get(slug__exact=slug, folder__isnull=True)
61         except:
62             try:
63                 folder = Folder.objects.get(slug__exact=slug, parent__isnull=True)
64                 if not request.user.is_authenticated():
65                     doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
66                 else:
67                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
68             except:
69                 raise Http404(slug)
70     template_name = "bpcms/%s.html" %(doc.format,)
71     c = {
72             "content"   : doc.content,
73             "title"     : doc.title,
74             "extracss"  : doc.extracss.all(),
75         }
76     return render(request, template_name, c)
77
78 def css_view(request, slug):
79     cssdoc = CSSDocument.objects.get(slug=slug)
80     if not cssdoc:
81         raise Http404()
82
83     return HttpResponse(cssdoc.content, content_type="text/css")
84