Les timestamps c'est merdique, maintenant tout est du datetime

This commit is contained in:
Pierre-Elliott Bécue 2015-10-03 14:09:08 +02:00
parent 4de62cd0a8
commit a1f27f9f1a
3 changed files with 64 additions and 43 deletions

View file

@ -638,26 +638,30 @@ class generalizedTimeFormat(Attr):
""" """
__slots__ = ("stamp", "datetime",) __slots__ = ("stamp", "datetime",)
default = "19700101000000Z" python_type = datetime.datetime
default = datetime_from_generalized_time_format("19700101000000Z")
def __unicode__(self):
return unicode(datetime_to_generalized_time_format(self.value))
def __float__(self): def __float__(self):
return self.stamp return from_generalized_time_format(unicode(self))
def __int__(self): def __int__(self):
return int(self.stamp) return int(float(self))
def __eq__(self, othertime): def __eq__(self, othertime):
if isinstance(othertime, generalizedTimeFormat): if isinstance(othertime, generalizedTimeFormat):
return self.stamp == othertime.stamp return self.value == othertime.value
elif isinstance(othertime, float): elif isinstance(othertime, float):
return self.stamp == othertime return float(self) == othertime
elif isinstance(othertime, int): elif isinstance(othertime, int):
return self.stamp == othertime return int(self) == othertime
elif isinstance(othertime, unicode) or isinstance(othertime, str): elif isinstance(othertime, unicode) or isinstance(othertime, str):
resource = generalizedTimeFormat(othertime, conn=None, Parent=None) resource = generalizedTimeFormat(othertime, conn=None, Parent=None)
return self.stamp == resource.stamp return self == resource
elif isinstance(othertime, datetime.datetime): elif isinstance(othertime, datetime.datetime):
return self.datetime == othertime return self.value == othertime
else: else:
return False return False
@ -666,16 +670,16 @@ class generalizedTimeFormat(Attr):
def __lt__(self, othertime): def __lt__(self, othertime):
if isinstance(othertime, generalizedTimeFormat): if isinstance(othertime, generalizedTimeFormat):
return self.stamp < othertime.stamp return self.value < othertime.value
elif isinstance(othertime, float): elif isinstance(othertime, float):
return self.stamp < othertime return float(self) < othertime
elif isinstance(othertime, int): elif isinstance(othertime, int):
return self.stamp < othertime return int(self) < othertime
elif isinstance(othertime, unicode) or isinstance(othertime, str): elif isinstance(othertime, unicode) or isinstance(othertime, str):
resource = generalizedTimeFormat(othertime, conn=None, Parent=None) resource = generalizedTimeFormat(othertime, conn=None, Parent=None)
return self.stamp < resource.stamp return self < resource
elif isinstance(othertime, datetime.datetime): elif isinstance(othertime, datetime.datetime):
return self.datetime < othertime return self.value < othertime
else: else:
return False return False
@ -690,21 +694,9 @@ class generalizedTimeFormat(Attr):
def parse_value(self, gtf): def parse_value(self, gtf):
if isinstance(gtf, str) or isinstance(gtf, unicode): if isinstance(gtf, str) or isinstance(gtf, unicode):
if not ('Z' in gtf or '+' in gtf or '-' in gtf): self.value = datetime_from_generalized_time_format(gtf)
self.stamp = gtf
self.value = to_generalized_time_format(float(gtf))
self.datetime = datetime_from_generalized_time_format(self.value)
else:
self.stamp = from_generalized_time_format(gtf)
self.datetime = datetime_from_generalized_time_format(gtf)
self.value = gtf
elif isinstance(gtf, float):
self.stamp = gtf
self.value = to_generalized_time_format(gtf)
self.datetime = datetime_from_generalized_time_format(self.value)
elif isinstance(gtf, datetime.datetime): elif isinstance(gtf, datetime.datetime):
self.datetime = gtf self.value = gtf
self.value = datetime_to_generalized_time_format(gtf)
self.stamp = from_generalized_time_format(self.value) self.stamp = from_generalized_time_format(self.value)
@crans_attribute @crans_attribute

View file

@ -1038,33 +1038,42 @@ class proprio(CransLdapObject):
def fin_adhesion(self): def fin_adhesion(self):
"""Retourne la date de fin d'adhésion""" """Retourne la date de fin d'adhésion"""
return max([ return max([
float(facture.get('finAdhesion', [crans_utils.from_generalized_time_format(attributs.finAdhesion.default)])[0]) facture.get('finAdhesion', [attributs.finAdhesion.default])[0]
for facture in self.factures(refresh=(time.time() - self._factures_last_update > FACTURES_REFRESH_PERIOD)) for facture in self.factures(refresh=(time.time() - self._factures_last_update > FACTURES_REFRESH_PERIOD))
if facture.get('controle', [''])[0] != u"FALSE" and facture.get('recuPaiement', []) if facture.get('controle', [''])[0] != u"FALSE" and facture.get('recuPaiement', [])
] + [0.0]) ] + [attributs.finAdhesion.default])
def fin_connexion_datetime(self):
return datetime.datetime.fromtimestamp(self.fin_connexion())
def fin_adhesion_datetime(self):
return datetime.datetime.fromtimestamp(self.fin_adhesion())
def fin_connexion(self): def fin_connexion(self):
"""Retourne la date de fin de connexion""" """Retourne la date de fin de connexion"""
return max([ return max([
float(facture.get('finConnexion', [crans_utils.from_generalized_time_format(attributs.finConnexion.default)])[0]) facture.get('finConnexion', [attributs.finConnexion.default])[0]
for facture in self.factures(refresh=(time.time() - self._factures_last_update > FACTURES_REFRESH_PERIOD)) for facture in self.factures(refresh=(time.time() - self._factures_last_update > FACTURES_REFRESH_PERIOD))
if facture.get('controle', [''])[0] != u"FALSE" and facture.get('recuPaiement', []) if facture.get('controle', [''])[0] != u"FALSE" and facture.get('recuPaiement', [])
] + [0.0]) ] + [attributs.finConnexion.default])
def adhesion_ok(self, no_bl=False): def adhesion_ok(self, no_bl=False):
"""Renvoie si le propriétaire a une adhésion en cours.""" """Renvoie si le propriétaire a une adhésion en cours."""
if self.dn == variables.base_dn: if self.dn == variables.base_dn:
return True return True
_now = crans_utils.localized_datetime()
fin_paiement = self.fin_adhesion() fin_paiement = self.fin_adhesion()
paiement = time.time() < fin_paiement or (config.periode_transitoire and config.debut_periode_transitoire <= fin_paiement <= config.fin_periode_transitoire) paiement = (
_now < fin_paiement
or
(
config.periode_transitoire
and
(
crans_utils.datetime_from_generalized_time_format(config.gtf_debut_periode_transitoire)
<= fin_paiement
<= crans_utils.datetime_from_generalized_time_format(config.gtf_fin_periode_transitoire)
)
)
)
return paiement return paiement
@ -1081,12 +1090,26 @@ class proprio(CransLdapObject):
if bl['type'] == 'paiement': if bl['type'] == 'paiement':
return False return False
_now = crans_utils.localized_datetime()
if isinstance(self, adherent): if isinstance(self, adherent):
fin_paiement = min(self.fin_adhesion(), self.fin_connexion()) fin_paiement = min(self.fin_adhesion(), self.fin_connexion())
else: else:
fin_paiement = self.fin_adhesion() fin_paiement = self.fin_adhesion()
paiement = time.time() < fin_paiement or (config.periode_transitoire and config.debut_periode_transitoire <= fin_paiement <= config.fin_periode_transitoire) paiement = (
_now < fin_paiement
or
(
config.periode_transitoire
and
(
crans_utils.datetime_from_generalized_time_format(config.gtf_debut_periode_transitoire)
<= fin_paiement
<= crans_utils.datetime_from_generalized_time_format(config.gtf_fin_periode_transitoire)
)
)
)
return paiement return paiement

View file

@ -267,25 +267,31 @@ def list_clubs(clubs, width=None):
) )
def proprio(proprio, params): def proprio(proprio, params):
_now = crans_utils.localized_datetime()
params['o'] = proprio params['o'] = proprio
etat_administratif = [] etat_administratif = []
if proprio.paiement_ok(): if proprio.paiement_ok():
etat_administratif.append(style(u"à jour", "vert")) etat_administratif.append(style(u"à jour", "vert"))
if not proprio.paiement_ok(): if not proprio.paiement_ok():
etat_administratif.append(style(u"cotisation non réglée", "violet")) etat_administratif.append(style(u"cotisation non réglée", "violet"))
if proprio.fin_adhesion() >= time.time():
if proprio.fin_adhesion() >= _now:
adh = style(u"Adhésion jusqu'au %s" % (time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(proprio.fin_adhesion())),), "vert") adh = style(u"Adhésion jusqu'au %s" % (time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(proprio.fin_adhesion())),), "vert")
elif proprio.paiement_ok(): elif proprio.paiement_ok():
adh = style(u"Adhésion terminée, mais il y a un sursis.", 'orange') adh = style(u"Adhésion terminée, mais il y a un sursis.", 'orange')
else: else:
adh = style(u"Pas adhérent actuellement.", 'rouge') adh = style(u"Pas adhérent actuellement.", 'rouge')
params["adh"] = adh params["adh"] = adh
if proprio.fin_connexion() >= time.time(): if proprio.fin_connexion() >= _now:
conn = style(u"Connexion jusqu'au %s" % (time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(proprio.fin_connexion())),), "vert") conn = style(u"Connexion jusqu'au %s" % (time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(proprio.fin_connexion())),), "vert")
elif proprio.paiement_ok(): elif proprio.paiement_ok():
conn = style(u"Connexion terminée, mais il y a un sursis.", 'orange') conn = style(u"Connexion terminée, mais il y a un sursis.", 'orange')
else: else:
conn = style(u"Pas connecté actuellement.", 'rouge') conn = style(u"Pas connecté actuellement.", 'rouge')
params["conn"] = conn params["conn"] = conn
params['etat_administratif'] = etat_administratif params['etat_administratif'] = etat_administratif