fix broken config, and no more static method for Config (which now is a singleton)
[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         config=Config()
18         return { 
19                  'MANIFOLD_URL':config.manifold_url(),
20                  }
21
22     def post (self,request):
23         env = self.default_env()
24         username = request.POST.get('username')
25         password = request.POST.get('password')
26         
27         # pass request within the token, so manifold session key can be attached to the request session.
28         token = {'username': username, 'password': password, 'request': request}    
29
30         # our authenticate function returns either
31         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
32         # . a django User in case of success
33         # . or None if the backend could be reached but the authentication failed
34         auth_result = authenticate(token=token)
35         # high-level errors, like connection refused or the like
36         if isinstance (auth_result, ManifoldResult):
37             manifoldresult = auth_result
38             # let's use ManifoldResult.__repr__
39             env['state']="%s"%manifoldresult
40             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
41         # user was authenticated at the backend
42         elif auth_result is not None:
43             user=auth_result
44             if user.is_active:
45                 print "LOGGING IN"
46                 login(request, user)
47                 return HttpResponseRedirect ('/login-ok')
48             else:
49                 env['state'] = "Your account is not active, please contact the site admin."
50                 return render_to_response('home-view.html',env, context_instance=RequestContext(request))
51         # otherwise
52         else:
53             env['state'] = "Your username and/or password were incorrect."
54             return render_to_response('home-view.html',env, context_instance=RequestContext(request))
55
56     # login-ok sets state="Welcome to MySlice" in urls.py
57     def get (self, request, state=None):
58         env = self.default_env()
59         env['username']=the_user(request)
60         env['topmenu_items'] = topmenu_items(None, request)
61         if state: env['state'] = state
62         elif not env['username']: env['state'] = "Please sign in"
63         return render_to_response('home-view.html',env, context_instance=RequestContext(request))
64