def logout(request): """simply logout the current user""" response = HttpResponse(mimetype='application/json') if request.user.is_authenticated(): username = request.user.username auth.logout(request) message = JSONResponse(success=True, message="Logout successful") response.write(message.json()) logger.info("User %s logged out" % username) else: message = JSONResponse(success=False, message="You must login before you logout") response.write(message.json()) logger.error("User tried to logout but wasn't logged in.") return response
def login(request): """Allow a user to login Used HTTP POST variables: username - the username password - the password """ response = HttpResponse(mimetype='application/json') auth.logout(request); if 'username' not in request.POST or 'password' not in request.POST: message = JSONResponse(success=False, message="Missing POST parameter!") response.write(message.json()) logger.error("Invalid login: Username or password not set.") return response user = auth.authenticate(username=request.POST['username'], password=request.POST['password']) if user is None: message = JSONResponse(success=False, message="Login failed!") response.write(message.json()); logger.warning("User %s tried to login, but login failed (wrong password or account does not exists)" % request.POST['username']) return response else: if user.is_active: auth.login(request, user) message = JSONResponse(success=True, message="Login successful") response.write(message.json()) logger.info("Login successful for user %s" % user) return response else: message = JSONResponse(success=False, message="User not active! Activate your account first") response.write(message.json()) logger.warning("User %s tried to login, but is not activated yet!" % user) return response