示例#1
0
文件: areas.py 项目: VladShg/Agrown
def add_area():
    json = request.get_json()
    name = json['name']
    crops_type = json['crops_type']
    square = float(json['square'])
    points = json['points']
    user = User.verify_auth_token(json['token'])
    area = Area(name=name,
                crops_type=crops_type,
                square=square,
                user_id=user.id)
    db.session.add(area)
    db.session.commit()
    db.session.refresh(area)
    for p in points:
        db.session.add(
            RectanglePoint(area_id=area.id,
                           latitude=float(p['lat']),
                           longitude=float(p['lng'])))
    db.session.commit()

    return jsonify({
        'status': 'created',
        "area": {
            "id": area.id,
            "name": area.name,
            'crops_type': crops_type,
            'square': square,
            'points': points
        }
    }), 200
示例#2
0
文件: flights.py 项目: VladShg/Agrown
def get_flights():
    json = request.get_json()
    token = json['token']
    user = User.verify_auth_token(token)
    user_flights = user.flights
    user_fights = sorted(user_flights, key=lambda f: f.id)
    data = []
    for flight in user_flights:
        data.append({
            'id': flight.id,
            'name': flight.name,
            'interval': flight.day_interval,
            'time': flight.begin_at.strftime("%H:%M"),
            'active': flight.active,
            'area': {
                'id': flight.area.id,
                'name': flight.area.name
            },
            'drone': {
                'id': flight.drone.id,
                'name': flight.drone.name
            }
        })

    return jsonify(data), 200
示例#3
0
def verify_password(username_or_token, password):
    user = User.verify_auth_token(username_or_token)
    if not user:
        user = User.query.filter_by(username=username_or_token).first()
        if not user or not user.verify_password(password):
            return False
    g.user = user
    return True
示例#4
0
def change_password():
    json = request.get_json()
    token = json['token']
    user = User.verify_auth_token(token)
    if user.password != json['password_old']:
        return jsonify({"error": "Wrong password"})
    user.password = json['password']
    db.session.commit()
    return jsonify({"status": "Password updated"})
示例#5
0
文件: auth.py 项目: VladShg/Agrown
 def token_check(*args, **kwargs):
     json = request.json
     token = json['token']
     user = User.verify_auth_token(token)
     if user is None:
         return jsonify({
             'status': 'error',
             'error': 'token not valid'
         }), 401
     return func(*args, **kwargs)
示例#6
0
def profile():
    json = request.get_json()
    token = json['token']
    user = User.verify_auth_token(token)

    return jsonify({
        'login': user.login,
        'email': user.email,
        'status': 'done'
    })
示例#7
0
文件: flights.py 项目: VladShg/Agrown
def delete_flight():
    json = request.get_json()
    user = User.verify_auth_token(json['token'])
    flight = Flight.query.filter(Flight.id == int(json['id']),
                                 Flight.user_id == user.id).first()
    if not flight:
        return jsonify({'status': "error", "error": 'flight not found'}), 400
    db.session.delete(flight)
    db.session.commit()
    return jsonify({'status': "deleted"}), 200
示例#8
0
文件: flights.py 项目: VladShg/Agrown
def update_flight():
    json = request.get_json()
    name = json['name']
    area_id = int(json['area_id'])
    drone_id = int(json['drone_id'])
    active = bool(json['active'])
    interval = int(json['interval'])
    time_obj = time()
    try:
        flight_time = json['time'].split(':')
        time_obj = time(hour=int(flight_time[0]),
                        minute=int(flight_time[1]),
                        second=0)
    except:
        return jsonify({'status': 'error', 'error': 'wrong time format'}), 400

    user = User.verify_auth_token(json['token'])
    flight = Flight.query.filter(Flight.user_id == user.id,
                                 Flight.id == int(json['id'])).first()
    if not flight:
        return jsonify({'status': 'error', 'error': 'flight not found'}), 400
    area = Area.query.filter(Area.id == area_id,
                             Area.user_id == user.id).first()
    if not area:
        return jsonify({'status': 'error', 'error': 'area not found'})
    drone = Drone.query.filter(Drone.id == drone_id,
                               Drone.user_id == user.id).first()
    if not drone:
        return jsonify({'status': 'error', 'error': 'drone not found'})

    flight.name = name
    flight.area_id = area.id
    flight.drone_id = drone.id
    flight.begin_at = time_obj
    flight.day_interval = interval
    flight.active = active
    db.session.commit()

    return jsonify({
        'status': 'updated',
        "flight": {
            "id": flight.id,
            "name": flight.name,
            'interval': flight.day_interval,
            'time': f'{flight.begin_at.strftime("%H:%M")}',
            'area': {
                'id': area.id,
                'name': area.name
            },
            'drone': {
                'id': drone.id,
                'name': drone.name
            }
        }
    }), 200
示例#9
0
文件: drones.py 项目: VladShg/Agrown
def delete_drone():
    json = request.get_json()
    user = User.verify_auth_token(json['token'])
    drone = Drone.query.filter(Drone.id == int(json['id']),
                               Drone.user_id == user.id).first()
    if len(drone.flights) != 0:
        return jsonify({'status': 'error', 'error': 'delete forbidden'}), 400
    if not drone:
        return jsonify({'status': "error", "error": 'drone not found'}), 400
    db.session.delete(drone)
    db.session.commit()
    return jsonify({'status': "deleted"}), 200
