client.py 3.93 KB
Newer Older
Daniel Stan's avatar
Daniel Stan committed
1 2 3 4
# -*- coding: utf-8 -*-

from __future__ import print_function

5
from gestion import secrets_new as secrets
Daniel Stan's avatar
Daniel Stan committed
6 7
import pika
import json
Daniel Stan's avatar
Daniel Stan committed
8 9 10 11
import random
import datetime
import string
from lc_ldap import crans_utils
12
import config
13
import sys
14

Daniel Stan's avatar
Daniel Stan committed
15

Daniel Stan's avatar
Daniel Stan committed
16 17 18 19 20 21 22
def gen_password():
    """Génère un mot de passe aléatoire"""
    random.seed(datetime.datetime.now().microsecond)
    chars = string.letters + string.digits + '/=+*'
    length = 10
    return ''.join([random.choice(chars) for _ in xrange(length)])

Daniel Stan's avatar
Daniel Stan committed
23
class Ticket(object):
24 25 26 27
    def __init__(self, debug=False, aux=False):
        self.data = []
        self.debug = debug
        self.aux = aux 
28

Daniel Stan's avatar
Daniel Stan committed
29 30 31 32 33 34 35
    def add_entry(self, x):
        self.data.append(x)

    def add_account(self, login, mdp):
        self.add_entry({'login': login, 'pass': mdp})

    def add_machine(self, machine):
36 37 38 39 40
        todo = {
            'host': machine['host'][0].split('.', 1)[0],
            'macAddress': machine['macAddress'][0].value,
            'type': 'fil',
        }
Daniel Stan's avatar
Daniel Stan committed
41 42 43 44 45
        if machine.has_key('ipsec'):
            todo['secret'] = machine['ipsec'][0].value
            todo['type'] = 'wifi'
        self.add_entry(todo)

46 47 48 49 50 51 52 53 54 55
    def add_facture(self, facture):
        proprietaire = facture.proprio()
        chambre = None

        #On vérifie que le propriétaire a une chambre sinon on met EXT
        if 'chbre' in proprietaire.keys():
            chambre = proprietaire['chbre'][0].value
        else:
            chambre = u'EXT'

56
        try:
Daniel Stan's avatar
Daniel Stan committed
57 58
            deb_adh = crans_utils.datetime_from_generalized_time_format(facture['debutAdhesion'][0].value)
            deb_adh = deb_adh.strftime('%d/%m/%Y')
59
        except:
Daniel Stan's avatar
Daniel Stan committed
60 61 62 63
            deb_adh=False
        try:
            fin_adh = crans_utils.datetime_from_generalized_time_format(facture['finAdhesion'][0].value)
            fin_adh = fin_adh.strftime('%d/%m/%Y')
64
        except:
Daniel Stan's avatar
Daniel Stan committed
65 66 67
            fin_adh=False
        try:
            fin_co  = crans_utils.datetime_from_generalized_time_format(facture['finConnexion'][0].value)
68
            fin_co = fin_co.strftime('%d/%m/%Y')
Daniel Stan's avatar
Daniel Stan committed
69 70
        except:
            fin_co=False
71 72 73 74 75
        todo = {
            'fid' : facture['fid'][0].value,
            'article' : [ art.value for art in facture['article']],
            'modePaiement' : facture['modePaiement'][0].value,
            'recuPaiement' : facture['recuPaiement'][0].value,
76 77 78
            'debutAdhesion' : deb_adh,
            'finAdhesion' : fin_adh,
            'finConnexion' : fin_co,
79 80 81 82 83 84
            'chbre' : chambre,
            'nom' : proprietaire['nom'][0].value,
            'prenom' : proprietaire['prenom'][0].value,
        }
        self.add_entry(todo)

Daniel Stan's avatar
Daniel Stan committed
85 86 87 88
    def reset_password(self, adh):
        login = adh['uid'][0].value
        try:
            value = gen_password()
89 90
            with adh:
                adh['userPassword'] = [crans_utils.hash_password(value).decode('ascii')]
91
                adh.history_gen()
92
                adh.save()
Daniel Stan's avatar
Daniel Stan committed
93 94 95 96 97
        except EnvironmentError:
            print("Impossible de changer le mot de passe de %s" % login)
            exit(2)
        self.add_account(login, value)

Daniel Stan's avatar
Daniel Stan committed
98
    def print(self):
Daniel Stan's avatar
Daniel Stan committed
99 100 101
        if not self.data:
            print("Nothing to print !")
            return
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        creds = pika.credentials.PlainCredentials('oie', secrets.get('rabbitmq_oie'), True)
        if self.debug:
            queue_name = 'CransTicketDebug'
            creds = pika.credentials.PlainCredentials('rasputin', secrets.get('rabbitmq_rasputin'), True)
            print("Debug")
        elif self.aux:
            queue_name = config.AUXQUEUE
        else:
            queue_name = config.MAINQUEUE
        params = pika.ConnectionParameters(host='rabbitmq.crans.org',
            port=5671, credentials=creds, ssl=True)
        rabbit_c = pika.BlockingConnection(params)
        ch = rabbit_c.channel()
        ch.queue_declare(queue_name)
        ch.basic_publish(exchange='', routing_key=queue_name,
Daniel Stan's avatar
Daniel Stan committed
117
            body=json.dumps(self.data))
Daniel Stan's avatar
Daniel Stan committed
118
        print("Un nouveau ticket est en cours d'impression ...")
119