[attributs] méthode de conversion en datetime
Parce que ça fait plusieurs fois que j'en ai besoin donc je pense que ça sera handy !
This commit is contained in:
parent
00a8e001e1
commit
66be47e496
2 changed files with 16 additions and 1 deletions
15
attributs.py
15
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
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue