Fixed servefiles urls
[bcms.git] / pages / views.py
blob7db23052e5955a00f841bebbaf01e2b13a12e1bd
1 # BC CMS - Content Management System
2 # Copyright (c) 2008, Carlos Daniel Ruvalcaba Valenzuela
3 # All rights reserved.
5 # Redistribution and use in source and binary forms, with or without modification,
6 # are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright notice, this
11 # list of conditions and the following disclaimer in the documentation and/or other
12 # materials provided with the distribution.
13 # * Neither the name of the BlackChair Software nor the names of its contributors may be
14 # used to endorse or promote products derived from this software without specific prior
15 # written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 import datetime
28 from django.http import HttpResponse, HttpResponseRedirect
29 from django.contrib.auth import authenticate, login, logout
30 from django import newforms as forms
31 from django.conf import settings
33 from bcms.template import template_django as template
35 from models import *
37 def logmeout(request):
38 logout(request)
39 if request.GET.has_key('redir'):
40 return HttpResponseRedirect(request.GET['redir'])
41 return HttpResponseRedirect("/")
43 def logme(request):
44 if request.method == 'POST':
45 username = request.POST['username']
46 passwd = request.POST['passwd']
47 user = authenticate(username=username, password=passwd)
48 if user is not None:
49 login(request, user)
50 if request.GET.has_key('redir'):
51 return HttpResponseRedirect(request.GET['redir'])
52 return HttpResponseRedirect("/")
53 else:
54 msg = "Username or password incorrect, please try again."
55 msg = "Login"
56 # Load the template
57 template = loader.load('login.html')
59 # Parse and execute the template with given variables
60 stream = template.generate(msg=msg, user=request.user)
62 # Return rendered template as HTML
63 return HttpResponse(stream.render('html', doctype='html'))
65 @template('edit.html')
66 def editPage(request, lang, pagename):
67 if request.method == 'POST':
68 title = request.POST['title']
69 contents = request.POST['contents']
70 print (title, contents)
71 try:
72 pg = Page.objects.filter(name__iexact = pagename, lang__iexact = lang)[0]
73 except:
74 pg = Page(name=pagename, lang=lang, published=datetime.datetime.now())
75 pg.save()
76 p = pg.pagerevision_set.create(title=title, contents=contents, author=0, published=datetime.datetime.now())
77 p.save()
78 return HttpResponseRedirect("/%s/%s" % (lang, pagename))
80 try:
81 pg = Page.objects.get(name__iexact = pagename, lang__iexact = lang)
82 view = pg.pagerevision_set.order_by("-published")[0]
83 contents = view.contents
84 title = view.title
85 except:
86 title = pagename
87 contents = ''
89 ptitle = 'Editing: ' + pagename
90 args = dict(contents=contents, title=title, ptitle=ptitle,
91 pagename=pagename, user=request.user)
92 return args
94 @template('view.html')
95 def viewPage(request, lang, pagename):
96 if not pagename:
97 pagename = 'index'
98 try:
99 pg = Page.objects.filter(name__iexact = pagename, lang__iexact = lang)[0]
100 view = pg.pagerevision_set.order_by("-published")[0]
101 except:
102 return HttpResponse("Requested page (%s) not Found!" % pagename)
104 args = dict(title = view.title, contents = view.contents,
105 pagename = pg.name, lang = lang)
106 return args