Added license notices
[bcms.git] / pages / views.py
blobe0a91459b0233ba52ff7a1199c82c9dd67c50ee6
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 genshi.template import TemplateLoader
34 from models import *
36 loader = TemplateLoader(settings.TEMPLATE_DIRS[0], auto_reload=True)
38 def logmeout(request):
39 logout(request)
40 if request.GET.has_key('redir'):
41 return HttpResponseRedirect(request.GET['redir'])
42 return HttpResponseRedirect("/")
44 def logme(request):
45 if request.method == 'POST':
46 username = request.POST['username']
47 passwd = request.POST['passwd']
48 user = authenticate(username=username, password=passwd)
49 if user is not None:
50 login(request, user)
51 if request.GET.has_key('redir'):
52 return HttpResponseRedirect(request.GET['redir'])
53 return HttpResponseRedirect("/")
54 else:
55 msg = "Username or password incorrect, please try again."
56 msg = "Login"
57 # Load the template
58 template = loader.load('login.html')
60 # Parse and execute the template with given variables
61 stream = template.generate(msg=msg, user=request.user)
63 # Return rendered template as HTML
64 return HttpResponse(stream.render('html', doctype='html'))
66 def editPage(request, pagename):
67 if not request.user.is_authenticated():
68 return HttpResponseRedirect("/login?redir=/p/edit/"+pagename)
69 if request.method == 'POST':
70 title = request.POST['title']
71 contents = request.POST['contents']
72 print (title, contents)
73 try:
74 pg = Page.objects.get(name__iexact = pagename)
75 except:
76 pg = Page(name=pagename, published=datetime.datetime.now())
77 pg.save()
78 p = pg.pagerevision_set.create(title=title, contents=contents, author=0, published=datetime.datetime.now())
79 p.save()
80 return HttpResponseRedirect("/p/" + pagename)
82 try:
83 pg = Page.objects.get(name__iexact = pagename)
84 view = pg.pagerevision_set.order_by("-published")[0]
85 contents = view.contents
86 title = view.title
87 except:
88 title = pagename
89 contents = ''
91 ptitle = 'Editing: ' + pagename
92 # Load the template
93 template = loader.load('edit.html')
95 # Parse and execute the template with given variables
96 stream = template.generate(contents=contents, title=title, ptitle=ptitle, pagename=pagename, user=request.user)
98 # Return rendered template as HTML
99 return HttpResponse(stream.render('html', doctype='html'))
101 def viewPage(request, pagename):
102 try:
103 pg = Page.objects.get(name__iexact = pagename)
104 view = pg.pagerevision_set.order_by("-published")[0]
105 except:
106 return HttpResponse("Page not Found!")
108 # Load the template
109 template = loader.load('view.html')
111 # Parse and execute the template with given variables
112 stream = template.generate(title = view.title, contents = view.contents, pagename = pg.name, user=request.user)
114 # Return rendered template as HTML
115 return HttpResponse(stream.render('html', doctype='html'))