Exemplo n.º 1
0
def PostPieces():
    result = {}
    status = httplib.CREATED
    headers = {}

    try:
        piece = ParseIncoming(request.form, 'pieces')

        if PieceExiste(piece['numero']):
            raise RequestError(httplib.CONFLICT,
                               'Ce numero de piece est deja pris')

        db.DBConnection().pieces.insert(piece)

        headers['Location'] = url_for('GetPiecesNumero',
                                      numero=piece['numero'])

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status, headers
Exemplo n.º 2
0
def MettreAJourExpirationMembre(numero):
    exp = CalculerExpirationMembre(numero)

    db.DBConnection().membres.update({'numero': numero},
                                     {'$set': {
                                         'expiration': exp
                                     }})
Exemplo n.º 3
0
def PostMembres():
    membre = {}
    headers = {}

    result = None
    status = httplib.CREATED

    try:
        membre = ParseIncoming(request.form, 'membres')

        if 'numero' in membre:
            # Numéro de membre fourni par le client, vérifier qu'il n'est pas pris
            if ObtenirMembre(membre['numero']) != None:
                raise RequestError(httplib.CONFLICT,
                                   "Ce numéro de membre est déjà pris.")
        else:
            # On attribut automatiquement un # de membre
            membre['numero'] = ObtenirProchainNumeroDeMembre()

        membre['inscription'] = datetime.datetime.now()
        db.DBConnection().membres.insert(membre)

        headers['Location'] = url_for('GetMembresNumero',
                                      numero=membre['numero'])

        result = {'numero': membre['numero']}

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status, headers
Exemplo n.º 4
0
def PostHeuresBenevoles(numero):
    result = {}
    status = httplib.CREATED
    headers = {'Content-type': 'application/json'}

    try:
        heures = ParseIncoming(request.form, 'heuresbenevole')

        if not EstBenevole(numero):
            raise RequestError(httplib.UNPROCESSABLE_ENTITY,
                               'Ce membre n\'est pas bénévole')

        if 'date' not in heures:
            heures['date'] = datetime.datetime.now()

        db.DBConnection().membres.update({"numero": numero},
                                         {'$push': {
                                             "heuresbenevole": heures
                                         }})

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status, headers
Exemplo n.º 5
0
def GetFactures():
    result = {}
    status = httplib.OK
    headers = {'Content-type': 'application/json'}

    try:
        filters = ParseIncoming(request.args, 'getfactures')

        debut = filters.pop('debut', None)
        fin = filters.pop('fin', None)

        if debut or fin:
            filters['date'] = {}

            if debut:
                filters['date']['$gte'] = debut

            if fin:
                filters['date']['$lte'] = fin

        result = list(db.DBConnection().factures.find(filters).sort(
            'numero', 1))

    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status, headers
Exemplo n.º 6
0
def DeleteFactures(numero):
    result = ''
    status = httplib.NO_CONTENT

    try:
        facture = ObtenirFacture(numero)
        mettreAJourAbonnement = False

        if not facture:
            raise RequestError(httplib.NOT_FOUND, "Cette facture n'existe pas")

        if 'pieces' in facture:
            lignes = facture['pieces']
            for ligne in lignes:
                AjouterQuantitePieces(ligne)

                if ligne['numero'] in abonnements:
                    mettreAJourAbonnement = True

        # Supprimer la facture
        db.DBConnection().factures.remove({'numero': numero})

        # Mettre à jour l'abonnement au besoin
        if mettreAJourAbonnement:
            MettreAJourExpirationMembre(facture['membre'])

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status
Exemplo n.º 7
0
def PutFactures(numero):
    result = {}
    status = httplib.NO_CONTENT

    try:
        if not FactureExiste(numero):
            raise RequestError(httplib.NOT_FOUND,
                               "Cette facture n'existe pas.")

        facture = ParseIncoming(request.form, 'factures', False)

        ValidationFactures(facture)

        update_result = db.DBConnection().factures.update(
            {'numero': numero},
            {'$set': facture},
        )

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status
Exemplo n.º 8
0
def PutPieces(numero):
    result = {}
    status = httplib.NO_CONTENT

    try:
        if not PieceExiste(numero):
            raise RequestError(httplib.NOT_FOUND, "Cette pièce n'existe pas.")

        piece = ParseIncoming(request.form, 'pieces', False)

        # Si changement de numéro, vérifier que le numéro n'est pas déjà pris
        if 'numero' in piece and PieceExiste(
                piece['numero']) and numero != piece['numero']:
            raise RequestError(httplib.CONFLICT,
                               'Ce numero de piece est deja pris')

        update_result = db.DBConnection().pieces.update({'numero': numero},
                                                        {'$set': piece})

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status
Exemplo n.º 9
0
def ObtenirProchainNumeroDeFacture():
    """Retourne le prochain numero de facture disponible."""
    d = db.DBConnection()

    if d.factures.count() == 0:
        return 1
    else:
        return d.factures.find().sort(
            'numero', pymongo.DESCENDING).limit(1)[0]['numero'] + 1
Exemplo n.º 10
0
def DeletePieces(numero):
    result = ''
    status = httplib.NO_CONTENT

    try:
        db.DBConnection().pieces.remove({'numero': numero})
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status
Exemplo n.º 11
0
def SoustraireQuantitePieces(entree_piece):
    numero_piece = entree_piece['numero']

    if numero_piece in abonnements:
        return

    quantite = entree_piece['quantite']
    db.DBConnection().pieces.update({'numero': numero_piece},
                                    {'$inc': {
                                        'quantite': -quantite
                                    }})
Exemplo n.º 12
0
def FacturesFermees():
    factures = list(db.DBConnection().factures.find({'complete': True}))
    membres = {}

    for facture in factures:
        if facture['membre'] not in membres:
            membres[facture['membre']] = ObtenirMembre(facture['membre'])

    return render_template('factures-fermees.html',
                           factures=factures,
                           membres=membres)
Exemplo n.º 13
0
def GetMembres():
    result = {}
    status = httplib.OK
    headers = {'Content-type': 'application/json'}

    try:
        result = list(db.DBConnection().membres.find())
    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status, headers
Exemplo n.º 14
0
def DeletePieceFromFacture(numero_facture, numero_piece):
    result = {}
    status = httplib.NO_CONTENT

    try:
        if not FactureExiste(numero_facture):
            raise RequestError(httplib.NOT_FOUND,
                               "Cette facture n'existe pas.")

        facture = ObtenirFacture(numero_facture)

        if 'pieces' not in facture:
            raise RequestError(httplib.NOT_FOUND,
                               "Cette facture ne contient pas cette pièce.")

        entree_piece = None

        for ep in facture['pieces']:
            if ep['numero'] == numero_piece:
                entree_piece = ep
                break

        if entree_piece is None:
            raise RequestError(httplib.NOT_FOUND,
                               "Cette facture ne contient pas cette pièce.")

        # Supprimer la pièce de la facture
        facture['pieces'] = [
            x for x in facture['pieces'] if x['numero'] != numero_piece
        ]

        # Ajuster le prix total de la facture
        facture['prixtotal'] = CalculerPrixTotalFacture(facture)

        # Écrire la facture dans la BD
        db.DBConnection().factures.update({'numero': numero_facture}, facture)

        # Ajuster les quantités en inventaire
        AjouterQuantitePieces(entree_piece)

        # Mise à jour de la date d'abonnement, si requis
        if numero_piece in abonnements:
            MettreAJourExpirationMembre(facture['membre'])

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status
Exemplo n.º 15
0
def GetBenevoles():
    result = {}
    status = httplib.OK
    headers = {'Content-type': 'application/json'}

    try:
        result = list(db.DBConnection().membres.find({'estbenevole': True}))

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status, headers
Exemplo n.º 16
0
def GetCategoriesPieces():
    result = {}
    status = httplib.OK
    headers = {'Content-type': 'application/json'}

    try:
        categories = list(db.DBConnection().pieces.distinct('section'))
        categories.sort()

        result = categories

    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status, headers
Exemplo n.º 17
0
def CalculerExpirationMembre(numero):
    if not MembreExiste(numero):
        raise ValueError('Membre inexistant')

    factures = db.DBConnection().factures.find({'membre': numero})

    latest = None
    for facture in factures:
        if 'pieces' not in facture:
            continue
        for ligne in facture['pieces']:
            if ligne['numero'] in abonnements:
                tentative = facture['date'] + abonnements[ligne['numero']]
                if latest is None or tentative > latest:
                    latest = tentative

    return latest
Exemplo n.º 18
0
def GetPiecesNumero(numero):
    result = {}
    status = httplib.OK
    headers = {'Content-type': 'application/json'}

    try:
        if not PieceExiste(numero):
            raise RequestError(httplib.NOT_FOUND, "Cette pièce n'existe pas.")

        result = db.DBConnection().pieces.find_one({'numero': numero})

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status, headers
Exemplo n.º 19
0
def PutMembres(numero):
    result = {}
    status = httplib.OK

    try:
        if not MembreExiste(numero):
            raise RequestError(httplib.NOT_FOUND, "Ce membre n'existe pas")

        membre = ParseIncoming(request.form, 'membres', False)

        update_result = db.DBConnection().membres.update({'numero': numero},
                                                         {'$set': membre})

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status
Exemplo n.º 20
0
def AdminRapport():
    # (année, mois) -> ($ pièces, $ abonnements)
    somme_ventes = {}

    factures = db.DBConnection().factures.find()

    for facture in factures:
        mois = (facture['date'].year, facture['date'].month)
        somme_pieces = facture['prixtotal']
        somme_abonnements = 0

        for piece in facture['pieces']:

            if piece['numero'] in abonnements:
                somme_pieces -= piece['prixtotal']
                somme_abonnements += piece['prixtotal']

        ancien = somme_ventes.get(mois, (0, 0))
        somme_ventes[mois] = (ancien[0] + somme_pieces,
                              ancien[1] + somme_abonnements)

    somme_ventes = sorted(somme_ventes.items())

    return render_template('admin-rapport.html', somme_ventes=somme_ventes)
