]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/views.py
fix: quick and dirty update to django 4.2
[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, slug=None, folders=None):
8     if folders == None and slug == None:
9         raise Http404()
10
11     folder = None
12
13     if folders != None:
14         folders = folders.split('/')
15         resfolder = None
16         for folder in folders:
17             try:
18                 if resfolder:
19                     resfolder = Folder.objects.get(slug__exact=folder, parent__exact=resfolder)
20                 else:
21                     resfolder = Folder.objects.get(slug__exact=folder, parent__isnull=True)
22                 resfolder = resfolder.id
23             except:
24                 raise Http404
25         try:
26             folder = Folder.objects.get(pk=resfolder)
27         except:
28             raise Http404
29
30     if folder != None:
31         try:
32             if not request.user.is_authenticated:
33                 doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__exact=folder)
34             else:
35
36                 doc = Document.objects.get(slug__exact=slug, folder__exact=folder)
37         except:
38             try:
39                 folder = Folder.objects.get(slug__exact=slug, parent__exact=folder)
40                 if not request.user.is_authenticated():
41                     doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
42                 else:
43                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
44             except:
45                 raise Http404
46     else:
47         try:
48             if not request.user.is_authenticated:
49                 doc = Document.objects.get(slug__exact=slug, islive__exact=True, folder__isnull=True)
50             else:
51                 doc = Document.objects.get(slug__exact=slug, folder__isnull=True)
52         except:
53             sys.stderr.write("Unexpected error:", sys.exc_info()[0])
54             try:
55                 folder = Folder.objects.get(slug__exact=slug, parent__isnull=True)
56                 if not request.user.is_authenticated():
57                     doc = Document.objects.get(slug__exact='index', islive__exact=True, folder__exact=folder)
58                 else:
59                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
60             except:
61                 raise Http404
62     template_name = "bpcms/%s.html" %(doc.format,)
63     c = {
64             "content"   : doc.content,
65             "title"     : doc.title,
66             "extracss"  : doc.extracss.all(),
67         }
68     return render(request, template_name, c)
69
70 def css_view(request, slug):
71     cssdoc = CSSDocument.objects.get(slug=slug)
72     if not cssdoc:
73         raise Http404()
74
75     return HttpResponse(cssdoc.content, content_type="text/css")
76