From 66be47e496b4506b2736104dc1afe2a699a27bc8 Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Thu, 6 Jun 2013 00:22:14 +0200 Subject: [PATCH] =?UTF-8?q?[attributs]=20m=C3=A9thode=20de=20conversion=20?= =?UTF-8?q?en=20datetime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parce que ça fait plusieurs fois que j'en ai besoin donc je pense que ça sera handy ! --- attributs.py | 15 +++++++++++++++ objets.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/attributs.py b/attributs.py index 962be90..6da0033 100644 --- a/attributs.py +++ b/attributs.py @@ -39,6 +39,7 @@ import re import sys import netaddr import time +import datetime import functools import smtplib import random @@ -1010,12 +1011,26 @@ class blacklist(Attr): @crans_attribute class historique(Attr): + """Un historique est usuellement de la forme JJ/MM/AAAA HH:mm:ss, action comm""" + singlevalue = False optional = True legend = u"Historique de l'objet" category = 'info' ldap_name = "historique" + # Thanks to 20-100, on va devoir gérer deux cas + FORMAT = "%d/%m/%Y %H:%M:%S" + FORMAT_OLD = "%d/%m/%Y %H:%M" # ancien binding + + def get_datetime(self): + """Renvoie un objet datetime de la ligne correspondante""" + datepart = self.value.split(',',1)[0] + try: + return datetime.datetime.strptime(self.FORMAT, datepart) + except ValueError: + return datetime.datetime.strptime(self.FORMAT_OLD, datepart) + @crans_attribute class info(Attr): singlevalue = False diff --git a/objets.py b/objets.py index 4ea231c..c70f9ae 100644 --- a/objets.py +++ b/objets.py @@ -166,7 +166,7 @@ class CransLdapObject(object): assert isinstance(login, unicode) assert isinstance(chain, unicode) - new_line = u"%s, %s : %s" % (time.strftime("%d/%m/%Y %H:%M:%S"), login, chain) + new_line = u"%s, %s : %s" % (time.strftime(attributs.historique.FORMAT), login, chain) # Attention, le __setitem__ est surchargé, mais pas .append sur l'historique self["historique"] = self.get("historique", []) + [new_line]