Exemplo n.º 21
0
def PostFactures():
    facture = {}
    headers = {'Content-type': 'application/json'}

    result = {}
    status = httplib.CREATED

    try:
        facture = ParseIncoming(request.form, 'factures')

        facture['numero'] = ObtenirProchainNumeroDeFacture()
        facture['pieces'] = []

        facture['prixtotal'] = 0

        if 'complete' not in facture:
            facture['complete'] = False

        if 'date' not in facture:
            facture['date'] = datetime.datetime.now()

        ValidationFactures(facture)

        db.DBConnection().factures.insert(facture)
        headers['Location'] = url_for('GetFacturesNumero',
                                      numero=facture['numero'])
        result = facture

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as ex:
        status = httplib.INTERNAL_SERVER_ERROR
        result = str(ex)

    return jsonify(result), status, headers
Exemplo n.º 22
0
def EstBenevole(numero):
    membre = db.DBConnection().membres.find_one({
        'numero': numero,
        'estbenevole': True
    })
    return membre != None
Exemplo n.º 23
0
def ObtenirFacture(numero):
    return db.DBConnection().factures.find_one({'numero': numero})
Exemplo n.º 24
0
def ObtenirPiece(numero):
    return db.DBConnection().pieces.find_one({'numero': numero})
Exemplo n.º 25
0
def ObtenirMembre(numero):
    return db.DBConnection().membres.find_one({'numero': numero})
Exemplo n.º 26
0
def UnMembre(numero):
    membre = db.DBConnection().membres.find_one({'numero': numero})
    return render_template('membre.html', numero=numero, membre=membre)
Exemplo n.º 27
0
def PostPieceInFacture(numero_facture):
    result = {}
    status = httplib.OK
    headers = {'Content-type': 'application/json'}

    try:
        if not FactureExiste(numero_facture):
            raise RequestError(httplib.NOT_FOUND,
                               "Cette facture n'existe pas.")

        facture = ObtenirFacture(numero_facture)

        val = ParseIncoming(request.form, 'factureajoutpiece')
        numero_piece = val['numero']

        if not PieceExiste(numero_piece):
            raise RequestError(httplib.UNPROCESSABLE_ENTITY,
                               "Cette pièce n'existe pas.")

        entree_piece_existante = None

        if 'pieces' in facture:
            for ep in facture['pieces']:
                if ep['numero'] == numero_piece:
                    if 'fusionsiexiste' in val and val['fusionsiexiste']:
                        entree_piece_existante = ep
                    else:
                        raise RequestError(
                            httplib.CONFLICT,
                            "Cette pièce est déjà présente dans cette facture."
                        )

        piece = ObtenirPiece(numero_piece)

        entree_piece = TraiterQuantitesAjoutPieceFacture(val, piece)

        if entree_piece_existante is None:
            # Ajouter la pièce dans la facture
            facture['pieces'].append(entree_piece)
            result = entree_piece
        else:
            # Ajouter les quantités à l'entrée existante
            # entree_piece_existante étant une référence, ça modifie directement dans la liste facture['pieces']
            FusionEntreesPieces(entree_piece_existante, entree_piece)
            result = entree_piece_existante

        # Ajuster le prix total de la facture
        facture['prixtotal'] = CalculerPrixTotalFacture(facture)

        # Écrire la facture dans la BD
        db.DBConnection().factures.update({'numero': numero_facture}, facture)

        # Ajuster les quantités en inventaire
        SoustraireQuantitePieces(entree_piece)

        # Mise à jour de la date d'abonnement, si requis
        if numero_piece in abonnements:
            MettreAJourExpirationMembre(facture['membre'])

    except RequestError as ex:
        status = ex.status
        result = ex.msg
    except Exception as e:
        result = str(e)
        status = httplib.INTERNAL_SERVER_ERROR

    return jsonify(result), status, headers
Exemplo n.º 28
0
def HeuresBenevoles():
    benevoles = list(db.DBConnection().membres.find({
        'estbenevole': True
    }).sort('prenom', pymongo.ASCENDING))

    return render_template('heuresbenevoles.html', benevoles=benevoles)
Exemplo n.º 29
0
def AdminListeDiffusion():
    membres = db.DBConnection().membres.find({'listedenvoi': 'oui'})

    return render_template('admin-listediffusion.html', membres=list(membres))