b9d039a55870df47702ef561803b89fe535d93e9
[myslice.git] / portal / homeview.py
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
8
9 from manifold.manifoldresult import ManifoldResult
10 from ui.topmenu import topmenu_items, the_user
11 from myslice.config import Config
12
13 class HomeView (View):
14
15     # expose this so we can mention the backend URL on the welcome page
16     def default_env (self):
17         return { 
18                  'MANIFOLD_URL':Config.manifold_url(),
19                  }
20
21     def post (self,request):
22         env = self.default_env()
23         username = request.POST.get('username')
24         password = request.POST.get('password')
25         
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}    
28
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:
42             user=auth_result
43             if user.is_active:
44                 print "LOGGING IN"
45                 login(request, user)
46                 return HttpResponseRedirect ('/login-ok')
47             else:
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))
50         # otherwise
51         else:
52             env['state'] = "Your username and/or password were incorrect."
53             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
54
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))
63