From b6cd0467c24e0a78ba7bfc3ff047393da9e10d73 Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Tue, 8 Apr 2008 02:21:17 -0700 Subject: [PATCH] Added simple authentication --- pages/views.py | 37 +++++++++++++++++++++++++++++++++---- templates/login.html | 29 +++++++++++++++++++++++++++++ templates/master.html | 10 ++++++++++ templates/view.html | 4 +++- urls.py | 2 ++ 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 templates/login.html diff --git a/pages/views.py b/pages/views.py index 3c5ac09..0567f86 100644 --- a/pages/views.py +++ b/pages/views.py @@ -2,6 +2,7 @@ import datetime from django.http import HttpResponse, HttpResponseRedirect +from django.contrib.auth import authenticate, login, logout from django import newforms as forms from django.conf import settings @@ -10,8 +11,37 @@ from models import * loader = TemplateLoader(settings.TEMPLATE_DIRS[0], auto_reload=True) +def logmeout(request): + logout(request) + if request.GET.has_key('redir'): + return HttpResponseRedirect(request.GET['redir']) + return HttpResponseRedirect("/") + +def logme(request): + if request.method == 'POST': + username = request.POST['username'] + passwd = request.POST['passwd'] + user = authenticate(username=username, password=passwd) + if user is not None: + login(request, user) + if request.GET.has_key('redir'): + return HttpResponseRedirect(request.GET['redir']) + return HttpResponseRedirect("/") + else: + msg = "Username or password incorrect, please try again." + msg = "Login" + # Load the template + template = loader.load('login.html') + + # Parse and execute the template with given variables + stream = template.generate(msg=msg, user=request.user) + + # Return rendered template as HTML + return HttpResponse(stream.render('html', doctype='html')) + def editPage(request, pagename): - print 'editPage' + if not request.user.is_authenticated(): + return HttpResponseRedirect("/login?redir=/p/edit/"+pagename) if request.method == 'POST': title = request.POST['title'] contents = request.POST['contents'] @@ -39,13 +69,12 @@ def editPage(request, pagename): template = loader.load('edit.html') # Parse and execute the template with given variables - stream = template.generate(contents=contents, title=title, ptitle=ptitle, pagename=pagename) + stream = template.generate(contents=contents, title=title, ptitle=ptitle, pagename=pagename, user=request.user) # Return rendered template as HTML return HttpResponse(stream.render('html', doctype='html')) def viewPage(request, pagename): - print 'viewPage' try: pg = Page.objects.get(name__iexact = pagename) view = pg.pagerevision_set.order_by("-published")[0] @@ -56,7 +85,7 @@ def viewPage(request, pagename): template = loader.load('view.html') # Parse and execute the template with given variables - stream = template.generate(title = view.title, contents = view.contents, pagename = pg.name) + stream = template.generate(title = view.title, contents = view.contents, pagename = pg.name, user=request.user) # Return rendered template as HTML return HttpResponse(stream.render('html', doctype='html')) \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..50c3dce --- /dev/null +++ b/templates/login.html @@ -0,0 +1,29 @@ + + + + + Login + + + +

+ +

+ + + + + + + + + + +
Username:
Password:
+
+ + + + diff --git a/templates/master.html b/templates/master.html index 1962c07..7acd3b4 100644 --- a/templates/master.html +++ b/templates/master.html @@ -52,16 +52,26 @@ hr{ Black Chair
Home +
+
+ +
+ + diff --git a/templates/view.html b/templates/view.html index 4eae11e..a83bcd9 100644 --- a/templates/view.html +++ b/templates/view.html @@ -7,7 +7,9 @@ -


+

+Edit +
diff --git a/urls.py b/urls.py index 8d63b34..068fc9a 100644 --- a/urls.py +++ b/urls.py @@ -6,6 +6,8 @@ from django.conf import settings urlpatterns = patterns('', (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': os.path.join(settings.APP_ROOT, 'media')}), (r'^admin/', include('django.contrib.admin.urls')), + (r'^login', 'bcms.pages.views.logme'), + (r'^logout', 'bcms.pages.views.logmeout'), (r'^p/', include('bcms.pages.urls')), (r'^', 'bcms.pages.views.viewPage', {'pagename':'index'}), ) -- 2.11.4.GIT