use logger instead of print as often as possible
[myslice.git] / portal / actions.py
index e1faf4b..7d57b9d 100644 (file)
@@ -1,5 +1,3 @@
-from __future__ import print_function
-
 from django.http                    import HttpResponse
 from manifold.core.query            import Query
 from manifoldapi.manifoldapi        import execute_query,execute_admin_query
@@ -16,7 +14,7 @@ from django.core.mail               import EmailMultiAlternatives, send_mail
 
 from myslice.theme                  import ThemeView
 from myslice.configengine           import ConfigEngine
-
+from myslice.settings               import logger
 
 theme = ThemeView()
 
@@ -83,8 +81,8 @@ def authority_check_pis(request, user_email):
             pi_status = True
         return pi_status
 
-    except Exception,e:
-        print("Exception in actions.py in authority_check_pis %s" % e)
+    except Exception as e:
+        logger.error("Exception in actions.py in authority_check_pis {}".format(e))
         return None
 
 
@@ -100,8 +98,8 @@ def authority_add_pis(request, authority_hrn,user_hrn):
         results = execute_query(request,query)
         newpis = authority_get_pis (request, authority_hrn)
         return newpis
-    except Exception,e: 
-        print("Exception in actions.py in authority_add_pis %s" % e)
+    except Exception as e: 
+        logger.error("Exception in actions.py in authority_add_pis {}".format(e))
         return None
 
 
@@ -117,14 +115,14 @@ def authority_remove_pis(request, authority_hrn,user_hrn):
         results = execute_query(request,query)
         newpis = authority_get_pis (request, authority_hrn)
         return newpis
-    except Exception,e: 
-        print("Exception in actions.py in authority_remove_pis %s" % e)
+    except Exception as e: 
+        logger.error("Exception in actions.py in authority_remove_pis {}".format(e))
         return None
 
 
 def authority_get_pi_emails(request, authority_hrn):
     pi_users = authority_get_pis(request,authority_hrn)
-    print("pi_users = %s" % pi_users)
+    logger.info("pi_users = %s" % pi_users)
 
     if any(pi['pi_users'] == None or not pi['pi_users']  for pi in pi_users):
         #theme.template_name = 'email_default_recipients.txt' 
@@ -176,13 +174,13 @@ def clear_user_creds(request, user_email):
                         else:
                             return None
 
-    except Exception,e:
-        print("Exception in actions.py in clear_user_creds %s" % e)
+    except Exception as e:
+        logger.error("Exception in actions.py in clear_user_creds {}".format(e))
         return None
 
 def is_pi(wsgi_request, user_hrn, authority_hrn):
     # authorities from user where user_hrn == "ple.upmc.jordan_auge"
-    print("#### actions.py is_pi authority_hrn = ", authority_hrn)
+    logger.debug("#### actions.py is_pi authority_hrn = {}".format(authority_hrn))
     try:
         # CACHE PB with fields
         page = Page(wsgi_request)
@@ -194,12 +192,11 @@ def is_pi(wsgi_request, user_hrn, authority_hrn):
         query  = Query().get('myslice:user').select(user_fields).filter_by('user_hrn','==',user_hrn)
         #query = Query.get('myslice:user').filter_by('user_hrn', '==', user_hrn).select('pi_authorities')
         results = execute_query(wsgi_request, query)
-        #print "is_pi results = ", results
         for user_detail in results:
             if authority_hrn in user_detail['pi_authorities']:
                 return True
-    except Exception,e:
-        print("Exception in actions.py in is_pi %s" % e)
+    except Exception as e:
+        logger.error("Exception in actions.py in is_pi {}".format(e))
     return False
     
 # SFA get record
@@ -226,7 +223,7 @@ def sfa_add_authority(request, authority_params):
     # REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
     query = Query.create('myslice:authority').set(authority_params).select('authority_hrn')
     results = execute_query(request, query)
-    print("sfa_add_auth results=",results)
+    logger.info("sfa_add_auth results={}".format(results))
     if not results:
         raise Exception, "Could not create %s. Already exists ?" % authority_params['hrn']
     return results
@@ -432,7 +429,7 @@ def make_request_authority(authority):
     return request
 
 def make_requests(pending_users, pending_slices, pending_authorities, pending_projects, pending_joins):
-    print("$$$$$$$$$$$$$$$  make_request")
+    logger.info("$$$$$$$$$$$$$$$  make_request")
     requests = []
     for user in pending_users:
         requests.append(make_request_user(user))
