Adapte gest_crans_lc pour les datetime

This commit is contained in:
Gabriel Detraz 2015-11-13 07:36:14 +01:00
parent 19b13c39d7
commit e2141e1043

View file

@ -18,6 +18,7 @@ import config.cotisation
import lc_ldap.objets as objets
import lc_ldap.attributs as attributs
from lc_ldap.attributs import UniquenessError
from lc_ldap import crans_utils
import proprio
from CPS import TailCall, tailcaller, Continue
@ -221,8 +222,7 @@ class Dialog(proprio.Dialog):
# Boite si on ne peux pas réahdérer
def box_already(end):
t_end = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(end))
self.dialog.msgbox("Actuellement adhérent jusqu'au %s.\nMerci de revenir lorsqu'il restera moins de %s jours avant la fin." % (t_end, config.cotisation.delai_readh_jour),
self.dialog.msgbox("Actuellement adhérent jusqu'au %s.\nMerci de revenir lorsqu'il restera moins de %s jours avant la fin." % (end, config.cotisation.delai_readh_jour),
width=0,
height=0,
timeout=self.timeout,
@ -231,8 +231,7 @@ class Dialog(proprio.Dialog):
# Boite de confirmation à l'ahésion
def box_adherer(end=None):
if end:
t_end = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(end))
adherer = self.confirm(text="Adhésion jusqu'au %s. Réadhérer ?" % t_end, title="Adhésion de %s %s" % (adherent.get("prenom", [''])[0], adherent["nom"][0]))
adherer = self.confirm(text="Adhésion jusqu'au %s. Réadhérer ?" % end, title="Adhésion de %s %s" % (adherent.get("prenom", [''])[0], adherent["nom"][0]))
else:
adherer = self.confirm(text="Adhésion pour un an, continuer ?", title="Adhésion de %s %s" % (adherent.get("prenom", [''])[0], adherent["nom"][0]))
return adherer
@ -246,9 +245,8 @@ class Dialog(proprio.Dialog):
# Génération de la facture pour adhésion
def paiement(tag_paiement, adherent, finadhesion, comment, facture, cancel_cont, cont):
now = time.time()
new_finadhesion = datetime.datetime.fromtimestamp(max(finadhesion, now))
new_finadhesion = time.mktime(new_finadhesion.replace(year=new_finadhesion.year + config.cotisation.duree_adh_an).timetuple()) + 86400
now = crans_utils.localized_datetime()
new_finadhesion = max(finadhesion, now).replace(year=max(finadhesion, now).year + 1)
new_debutadhesion = now
if facture:
facture = self.conn.search(dn=facture.dn, scope=0, mode='rw')[0]
@ -260,8 +258,8 @@ class Dialog(proprio.Dialog):
facture['modePaiement'] = unicode(tag_paiement, 'utf-8')
facture['info'] = unicode(comment, 'utf-8')
facture['article'].append(config.cotisation.dico_adh)
facture["finAdhesion"] = unicode(new_finadhesion)
facture["debutAdhesion"] = unicode(new_debutadhesion)
facture["finAdhesion"] = new_finadhesion
facture["debutAdhesion"] = new_debutadhesion
# On peut retarder le credit pour ajouter des contribution pour la connexion internet à la facture
if crediter:
if self.confirm_item(item=facture,
@ -284,9 +282,13 @@ class Dialog(proprio.Dialog):
raise Continue(cont(adherent=adherent))
finadhesion = adherent.fin_adhesion()
now = crans_utils.localized_datetime()
try:
finadhesion = adherent.fin_adhesion().value
except AttributeError:
finadhesion = now
# Si fin de l'adhésion trop loin dans le futur, rien a faire
if finadhesion and finadhesion - config.cotisation.delai_readh > time.time():
if finadhesion and (finadhesion - now).days > config.cotisation.delai_readh_jour:
self.handle_dialog(cancel_cont if cancel_cont else cont, box_already, finadhesion)
raise Continue(cancel_cont if cancel_cont else cont)
@ -340,7 +342,7 @@ class Dialog(proprio.Dialog):
# Une boite pour choisir un nombre de mois pour prolonger la connexion
def box(finconnexion, default_item=None):
t_end = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(finconnexion))
t_end = finconnexion
return self.dialog.menu(
"Connexion jusqu'au %s" % t_end if finconnexion else "N'a jamais été connecté",
width=0,
@ -357,17 +359,16 @@ class Dialog(proprio.Dialog):
# Génération et crédit de la facture
def todo(adherent, mois, finadhesion, finconnexion, cancel_cont, cont, facture=None, tag_paiment=None, comment=None):
now = time.time()
new_finconnexion = datetime.datetime.fromtimestamp(max(finconnexion, now))
# On ajoute 3600 secondes sur suggestion de Raphaël Bonaque (<bonaque@crans.org>), pour tenir compte des malheureux qui
# pourraient subir le changement d'heure.
new_finconnexion = time.mktime((new_finconnexion + dateutil.relativedelta.relativedelta(months=mois)).timetuple()) + 3600
now = crans_utils.localized_datetime()
new_debutconnexion = max(now, finconnexion)
con_month = new_debutconnexion.month
con_year = new_debutconnexion.year
new_finconnexion = max(finconnexion, now).replace(year=con_year + ((con_month + mois) // 12), month= (con_month + mois - 1) % 12 + 1)
if new_finconnexion > finadhesion:
t_end_adh = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(finadhesion))
t_end_conn = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(new_finconnexion))
if new_finconnexion - finadhesion > 30 * 3600 * 24:
if (new_finconnexion - finadhesion.value).days > 0:
t_end_adh = finadhesion.value()
t_end_conn = finconnexion
if (new_finconnexion - finadhesion.value).days > 30:
raise ValueError("Impossible de prolonger la connexion jusqu'au %s plus d'un mois après la fin de l'adhésion au %s" % (t_end_conn, t_end_adh))
else:
if not self.confirm("La fin de la connexion de l'adhérent (%s) tombera après la fin de son adhésion (%s).\n" \
@ -377,8 +378,8 @@ class Dialog(proprio.Dialog):
if facture:
with self.conn.search(dn=facture.dn, scope=0, mode='rw')[0] as facture:
if mois:
facture["finConnexion"] = unicode(new_finconnexion)
facture["debutConnexion"] = unicode(new_debutconnexion)
facture["finConnexion"] = new_finconnexion
facture["debutConnexion"] = new_debutconnexion
facture["article"].append(config.cotisation.dico_cotis(mois))
if self.confirm_item(item=facture,
text=u"Le paiement de %sEUR a-t-il bien été reçu (mode : %s) ?\n" % (facture.total(), facture['modePaiement'][0]),
@ -401,8 +402,8 @@ class Dialog(proprio.Dialog):
facture['modePaiement'] = unicode(tag_paiment, 'utf-8')
facture['article'].append(config.cotisation.dico_cotis(mois))
facture['info'] = unicode(comment, 'utf-8')
facture["finConnexion"] = unicode(new_finconnexion)
facture["debutConnexion"] = unicode(new_debutconnexion)
facture["finConnexion"] = new_finconnexion
facture["debutConnexion"] = new_debutconnexion
if self.confirm_item(item=facture,
text=u"Le paiement de %sEUR a-t-il bien été reçu (mode : %s) ?\n" % (facture.total(), tag_paiment),
title=u"Validation du paiement",
@ -434,15 +435,15 @@ class Dialog(proprio.Dialog):
finconnexion = adherent.fin_connexion()
# Si l'adhésion fini avant la connexion
if finadhesion <= time.time() or finadhesion <= finconnexion:
if finadhesion <= crans_utils.localized_datetime() or finadhesion <= finconnexion:
if finadhesion:
t_end_adh = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(finadhesion))
t_end_adh = finadhesion
# Si l'adhésion est déjà fini
if finadhesion <= time.time():
if finadhesion <= crans_utils.localized_datetime():
self.dialog.msgbox(text=u"L'adhésion a expiré le %s, il va falloir réadhérer d'abord" % t_end_adh, title="Réadhésion nécessaire", width=0, height=0, timeout=self.timeout)
# Sinon si elle fini avant la fin de la connexion courante
elif finadhesion < finconnexion:
t_end_conn = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(finconnexion))
t_end_conn = finconnexion
self.dialog.msgbox(text=u"L'adhésion de termine le %s, avant la fin de la connexion le %s, il va falloir réadhérer d'abord" % (t_end_adh, t_end_conn), title="Réadhésion nécessaire", width=0, height=0, timeout=self.timeout)
# Échouera si on essaie de prolonger la connexion au dela de l'adhésion et que l'adhésion est encore valable plus de quinze jours
return self.adherent_adhesion(cont=self_cont, cancel_cont=cont, adherent=adherent, crediter=False)