]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/views.py
Enable CSRF and Comment Moderation
[sommitrealweird.git] / sommitrealweird / bpcms / views.py
1 from bpcms.models import CSSDocument, 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.is_authenticated():
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             "extracss"  : doc.extracss.all(),
66         })
67     return HttpResponse(t.render(c))
68
69 def css_view(request, slug):
70     cssdoc = CSSDocument.objects.get(slug=slug)
71     if not cssdoc:
72         raise Http404()
73
74     return HttpResponse(cssdoc.content, content_type="text/css")
75