activity for users
[unfold.git] / portal / homeview.py
index 029dc31..7af4776 100644 (file)
@@ -5,6 +5,7 @@ from django.contrib.auth import authenticate, login, logout
 from django.template import RequestContext
 from django.shortcuts import render_to_response
 from django.shortcuts import render
+
 import json
 
 from unfold.loginrequired import FreeAccessView
@@ -18,6 +19,8 @@ from myslice.configengine import ConfigEngine
 
 from myslice.theme import ThemeView
 
+import activity.user
+
 class HomeView (FreeAccessView, ThemeView):
     template_name = 'home-view.html'
         
@@ -62,6 +65,9 @@ class HomeView (FreeAccessView, ThemeView):
                     env['person'] = self.request.user
                     env['username'] = self.request.user
                     
+                    # log user activity
+                    activity.user.login(self.request)
+                    
                     ## check user is pi or not
                     platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
                     account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
@@ -75,7 +81,7 @@ class HomeView (FreeAccessView, ThemeView):
                                     if 'myslice' in platform_detail['platform']:
                                         acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
                     # assigning values
-                    if acc_auth_cred == {}:
+                    if acc_auth_cred=={} or acc_auth_cred=='N/A':
                         pi = "is_not_pi"
                     else:
                         pi = "is_pi"
@@ -85,33 +91,40 @@ class HomeView (FreeAccessView, ThemeView):
                     env['person'] = None
                 return render_to_response(self.template,env, context_instance=RequestContext(request))
             else:
+                # log user activity
+                activity.user.login(self.request, "notactive")
                 env['state'] = "Your account is not active, please contact the site admin."
                 env['layout_1_or_2']="layout-unfold2.html"
                 
                 return render_to_response(self.template,env, context_instance=RequestContext(request))
         # otherwise
         else:
+            # log user activity
+            activity.user.login(self.request, "error")
             env['state'] = "Your username and/or password were incorrect."
             
             return render_to_response(self.template, env, context_instance=RequestContext(request))
 
     def get (self, request, state=None):
         env = self.default_env()
+        acc_auth_cred={}
         if request.user.is_authenticated():
             ## check user is pi or not
             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
+            # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
             platform_details = execute_query(self.request, platform_query)
             account_details = execute_query(self.request, account_query)
             for platform_detail in platform_details:
                 for account_detail in account_details:
-                    if platform_detail['platform_id'] == account_detail['platform_id']:
-                        if 'config' in account_detail and account_detail['config'] is not '':
-                            account_config = json.loads(account_detail['config'])
-                            if 'myslice' in platform_detail['platform']:
-                                acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
+                    if 'platform_id' in platform_detail:
+                        if platform_detail['platform_id'] == account_detail['platform_id']:
+                            if 'config' in account_detail and account_detail['config'] is not '':
+                                account_config = json.loads(account_detail['config'])
+                                if 'myslice' in platform_detail['platform']:
+                                    acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
             # assigning values
-            if acc_auth_cred == {}:
+            if acc_auth_cred=={} or acc_auth_cred=='N/A':
                 pi = "is_not_pi"
             else:
                 pi = "is_pi"