196 lines
7.4 KiB
Python
196 lines
7.4 KiB
Python
#!/bin/bash /usr/scripts/python.sh
|
|
# -*- coding: utf-8 -*-
|
|
from gestion.affich_tools import coul, tableau
|
|
import importlib
|
|
import time
|
|
import sys
|
|
|
|
# Import inutile, mais on en a besoin pour que le
|
|
# script continue à fonctionner.
|
|
import gestion
|
|
import gestion.annuaires_pg
|
|
|
|
def try_import(lib):
|
|
"""
|
|
Cette fonction sert à faire de l'import soft : si l'import
|
|
crashe, par exemple, si le fichier de secrets ne peut être
|
|
lu par un apprenti, ça permet toujours d'utiliser ce
|
|
qui n'utilise pas l'import raté.
|
|
"""
|
|
if not isinstance(lib, unicode):
|
|
lib = lib.decode('utf-8')
|
|
try:
|
|
lib = importlib.import_module(lib)
|
|
except:
|
|
if sys.stdout.isatty():
|
|
print (u"Impossible d'importer %s, c'est sans doute un problème de droits." % lib).encode('utf-8')
|
|
lib = None
|
|
return lib
|
|
|
|
# Ici, on utilise le module gestion importé
|
|
# Il faudrait remplacer cet import là par une
|
|
# fonction prise_etat propre à lc_ldap, pour
|
|
# ne plus en avoir besoin ci-après.
|
|
gestion.whos = try_import(u"gestion.whos")
|
|
|
|
def prise_etat(chbre):
|
|
if chbre=="????":
|
|
return coul("Chambre invalide", "violet")
|
|
return gestion.whos.prise_etat(chbre)[0]
|
|
|
|
def timeformat(t, format):
|
|
return time.strftime(format, time.localtime(t))
|
|
|
|
def blacklist(l):
|
|
bl=[]
|
|
for b in l:
|
|
debut=b['debut'] if b['debut'] == '-' else time.strftime("%d/%m/%Y %H:%M", time.localtime(b['debut']))
|
|
fin=b['fin'] if b['fin'] == '-' else time.strftime("%d/%m/%Y %H:%M", time.localtime(b['fin']))
|
|
couleur='rouge' if b['actif'] else None
|
|
if debut != '-' and fin !='-':
|
|
bl.append(coul(u"du %s au %s : %s [%s]" % (debut, fin, b['type'], b['comm']), couleur))
|
|
elif debut != '-':
|
|
bl.append(coul(u"À partir du %s : %s [%s]" % (debut, b['type'], b['comm']), couleur))
|
|
elif fin != '-':
|
|
bl.append(coul(u"Jusqu'au %s : %s [%s]" % (fin, b['type'], b['comm']), couleur))
|
|
else:
|
|
bl.append(coul(u"%s [%s]" % (b['type'], b['comm']), couleur))
|
|
return bl
|
|
|
|
def split(str, *arg):
|
|
return str.split(*arg)
|
|
|
|
def telephone(l):
|
|
tel=[]
|
|
for t in l:
|
|
if len(str(t)) == 10:
|
|
tel.append("%c%c.%c%c.%c%c.%c%c.%c%c" % tuple(map(ord, str(t))))
|
|
else:
|
|
if str(t).startswith("00"):
|
|
t="+%s" % str(t)[2:]
|
|
tel.append(t)
|
|
return tel
|
|
|
|
templateEnv=None
|
|
def template():
|
|
global templateEnv
|
|
if not templateEnv:
|
|
# un import paresseux, comme ça, pas la peine d'installer jinja2 sur les machines où il n'y en a pas besoin
|
|
import jinja2
|
|
template_path = '/usr/scripts/lc_ldap/printing/templates/'
|
|
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
|
|
templateEnv = jinja2.Environment( loader=templateLoader, trim_blocks=True )
|
|
templateEnv.add_extension('jinja2.ext.do')
|
|
templateEnv.filters['coul'] = coul
|
|
templateEnv.filters['blacklist'] = blacklist
|
|
templateEnv.filters['prise_etat'] = prise_etat
|
|
templateEnv.filters['timeformat'] = timeformat
|
|
templateEnv.filters['split'] = split
|
|
templateEnv.filters['telephone'] = telephone
|
|
templateEnv.filters['tableau'] = tableau
|
|
return templateEnv
|
|
|
|
def machine(machine, params):
|
|
params['o']=machine
|
|
return template().get_template("machine").render(params)
|
|
|
|
|
|
def list_machines(machines):
|
|
return tableau([
|
|
[m['mid'][0], m['rid'][0] , str(m['objectClass'][0])[7:], str(m['host'][0]).split('.')[0],
|
|
m['ipHostNumber'][0] if m.get('ipHostNumber',[]) else '-', m['macAddress'][0],
|
|
m.blacklist_actif()[0] if m.blacklist_actif() else '-'] for m in machines],
|
|
titre = [u'mid', u'rid', u'Type', u'Nom de machine', u'Adresse IP', u'Adresse MAC', u'Limitation'],
|
|
largeur = [5, 5, 6, '*', 15, 17, 10],
|
|
alignement = ['d', 'd', 'c', 'c', 'c', 'c', 'c'])
|
|
|
|
def list_factures(factures):
|
|
return tableau([
|
|
[f['fid'][0], f['modePaiement'][0],
|
|
coul("OK", "vert") if f.get('recuPaiement', []) else coul("NON", "rouge"),
|
|
' '.join(attr['code'] for attr in f.get('article',[])),
|
|
u"%s€" % sum([float(a['pu'])*int(a['nombre']) for a in f.get('article',[])])
|
|
] for f in factures],
|
|
titre = [u'fid', u'Mode de paiement', u'Payé', u'Articles', u"Total"],
|
|
largeur = [5, 16, 6, '*', 8],
|
|
alignement = ['d', 'g', 'c', 'g', 'd'])
|
|
|
|
def list_adherents(adherents):
|
|
return tableau([
|
|
[a['aid'][0],
|
|
u' '.join(unicode(i) for i in a['prenom'] + a['nom']),
|
|
a['chbre'][0], coul('o', 'vert') if a.paiement_ok() else coul('n', 'rouge'),
|
|
coul('o', 'vert') if a.carte_ok() else coul('n', 'rouge'),
|
|
u', '.join(unicode(m['host'][0]).split('.',1)[0] for m in a.machines())
|
|
] for a in adherents ],
|
|
titre = [u'aid', u'Prénom Nom', u'Chbre', u'P', u'C', u'Machines'],
|
|
largeur = [5, 35, 5, 1, 1, '*'],
|
|
alignement = ['d', 'c', 'c', 'c', 'c', 'g'])
|
|
|
|
def proprio(proprio, params):
|
|
params['o']=proprio
|
|
etat_administratif=[]
|
|
if proprio.paiement_ok() and proprio.carte_ok():
|
|
etat_administratif.append(coul(u"à jour", "vert"))
|
|
if not proprio.carte_ok():
|
|
etat_administratif.append(coul(u"manque carte d'étudiant", "violet"))
|
|
if not proprio.paiement_ok():
|
|
etat_administratif.append(coul(u"cotisation non réglée", "violet"))
|
|
params['etat_administratif']=etat_administratif
|
|
if proprio["chbre"][0].value not in ["????", "EXT"]:
|
|
params['brassage'] = coul("Cr@ns", "bleu") if gestion.annuaires_pg.is_crans(proprio["chbre"][0].value[0], proprio["chbre"][0].value[1:]) else coul("CROUS", "jaune")
|
|
|
|
if proprio.machines():
|
|
params['machines']=list_machines(proprio.machines())
|
|
if proprio.factures():
|
|
params['factures']=list_factures(proprio.factures())
|
|
return params
|
|
|
|
def club(club, params):
|
|
params=proprio(club, params)
|
|
return template().get_template("club").render(params)
|
|
|
|
def adherent(adherent, params):
|
|
params=proprio(adherent, params)
|
|
return template().get_template("adherent").render(params)
|
|
|
|
def facture(facture, params):
|
|
params['o']=facture
|
|
return template().get_template("facture").render(params)
|
|
|
|
def sprint(object, limit=5):
|
|
from lc_ldap import objets
|
|
params={'limit':limit}
|
|
if isinstance(object, objets.machine):
|
|
return machine(object, params)
|
|
elif isinstance(object, objets.adherent):
|
|
return adherent(object, params)
|
|
elif isinstance(object, objets.club):
|
|
return club(object, params)
|
|
elif isinstance(object, objets.facture):
|
|
return facture(object, params)
|
|
else:
|
|
return str(object)
|
|
|
|
|
|
|
|
def sprint_list(list):
|
|
from lc_ldap import objets
|
|
mlist={}
|
|
ret=[]
|
|
for o in list:
|
|
mlist[o.__class__] = mlist.get(o.__class__, []) + [o]
|
|
|
|
for classe, list in mlist.items():
|
|
if issubclass(classe, objets.machine):
|
|
ret.append(list_machines(list))
|
|
elif issubclass(classe, objets.adherent):
|
|
ret.append(list_adherents(list))
|
|
#elif issubclass(classe, objets.club):
|
|
# ret.append(list_clubs(list))
|
|
elif issubclass(classe, objets.facture):
|
|
ret.append(list_factures(list))
|
|
else:
|
|
ret.append("Listes d'objets de classe %s non affichage" % classe.__name__)
|
|
return '\n'.join(ret)
|
|
|