adding login/logout stuff
authorLincoln de Sousa <lincoln@minaslivre.org>
Tue, 1 Jul 2008 01:45:09 +0000 (22:45 -0300)
committerLincoln de Sousa <lincoln@minaslivre.org>
Tue, 1 Jul 2008 01:45:09 +0000 (22:45 -0300)
eventos/urls.py [new file with mode: 0644]
eventos/views.py
urls.py

diff --git a/eventos/urls.py b/eventos/urls.py
new file mode 100644 (file)
index 0000000..ba07a72
--- /dev/null
@@ -0,0 +1,6 @@
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+    (r'^login/', 'eventos.views.login'),
+    (r'^logout/', 'eventos.views.logout'),
+)
index 60f00ef..ce68872 100644 (file)
@@ -1 +1,53 @@
-# Create your views here.
+# -*- coding: utf-8 -*-
+# Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+from django.http import HttpResponseRedirect
+from django.contrib import auth
+from django.contrib.auth.forms import AuthenticationForm
+
+def login(request):
+    """This is a function that will be used as a front-end to the
+    django's login system. It receives username and password fields
+    from a POST request and tries to login the user.
+
+    If login is successful, user will be redirected to the referer
+    address, otherwise will be redirected to /?login_failed.
+    """
+    errors = {}
+    manipulator = AuthenticationForm(request)
+    if request.POST:
+        errors = manipulator.get_validation_errors(request.POST)
+        got_user = manipulator.get_user()
+        if got_user:
+            auth.login(request, got_user)
+            try:
+                request.session.delete_test_cookie()
+            except KeyError:
+                pass
+            return HttpResponseRedirect('/')
+        else:
+            return HttpResponseRedirect('/?login_failed')
+
+    request.session.set_test_cookie()
+    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
+
+def logout(request):
+    """Simple front-end to django's logout stuff. This function should
+    be mapped to an url and simply called without any parameter.
+    """
+    auth.logout(request)
+    return HttpResponseRedirect('/login')
diff --git a/urls.py b/urls.py
index 572767b..7c3de47 100644 (file)
--- a/urls.py
+++ b/urls.py
@@ -2,5 +2,6 @@ from django.conf.urls.defaults import *
 
 urlpatterns = patterns('',
     (r'^admin/', include('django.contrib.admin.urls')),
 
 urlpatterns = patterns('',
     (r'^admin/', include('django.contrib.admin.urls')),
+    (r'^', include('ema.eventos.urls')),
     (r'^', include('diario.urls.entries')),
 )
     (r'^', include('diario.urls.entries')),
 )