Initial update: tools page - in progress
[myslice.git] / portal / usersview.py
1 from django.contrib.auth        import authenticate, login, logout
2 from django.template            import RequestContext
3 from django.shortcuts           import render, render_to_response
4
5 from manifoldapi.manifoldresult import ManifoldResult
6 from ui.topmenu                 import topmenu_items, the_user
7 from myslice.configengine       import ConfigEngine
8 from manifold.core.query        import Query
9 from unfold.page                import Page
10 from manifoldapi.manifoldapi    import execute_admin_query
11 from unfold.loginrequired       import LoginRequiredAutoLogoutView
12
13 from myslice.theme import ThemeView
14 import json
15
16 class UsersView (LoginRequiredAutoLogoutView, ThemeView):
17     template_name = 'usersview.html'
18         
19     # expose this so we can mention the backend URL on the welcome page
20     def default_env (self):
21         return { 
22                  'MANIFOLD_URL':ConfigEngine().manifold_url(),
23                  }
24
25     def post (self,request):
26         env = self.default_env()
27         env['theme'] = self.theme
28         return render_to_response(self.template, env, context_instance=RequestContext(request))
29
30     def get (self, request, state=None):
31         env = self.default_env()
32         
33
34         email_list = []
35         status_list = []
36         authority_list = []
37         config={}
38
39         user_query  = Query().get('local:user').select('email','status','config')
40         user_details = execute_admin_query(self.request, user_query)
41
42         for user in user_details:
43             # get email
44             email_list.append(user['email'])
45             # get status
46             if user['status'] == 0:
47                 user_status = 'Disabled'
48             elif user['status'] == 1:
49                 user_status = 'Validation Pending'
50             elif user['status'] == 2:
51                 user_status = 'Enabled'
52             else:
53                 user_status = 'N/A'
54
55             status_list.append(user_status)
56             #get authority
57             if user['config'] is not None:
58                 user_config = json.loads(user['config'])
59                 user_authority = user_config.get('authority','N/A')
60                 authority_list.append(user_authority)
61     
62         user_list = [{'email': t[0], 'status': t[1], 'authority':t[2]}
63             for t in zip(email_list, status_list, authority_list)]
64
65         if request.user.is_authenticated(): 
66             env['person'] = self.request.user
67         else: 
68             env['person'] = None
69     
70         env['theme'] = self.theme
71         env['user_list']= user_list
72
73         env['username']=the_user(request)
74         env['topmenu_items'] = topmenu_items(None, request)
75         if state: env['state'] = state
76         elif not env['username']: env['state'] = None
77         # use one or two columns for the layout - not logged in users will see the login prompt
78         env['layout_1_or_2']="layout-unfold2.html" if not env['username'] else "layout-unfold1.html"
79         
80         
81         return render_to_response(self.template, env, context_instance=RequestContext(request))
82