1 # this somehow is not used anymore - should it not be ?
2 from django.views.generic import View
3 from django.core.context_processors import csrf
4 from django.http import HttpResponseRedirect
5 from django.contrib.auth import authenticate, login, logout
6 from django.template import RequestContext
7 from django.shortcuts import render_to_response
9 from manifold.manifoldresult import ManifoldResult
10 from myslice.viewutils import topmenu_items, the_user
11 from myslice.config import Config
13 class HomeView (View):
15 # expose this so we can mention the backend URL on the welcome page
16 def default_env (self):
18 'manifold_url':Config.manifold_url,
21 def post (self,request):
22 env = self.default_env()
23 username = request.POST.get('username')
24 password = request.POST.get('password')
26 # pass request within the token, so manifold session key can be attached to the request session.
27 token = {'username': username, 'password': password, 'request': request}
29 # our authenticate function returns either
30 # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
31 # . a django User in case of success
32 # . or None if the backend could be reached but the authentication failed
33 auth_result = authenticate(token=token)
34 # high-level errors, like connection refused or the like
35 if isinstance (auth_result, ManifoldResult):
36 manifoldresult = auth_result
37 # let's use ManifoldResult.__repr__
38 env['state']="%s"%manifoldresult
39 return render_to_response('home-view.html',env, context_instance=RequestContext(request))
40 # user was authenticated at the backend
41 elif auth_result is not None:
46 return HttpResponseRedirect ('/login-ok')
48 env['state'] = "Your account is not active, please contact the site admin."
49 return render_to_response('home-view.html',env, context_instance=RequestContext(request))
52 env['state'] = "Your username and/or password were incorrect."
53 return render_to_response('home-view.html',env, context_instance=RequestContext(request))
55 # login-ok sets state="Welcome to MySlice" in urls.py
56 def get (self, request, state=None):
57 env = self.default_env()
58 env['username']=the_user(request)
59 env['topmenu_items'] = topmenu_items('', request)
60 if state: env['state'] = state
61 elif not env['username']: env['state'] = "Please sign in"
62 return render_to_response('home-view.html',env, context_instance=RequestContext(request))