+# -*- 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')