added titles to pages
[myslice.git] / portal / homeview.py
1 # this somehow is not used anymore - should it not be ?
2 from django.core.context_processors import csrf
3 from django.http import HttpResponseRedirect
4 from django.contrib.auth import authenticate, login, logout
5 from django.template import RequestContext
6 from django.shortcuts import render_to_response
7 from django.shortcuts import render
8
9 from unfold.loginrequired import FreeAccessView
10
11 from manifoldapi.manifoldresult import ManifoldResult
12 from ui.topmenu import topmenu_items, the_user
13 from myslice.configengine import ConfigEngine
14
15 from myslice.theme import ThemeView
16
17 class HomeView (FreeAccessView, ThemeView):
18     template_name = 'home-view.html'
19         
20     # expose this so we can mention the backend URL on the welcome page
21     def default_env (self):
22         return { 
23                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
24                  }
25
26     def post (self,request):
27         env = self.default_env()
28         env['theme'] = self.theme
29         env['section'] = "Dashboard"
30         
31         username = request.POST.get('username')
32         password = request.POST.get('password')
33         
34         # pass request within the token, so manifold session key can be attached to the request session.
35         token = {'username': username, 'password': password, 'request': request}    
36
37         # our authenticate function returns either
38         # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
39         # . a django User in case of success
40         # . or None if the backend could be reached but the authentication failed
41         auth_result = authenticate(token=token)
42         # use one or two columns for the layout - not logged in users will see the login prompt
43         # high-level errors, like connection refused or the like
44         if isinstance (auth_result, ManifoldResult):
45             manifoldresult = auth_result
46             # let's use ManifoldResult.__repr__
47             env['state']="%s"%manifoldresult
48             
49             return render_to_response(self.template,env, context_instance=RequestContext(request))
50         # user was authenticated at the backend
51         elif auth_result is not None:
52             user=auth_result
53             if user.is_active:
54                 print "LOGGING IN"
55                 login(request, user)
56                 
57                 if request.user.is_authenticated(): 
58                     env['person'] = self.request.user
59                     env['username'] = self.request.user
60                 else: 
61                     env['person'] = None
62                 return render_to_response(self.template,env, context_instance=RequestContext(request))
63             else:
64                 env['state'] = "Your account is not active, please contact the site admin."
65                 env['layout_1_or_2']="layout-unfold2.html"
66                 
67                 return render_to_response(self.template,env, context_instance=RequestContext(request))
68         # otherwise
69         else:
70             env['state'] = "Your username and/or password were incorrect."
71             
72             return render_to_response(self.template, env, context_instance=RequestContext(request))
73
74     def get (self, request, state=None):
75         env = self.default_env()
76
77         if request.user.is_authenticated(): 
78             env['person'] = self.request.user
79         else: 
80             env['person'] = None
81     
82         env['theme'] = self.theme
83         env['section'] = "Dashboard"
84
85         env['username']=the_user(request)
86         env['topmenu_items'] = topmenu_items(None, request)
87         if state: env['state'] = state
88         elif not env['username']: env['state'] = None
89         # use one or two columns for the layout - not logged in users will see the login prompt
90         
91         
92         return render_to_response(self.template, env, context_instance=RequestContext(request))
93