@@ -447,7 +444,7 @@ def make_requests(pending_users, pending_slices, pending_authorities, pending_pr
     return requests   
 
 def get_request_by_id(ids):
-    print("$$$$$$$$$$$$$$$$  get_request_by_id")
+    logger.info("$$$$$$$$$$$$$$$$  get_request_by_id")
     sorted_ids = { 'user': [], 'slice': [], 'authority': [], 'project': [], 'join': [] }
     for type__id in ids:
         type, id = type__id.split('__')
@@ -469,7 +466,7 @@ def get_request_by_id(ids):
     return make_requests(pending_users, pending_slices, pending_authorities, pending_projects, pending_joins)
 
 def get_requests(authority_hrns=None):
-    print("$$$$$$$$$$$$$   get_request_by_authority auth_hrns = ", authority_hrns)
+    logger.info("$$$$$$$$$$$$$   get_request_by_authority auth_hrns = {}".format(authority_hrns))
     if not authority_hrns:
         ## get those pending users who have confirmed their emails
         pending_users  = PendingUser.objects.filter(status__iexact = 'True')
@@ -576,7 +573,7 @@ def portal_validate_request(wsgi_request, request_ids):
                     #'pi'        : None,
                     #'enabled'    : True
                 }
-                print("ADD Authority")
+                logger.info("ADD Authority")
                 sfa_add_authority(wsgi_request, sfa_authority_params)
                 request_status['SFA authority'] = {'status': True }
                 PendingAuthority.objects.get(id=request['id']).delete()
@@ -689,8 +686,8 @@ def portal_reject_request(wsgi_request, request_ids):
                     msg = EmailMultiAlternatives(subject, text_content, sender, [user_email])
                     msg.attach_alternative(html_content, "text/html")
                     msg.send()
-                except Exception, e:
-                    print("Failed to send email, please check the mail templates and the SMTP configuration of your server")   
+                except Exception as e:
+                    logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")   
 
                 # removing from Django portal_pendinguser
                 PendingUser.objects.get(id=request['id']).delete()
@@ -730,8 +727,8 @@ def portal_reject_request(wsgi_request, request_ids):
                 msg = EmailMultiAlternatives(subject, text_content, sender, [user_email])
                 msg.attach_alternative(html_content, "text/html")
                 msg.send()
-            except Exception, e:
-                print("Failed to send email, please check the mail templates and the SMTP configuration of your server")
+            except Exception as e:
+                logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
                       
             PendingSlice.objects.get(id=request['id']).delete()
 
@@ -769,8 +766,8 @@ def portal_reject_request(wsgi_request, request_ids):
                 msg = EmailMultiAlternatives(subject, text_content, sender, [user_email])
                 msg.attach_alternative(html_content, "text/html")
                 msg.send()
-            except Exception, e:
-                print("Failed to send email, please check the mail templates and the SMTP configuration of your server")
+            except Exception as e:
+                logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
 
             PendingAuthority.objects.get(id=request['id']).delete()
 
@@ -868,8 +865,8 @@ def create_slice(wsgi_request, request):
             msg = EmailMultiAlternatives(subject, text_content, sender, [user_email])
             msg.attach_alternative(html_content, "text/html")
             msg.send()
-        except Exception, e:
-            print("Failed to send email, please check the mail templates and the SMTP configuration of your server")
+        except Exception as e:
+            logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
        
     return results
 
@@ -906,8 +903,8 @@ def create_pending_slice(wsgi_request, request, email):
         msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
         msg.attach_alternative(html_content, "text/html")
         msg.send()
-    except Exception, e:
-        print("Failed to send email, please check the mail templates and the SMTP configuration of your server")
+    except Exception as e:
+        logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
 
 
 def create_pending_project(wsgi_request, request):
@@ -1063,8 +1060,8 @@ def sfa_create_user(wsgi_request, request, namespace = None, as_admin = False):
             msg = EmailMultiAlternatives(subject, text_content, sender, [request['email']])
             msg.attach_alternative(html_content, "text/html")
             msg.send()
-        except Exception, e:
-            print("Failed to send email, please check the mail templates and the SMTP configuration of your server")
+        except Exception as e:
+            logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
 
     return results
 
@@ -1102,7 +1099,7 @@ def iotlab_create_user (wsgi_request, request, namespace = None, as_admin=False)
    
     iotlab_user_params1 = json.dumps(iotlab_user_params)
     r=requests.post(url=URL_REST, data=iotlab_user_params1, headers=headers, auth=auth)
-    print('Create iotlab user : ', r.status_code, r.text)
+    logger.info('Create iotlab user : {} {}'.format(r.status_code, r.text))
     return r.text
 
 def create_user(wsgi_request, request, namespace = None, as_admin = False):
@@ -1198,8 +1195,8 @@ def create_pending_user(wsgi_request, request, user_detail):
             'config'        : json.dumps(account_config),
         }
         manifold_add_account(wsgi_request, account_params)
-    except Exception, e:
-        print("Failed creating manifold account on platform %s for user: %s" % ('myslice', request['email']))
+    except Exception as e:
+        logger.error("Failed creating manifold account on platform {} for user: {}".format('myslice', request['email']))
 
     try:
         # Send an email: the recipients are the PI of the authority
@@ -1224,6 +1221,6 @@ def create_pending_user(wsgi_request, request, user_detail):
         msg.attach_alternative(html_content, "text/html")
         msg.send()
     except Exception, e:
-        print("Failed to send email, please check the mail templates and the SMTP configuration of your server")
+        logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
         import traceback
-        traceback.print_exc()
+        logger.error(traceback.format_exc())