Пример #1
0
def get_average_distance(username):
    time_cutoff = datetime.datetime.now() - datetime.timedelta(7)
    user = User.query.filter_by(username=username).first()
    joggs = Jogg.query.filter_by(user_id=user.id).filter(
        Jogg.created_date >= time_cutoff).all()
    distances = [extract_jogg_distance(jogg) for jogg in joggs]
    return validation_util.success_message(data={
        'average_distance(km)':
        safe_division(sum(distances), len(distances))
    })
Пример #2
0
def delete_jogg(jogg_id, deleted_by=None, force=False):
    jogg = Jogg.query.filter_by(id=jogg_id).first()
    if jogg:
        if force or jogg.user_id == deleted_by:
            db.session.delete(jogg)
            db.session.commit()
            return validation_util.success_message(
                data=f"Successfully deleted jogg with id {jogg.id}")
    return validation_util.error_message(
        message=f"Unable to delete jogg with id {jogg.id}")
Пример #3
0
def delete_user(username):
    user = User.query.filter_by(username=username).first()
    if user:
        jogg = Jogg.query.filter_by(user_id=user.id).first()
        if jogg:
            return validation_util.error_message(
                message=
                "Cannot delete user. User has associated jogg objects created. Delete all user's joggs first"
            )
        db.session.delete(user)
        db.session.commit()
        return validation_util.success_message("Deleted Successfully!")
    return validation_util.error_message("User does not exist")
Пример #4
0
def update_jogg(jogg_id,
                username,
                start_time,
                end_time,
                start_location,
                end_location,
                auto_create=False):
    existing_jogg = Jogg.query.get(jogg_id)
    if existing_jogg:
        if username:
            user = User.query.filter_by(username=username).first()
            existing_jogg.user_id = user.id

        start_weather_update = check_start_weather_data(
            existing_jogg, start_location, start_time)
        end_weather_update = check_end_weather_data(existing_jogg,
                                                    end_location, end_time)
        start_time = start_time if start_time else existing_jogg.start_time
        end_time = end_time if end_time else existing_jogg.end_time
        start_location = location_util.get_location_object(
            start_location) if start_location else Location(
                existing_jogg.start_lat, existing_jogg.lon)
        end_location = location_util.get_location_object(
            end_location) if end_location else Location(
                existing_jogg.end_lat, existing_jogg.end_lon)

        existing_jogg.start_time = datetime.fromtimestamp(start_time)
        existing_jogg.end_time = datetime.fromtimestamp(end_time)
        existing_jogg.start_lat = start_location.lat
        existing_jogg.start_lon = start_location.lon
        existing_jogg.end_lat = end_location.lat
        existing_jogg.end_lon = end_location.lon

        if start_weather_update:
            existing_jogg.start_weather = json.dumps(
                weather_service.get_weather_forecast_from_server(
                    start_location.lat, start_location.lon, start_time))
        if end_weather_update:
            existing_jogg.end_weather = json.dumps(
                weather_service.get_weather_forecast_from_server(
                    end_location.lat, end_location.lon, end_time))

        db.session.commit()
        return validation_util.success_message(
            data=jogg_schema.dump(existing_jogg))
    else:
        if auto_create:
            return create_jogg(username, start_time, end_time, start_location,
                               end_location)
        return validation_util.error_message(
            message="No Jogg found with given ID.")
Пример #5
0
def update_user(username, password, type, auto_create=False):
    user = User.query.filter_by(username=username).first()
    if not user:
        if auto_create:
            return create_user(username, password, type)
        else:
            return validation_util.error_message(
                message="Cannot update user! Invalid User Details")
    else:
        user.username = username
        if password:
            user.password = bcrypt.generate_password_hash(password)
        if type:
            user.type = type
        db.session.commit()
        return validation_util.success_message(data=user_schema.dump(user))
Пример #6
0
def create_user(username, password, type):
    user = User.query.filter_by(username=username).first()
    if user:
        return validation_util.error_message(message="User Already Registered")
    else:
        err = validation_util.validate({
            "username": username,
            "password": password,
            "type": type
        })
        if err:
            return err
        user = User(username=username,
                    password=bcrypt.generate_password_hash(password),
                    type=type)
        db.session.add(user)
        db.session.commit()
        return validation_util.success_message(data=user_schema.dump(user))
Пример #7
0
def create_jogg(username, start_time, end_time, start_location, end_location):
    user = User.query.filter_by(username=username).first()
    if user:
        user_id = user.id
    else:
        return validation_util.error_message(
            message="Cannot create Jogg without a Valid User")

    err = validation_util.validate({
        "start_time": start_time,
        "end_time": end_time,
        "start_location": start_location,
        "end_location": end_location
    })
    if err:
        return err

    if not isinstance(start_time, int) or not isinstance(end_time, int):
        validation_util.error_message(
            f"Invalid start_time and end_time timestamps. Expecting integers")

    start_location = location_util.get_location_object(start_location)
    end_location = location_util.get_location_object(end_location)

    start_weather = json.dumps(
        weather_service.get_weather_forecast_from_server(
            start_location.lat, start_location.lon, start_time))
    end_weather = json.dumps(
        weather_service.get_weather_forecast_from_server(
            end_location.lat, end_location.lon, end_time))

    new_jogg = Jogg(user_id=user_id,
                    start_time=datetime.fromtimestamp(start_time),
                    end_time=datetime.fromtimestamp(end_time),
                    start_lat=start_location.lat,
                    start_lon=start_location.lon,
                    end_lat=end_location.lat,
                    end_lon=end_location.lon,
                    start_weather=start_weather,
                    end_weather=end_weather)
    db.session.add(new_jogg)
    db.session.commit()
    return validation_util.success_message(data=jogg_schema.dump(new_jogg))
Пример #8
0
def get_users_paginated(user_types=None, page=1, per_page=10):
    if user_types is None:
        user_types = ["User"]
    return validation_util.success_message(data=users_schema.dump(
        User.query.order_by(User.created_date.desc()).filter(
            User.type.in_(user_types)).paginate(page, per_page).items))
Пример #9
0
def get_user(username):
    return validation_util.success_message(
        data=user_schema.dump(User.query.filter_by(username=username).first()))
Пример #10
0
def get_paginated_joggs_for_user(username, page=1, per_page=10):
    user = User.query.filter_by(username=username).first()
    return validation_util.success_message(data=joggs_schema.dump(
        Jogg.query.filter_by(user_id=user.id).order_by(
            Jogg.created_date.desc()).paginate(page, per_page).items))
Пример #11
0
def get_paginated_joggs(page=1, per_page=10):
    return validation_util.success_message(data=joggs_schema.dump(
        Jogg.query.order_by(Jogg.created_date.desc()).paginate(
            page, per_page).items))
Пример #12
0
def get_jogg_by_id(jogg_id):
    return validation_util.success_message(
        data=jogg_schema.dump(Jogg.query.get(jogg_id)))