示例#10
0
def update_user():
    json = request.get_json()
    user = User.verify_auth_token(json['token'])
    check_users = User.query.filter(User.email == json['email']).all()
    check_users = [
        check_user for check_user in check_users if check_user.id != user.id
    ]
    if check_users:
        return jsonify({"error": "Email is already taken"})
    user.email = json['email']
    db.session.commit()
    return jsonify({"status": "Information updated"})
示例#11
0
文件: areas.py 项目: VladShg/Agrown
def delete_area():
    json = request.get_json()
    user = User.verify_auth_token(json['token'])
    area = Area.query.filter(Area.id == int(json['id']),
                             Area.user_id == user.id).first()
    if not area:
        return jsonify({'status': "error", "error": 'area not found'}), 400
    if len(area.flights) != 0:
        return jsonify({'status': 'error', 'error': 'delete forbidden'}), 400
    for point in area.points:
        db.session.delete(point)
    db.session.delete(area)
    db.session.commit()
    return jsonify({'status': "deleted"}), 200
示例#12
0
文件: drones.py 项目: VladShg/Agrown
def add_drone():
    json = request.get_json()
    name = json['name']
    code = int(json['code'])
    model_id = int(json['model_id'])
    user = User.verify_auth_token(json['token'])
    model = db.session.query(Model).filter(Model.id == model_id).first()
    drone_code = Drone.query.filter(Drone.user_id == user.id,
                                    Drone.code == code).first()
    if drone_code:
        return jsonify({"error": "duplicate code"}), 400
    drone = Drone(name=name, model_id=model.id, code=code, user_id=user.id)
    db.session.add(drone)
    db.session.commit()

    return jsonify({'status': 'created'}), 200
示例#13
0
文件: drones.py 项目: VladShg/Agrown
def get_drones():
    json = request.get_json()
    token = json['token']
    user = User.verify_auth_token(token)
    drones = user.drones
    data = [{
        "id": drone.id,
        "name": drone.name,
        "model": {
            "id": drone.model.id,
            "name": drone.model.name
        },
        "code": drone.code,
        "sync": drone.synced,
        "last_synced": drone.last_synced
    } for drone in drones]
    response = jsonify(data)
    return response, 200
示例#14
0
文件: areas.py 项目: VladShg/Agrown
def get_areas():
    json = request.get_json()
    token = json['token']
    user = User.verify_auth_token(token)
    user_areas = user.areas
    data = []
    for area in user_areas:
        points = []
        for point in area.points:
            points.append({'lat': point.latitude, 'lng': point.longitude})
        data.append({
            'id': area.id,
            'name': area.name,
            'crops_type': area.crops_type,
            'square': area.square,
            'points': points
        })

    response = jsonify(data)
    return response, 200
示例#15
0
文件: areas.py 项目: VladShg/Agrown
def update_area():
    json = request.get_json()
    user = User.verify_auth_token(json['token'])
    area = Area.query.filter(Area.user_id == user.id,
                             Area.id == int(json['id'])).first()
    if not area:
        return jsonify({'status': 'error', 'error': 'area not found'}), 400
    area.name = json['name']
    area.crops_type = json['crops_type']
    area.square = float(json['square'])
    db.session.query(RectanglePoint).filter(
        RectanglePoint.area_id == area.id).delete()
    db.session.commit()
    for point in json['points']:
        p = RectanglePoint(area_id=area.id,
                           latitude=float(point['lat']),
                           longitude=float(point['lng']))
        db.session.add(p)
    db.session.commit()
    area.name = json['name']
    db.session.commit()
    return jsonify({
        "status": "updated",
        "area": {
            "id":
            area.id,
            "name":
            area.name,
            'crops_type':
            area.crops_type,
            'square':
            area.square,
            "points": [{
                "lat": p.latitude,
                'lng': p.longitude
            } for p in area.points]
        }
    }), 200
示例#16
0
文件: drones.py 项目: VladShg/Agrown
def update_drone():
    json = request.get_json()
    user = User.verify_auth_token(json['token'])
    drone = Drone.query.filter(Drone.user_id == user.id,
                               Drone.id == int(json['id'])).first()
    if not drone:
        return jsonify({
            'status': 'error',
            'error': 'drone by id not found'
        }), 400
    drone_code = Drone.query.filter(Drone.user_id == user.id,
                                    Drone.code == int(json['code'])).all()
    drone_code = [drone for drone in drone_code if drone.id != int(json['id'])]
    if drone_code:
        return jsonify({"status": 'error', 'error': 'duplicate code'}), 400
    if drone.code != int(json['code']):
        drone.synced = False
    drone.code = int(json['code'])
    drone.name = json['name']
    model_id = int(json['model_id'])
    model = db.session.query(Model).filter(Model.id == model_id).first()
    drone.model_id = model.id
    db.session.commit()
    return jsonify({
        "status": "updated",
        "drone": {
            "id": drone.id,
            "name": drone.name,
            "model": {
                "id": drone.model.id,
                "name": drone.model.name
            },
            "code": drone.code,
            "sync": drone.synced,
            "last_synced": drone.last_synced
        }
    }), 200
示例#17
0
文件: auth.py 项目: VladShg/Agrown
def check_token():
    json = request.get_json()
    token = json['token']
    is_valid = User.verify_auth_token(token=token) is not None
    return jsonify({'token_is_valid': is_valid}), 200