<commit_before><commit_msg>Add taxi API<commit_after># -*- coding: utf8 -*-
from flask_restful import Resource, reqparse
from flask.ext.security import login_required, current_user, roles_accepted
from flask import request, redirect, url_for, abort, jsonify
from ..models import taxis as taxis_models, administrative as administrative_models
from .. import db, api


@api.route('/taxi/', endpoint="taxi")
class Taxi(Resource):

    @api.doc(responses={404:'Resource not found',
        403:'You\'re not authorized to view it'})
    @login_required
    @roles_accepted('admin', 'operateur')
    def put(self):
        json = request.get_json()
        fields = ['immatriculation', 'numero_ads', 'insee', 'carte_pro', 'departement']
        if not json or\
            any(map(lambda f: f not in json, fields)):
            abort(400)
        departement = administrative_models.Departement.query\
            .filter_by(numero=str(json['departement'])).first()
        if not departement:
            abort(404)
        conducteur = taxis_models.Conducteur.query\
                .filter_by(carte_pro=json['carte_pro'],
                           departement_id=departement.id).first()
        if not conducteur:
            abort(404, {"error": "Unable to find carte_pro"})
        vehicle = taxis_models.Vehicle.query\
                .filter_by(immatriculation=json['immatriculation']).first()
        if not vehicle:
            abort(404, {"error": "Unable to find immatriculation"})
        ads = taxis_models.ADS.query\
                .filter_by(numero=json['numero_ads'], insee=json['insee']).first()
        if not ads:
            abort(404, {"error": "Unable to find numero_ads for this insee code"})
        taxi = taxis_models.Taxi.query.filter_by(conducteur_id=conducteur.id,
                vehicle_id=vehicle.id, ads_id=ads.id).first()
        if not taxi:
            taxi = taxis_models.Taxi()
            taxi.conducteur_id = conducteur.id
            taxi.vehicle_id = vehicle.id
            taxi.ads_id = ads.id
            db.session.add(taxi)
            db.session.commit()
        return redirect(url_for('taxi_id', taxi_id=taxi.id))



@api.route('/taxi/<int:taxi_id>/', endpoint="taxi_id")
class TaxiId(Resource):

    @api.doc(responses={404:'Resource not found',
        403:'You\'re not authorized to view it'})
    @login_required
    @roles_accepted('admin', 'operateur')
    def get(self, taxi_id):
        taxi = taxis_models.Taxi.query.get(taxi_id)
#@TODO:gérer la relation operateur<->conducteur
        return taxi.as_dict()


{'fraction_non_alphanumeric': 0.12524385485758877, 'fraction_numerical': 0.01092469761997659, 'mean_word_length': 2.737609329446064, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1668679', 'n_tokens_mistral': 826, 'n_tokens_neox': 803, 'n_words': 171}