Beispiel #1
0
            if key in d:
                d[key] = d[key].replace(tzinfo=None)

        old_pilot = flight.pilot_id

        for key, value in d.iteritems():
            setattr(flight, key, value)

        if not (flight.takeoff_time <= flight.scoring_start_time <= flight.scoring_end_time <= flight.landing_time):
            return jsonify(error='invalid-times'), 422

        if flight.pilot_id != old_pilot and flight.pilot_id:
            flight.club_id = users[flight.pilot_id].club_id

        flight.privacy_level = Flight.PrivacyLevel.PUBLIC
        flight.time_modified = datetime.utcnow()

    db.session.commit()

    for flight_id in flights.iterkeys():
        try:
            tasks.analyse_flight.delay(flight_id)
            tasks.find_meetings.delay(flight_id)
        except ConnectionError:
            current_app.logger.info('Cannot connect to Redis server')

    return jsonify()


@upload_blueprint.route('/flights/upload/airspace/<string:cache_key>/<int:airspace_id>.png')
def airspace_image(cache_key, airspace_id):
Beispiel #2
0
                d[key] = d[key].replace(tzinfo=None)

        old_pilot = flight.pilot_id

        for key, value in d.iteritems():
            setattr(flight, key, value)

        if not (flight.takeoff_time <= flight.scoring_start_time <=
                flight.scoring_end_time <= flight.landing_time):
            return jsonify(error='invalid-times'), 422

        if flight.pilot_id != old_pilot and flight.pilot_id:
            flight.club_id = users[flight.pilot_id].club_id

        flight.privacy_level = Flight.PrivacyLevel.PUBLIC
        flight.time_modified = datetime.utcnow()

    db.session.commit()

    for flight_id in flights.iterkeys():
        try:
            tasks.analyse_flight.delay(flight_id)
            tasks.find_meetings.delay(flight_id)
        except ConnectionError:
            current_app.logger.info('Cannot connect to Redis server')

    return jsonify()


@upload_blueprint.route(
    '/flights/upload/airspace/<string:cache_key>/<int:airspace_id>.png')
Beispiel #3
0
def index_post():
    current_user = User.get(request.user_id)

    form = request.form

    if form.get("pilotId") == u"":
        form = form.copy()
        form.pop("pilotId")
    try:
        data = FlightSchema(only=("pilotId", "pilotName")).load(form).data
    except ValidationError as e:
        return jsonify(error="validation-failed", fields=e.messages), 422

    pilot_id = data.get("pilot_id")
    pilot = pilot_id and User.get(pilot_id)
    pilot_id = pilot and pilot.id

    club_id = (pilot and pilot.club_id) or current_user.club_id

    results = []

    _files = request.files.getlist("files")

    prefix = 0
    for name, f in iterate_upload_files(_files):
        igc_file = IGCFile()
        prefix += 1
        try:
            igc_file.date_condor = get_date_from_name(
                name)  #name differs from filename which has _1 etc appended
            igc_file.time_created = igc_file.date_condor
            igc_file.time_modified = datetime.utcnow()
        except:
            files.delete_file(name)
            results.append(UploadResult.date_not_in_filename(
                name, str(prefix)))
            continue
        filename = files.sanitise_filename(name)
        filename = files.add_file(filename, f)

        # check if the file already exists
        with files.open_file(filename) as f:
            md5 = file_md5(f)
            other = Flight.by_md5(md5)
            if other:
                files.delete_file(filename)
                results.append(
                    UploadResult.for_duplicate(name, other, str(prefix)))
                continue

        igc_file.owner = current_user
        igc_file.filename = filename
        igc_file.md5 = md5
        igc_file.update_igc_headers(
        )  #gets condor flight plan and flight_plan_md5
        if not igc_file.is_condor_file:
            files.delete_file(filename)
            results.append(UploadResult.not_condor(name, str(prefix)))
            continue

        igc_file.date_utc = igc_file.date_condor

        if igc_file.date_utc is None:
            files.delete_file(filename)
            results.append(UploadResult.for_missing_date(name, str(prefix)))
            continue

        flight = Flight()  #instance
        flight.pilot_id = pilot_id or current_user.id
        flight.pilot_name = data.get("pilot_name") or current_user.name
        flight.club_id = club_id
        flight.landscape = igc_file.landscape
        flight.flight_plan_md5 = igc_file.flight_plan_md5
        flight.time_created = igc_file.date_condor
        flight.time_modified = datetime.utcnow()
        flight.time_igc_upload = igc_file.time_modified
        flight.date_local = igc_file.date_condor
        flight.model_id = igc_file.get_model()
        flight.igc_file = igc_file
        flight.registration = igc_file.registration
        flight.competition_id = igc_file.competition_id

        # fp = flight_path(flight.igc_file, add_elevation=True, max_points=None)
        fp = flight_path(igc_file, add_elevation=True, max_points=None)

        analyzed = False
        try:
            analyzed = analyse_flight(flight, fp=fp)
        except:
            current_app.logger.exception(
                "analyse_flight() raised an exception")

        if not analyzed:
            files.delete_file(filename)
            results.append(UploadResult.for_parser_error(name, str(prefix)))
            continue

        if not flight.takeoff_time or not flight.landing_time:
            files.delete_file(filename)
            results.append(UploadResult.for_no_flight(name, str(prefix)))
            continue

        if flight.time_created > datetime.utcnow():
            files.delete_file(filename)
            results.append(UploadResult.for_future_flight(name, str(prefix)))
            continue

        if not flight.update_flight_path():
            files.delete_file(filename)
            results.append(UploadResult.for_no_flight(name, str(prefix)))
            continue

        # flight.privacy_level = Flight.PrivacyLevel.PRIVATE
        flight.privacy_level = Flight.PrivacyLevel.PUBLIC  #bch

        trace = _encode_flight_path(fp, qnh=flight.qnh)
        infringements = get_airspace_infringements(fp, qnh=flight.qnh)

        db.session.add(igc_file)
        db.session.add(flight)

        # flush data to make sure we don't get duplicate files from ZIP files
        db.session.flush()

        # Store data in cache for image creation
        cache_key = hashlib.sha1(
            (to_unicode(flight.id) + u"_" +
             to_unicode(current_user.id)).encode("utf-8")).hexdigest()

        cache.set("upload_airspace_infringements_" + cache_key,
                  infringements,
                  timeout=15 * 60)
        cache.set("upload_airspace_flight_path_" + cache_key,
                  fp,
                  timeout=15 * 60)

        airspace = (db.session.query(Airspace).filter(
            Airspace.id.in_(infringements.keys())).all())

        results.append(
            UploadResult(
                name,
                flight,
                UploadStatus.SUCCESS,
                str(prefix),
                trace,
                airspace,
                cache_key,
            ))

        groupflight_actions(flight, igc_file)

        create_flight_notifications(flight)

    db.session.commit()

    results = UploadResultSchema().dump(results, many=True).data

    club_members = []
    if current_user.club_id:
        member_schema = UserSchema(only=("id", "name"))

        club_members = (User.query(club_id=current_user.club_id).order_by(
            func.lower(User.name)).filter(User.id != current_user.id))

        club_members = member_schema.dump(club_members.all(), many=True).data

    aircraft_models = (AircraftModel.query().order_by(
        AircraftModel.kind).order_by(AircraftModel.name).all())

    aircraft_models = AircraftModelSchema().dump(aircraft_models,
                                                 many=True).data

    return jsonify(results=results,
                   club_members=club_members,
                   aircraft_models=aircraft_models)