commit 4c755abe699f5f5cbf55c5299ed189c7724cee80
Author: Jérôme Schneider <jschneider@entrouvert.com>
Date:   Fri Sep 19 16:24:53 2014 +0200

    backends: add last_connection_date and creation_date to update_or_create
    
    Closes #5542

diff --git a/mandaye/backends/default.py b/mandaye/backends/default.py
index 3c1164e..0caabd1 100644
--- a/mandaye/backends/default.py
+++ b/mandaye/backends/default.py
@@ -1,4 +1,5 @@
 
+from datetime import datetime
 from importlib import import_module
 
 from mandaye import config
@@ -68,8 +69,10 @@ class AssociationExample(object):
         pass
 
     @staticmethod
-    def update_or_create(sp_name, sp_login, sp_post_values, idp_unique_id, idp_name):
+    def update_or_create(sp_name, sp_login, sp_post_values, idp_unique_id,
+            idp_name='default', creation_date=None, last_connection_date=None):
         """ update or create an associtaion which match the following values
+        creation_date and last_connection_date: by default datetime.utcnow()
         return the association id
         """
         pass
diff --git a/mandaye/backends/ldap_back.py b/mandaye/backends/ldap_back.py
index 3033283..6eaaf73 100644
--- a/mandaye/backends/ldap_back.py
+++ b/mandaye/backends/ldap_back.py
@@ -1,10 +1,11 @@
 
-import datetime
 import json
 import ldap
 import ldap.modlist
 import random
 
+from datetime import datetime
+
 from mandaye import config
 from mandaye.log import logger
 from mandaye.backends.default import storage_conn
@@ -33,10 +34,10 @@ class Association(object):
                 'sp_post_values': json.loads(ldap_object['spPostValues'][0]),
                 'idp_unique_id': ldap_object['idpUniqueID'][0],
                 'idp_name': ldap_object['idpName'][0],
-                'last_connection': datetime.datetime.strptime(
+                'last_connection': datetime.strptime(
                     ldap_object['lastConnectionDate'][0][:14],
                     '%Y%m%d%H%M%S'),
-                'creation_date': datetime.datetime.strptime(
+                'creation_date': datetime.strptime(
                     ldap_object['creationDate'][0][:14],
                     '%Y%m%d%H%M%S'),
                 }
@@ -73,7 +74,8 @@ class Association(object):
         return False
 
     @staticmethod
-    def update_or_create(sp_name, sp_login, sp_post_values, idp_unique_id, idp_name='default'):
+    def update_or_create(sp_name, sp_login, sp_post_values, idp_unique_id,
+            idp_name='default', creation_date=None, last_connection_date=None):
         """ update or create an associtaion which match the following values
         return the association id
         """
@@ -81,13 +83,15 @@ class Association(object):
                 filterstr='(&(objectClass=MandayeUser)(spName=%s)(spLogin=%s)(idpUniqueID=%s)(idpName=%s))' %\
                         (sp_name, sp_login, idp_unique_id, idp_name))
         if not results:
+            creation_date = creation_date or datetime.utcnow()
+            last_connection_date = last_connection_date or datetime.utcnow()
             association = {'spName': str(sp_name),
                     'spLogin': str(sp_login),
                     'spPostValues': json.dumps(sp_post_values),
                     'idpUniqueID': str(idp_unique_id),
                     'idpName': str(idp_name),
-                    'creationDate': datetime.datetime.utcnow().strftime('%Y%m%d%H%M%SZ'),
-                    'lastConnectionDate': datetime.datetime.utcnow().strftime('%Y%m%d%H%M%SZ'),
+                    'creationDate': creation_date.strftime('%Y%m%d%H%M%SZ'),
+                    'lastConnectionDate': last_connection_date.strftime('%Y%m%d%H%M%SZ'),
                     'objectClass': 'MandayeUser'
                     }
             mod_list = ldap.modlist.addModlist(association)
@@ -131,7 +135,7 @@ class Association(object):
         """ update the association last connection time with the current time
         return a dict of the association
         """
-        last_connection = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%SZ")
+        last_connection = datetime.utcnow().strftime("%Y%m%d%H%M%SZ")
         dn = "uniqueID=%s,%s" % (asso_id, config.ldap_base_dn)
         mod_list = [(ldap.MOD_REPLACE, 'lastConnectionDate', last_connection)]
         storage_conn.modify_s(dn, mod_list)
diff --git a/mandaye/backends/sql.py b/mandaye/backends/sql.py
index 23844b9..d62f43f 100644
--- a/mandaye/backends/sql.py
+++ b/mandaye/backends/sql.py
@@ -71,7 +71,8 @@ class Association(object):
         return False
 
     @staticmethod
-    def update_or_create(sp_name, sp_login, sp_post_values, idp_unique_id, idp_name='default'):
+    def update_or_create(sp_name, sp_login, sp_post_values, idp_unique_id,
+            idp_name='default', creation_date=None, last_connection_date=None):
         """ update or create an associtaion which match the following values
         return the association id
         """
@@ -105,11 +106,15 @@ class Association(object):
                         idp_id=idp_name
                         )
                 storage_conn.add(idp_user)
+            creation_date = creation_date or datetime.utcnow()
+            last_connection_date = last_connection_date or datetime.utcnow()
             sp_user = SPUser(
                     login=sp_login,
                     post_values=sp_post_values,
                     idp_user=idp_user,
-                    service_provider=service_provider
+                    service_provider=service_provider,
+                    creation_date=creation_date,
+                    last_connection=last_connection_date
                     )
             storage_conn.add(sp_user)
             try:
