def modify_jogg(username): if current_identity.username == username: if request.method == 'POST' or request.method == 'PUT': data = request.json if data.get('jogg_id') and jogg_service.get_jogg_by_id( data.get('jogg_id')): existing_jogg = jogg_service.get_jogg_by_id( data.get('jogg_id')) if existing_jogg.get('user_id') == current_identity.id: return jsonify( jogg_service.update_jogg( jogg_id=data.get('jogg_id'), username=data.get('username'), start_time=data.get('start_time'), end_time=data.get('end_time'), start_location=data.get('start_location'), end_location=data.get('end_location'), auto_create=True)) else: return jsonify( validation_util.error_message( message="Invalid Request")) else: return jsonify( jogg_service.update_jogg( jogg_id=data.get('jogg_id'), username=current_identity.username, start_time=data.get('start_time'), end_time=data.get('end_time'), start_location=data.get('start_location'), end_location=data.get('end_location'), auto_create=True)) elif request.method == 'DELETE': data = request.json if data and 'jogg_id' in data: existing_jogg = jogg_service.get_jogg_by_id( data.get('jogg_id')).get('data') if existing_jogg and existing_jogg.get( 'user_id') == current_identity.id: return jsonify( jogg_service.delete_jogg( data.get('jogg_id'), deleted_by=current_identity.id)) else: return jsonify( validation_util.error_message( message="Invalid Request")) else: return validation_util.error_message( message="Could not delete jogg. Invalid jogg id detected") else: return jsonify( validation_util.error_message(message="Invalid Request"))
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")
def get_average_user_distance(username): if current_identity.username == username: return jsonify( user_service.get_average_distance(current_identity.username)) else: return jsonify( validation_util.error_message(message="Invalid Request"))
def get_joggs(username, page=1): if current_identity.username == username: return jsonify( jogg_service.get_paginated_joggs_for_user( current_identity.username, page=page)) else: return jsonify( validation_util.error_message(message="Invalid Request"))
def get_user(username=None): if username: if current_identity.username == username: return jsonify(user_service.get_user(current_identity.username)) else: return jsonify( validation_util.error_message(message="Invalid Request")) else: return jsonify(user_service.get_user(current_identity.username))
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))
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}")
def modify_user(): data = request.json if 'username' not in data: validation_util.error_message(message="Invalid Operation! Need a valid Username") username = data.get("username") user = User.query.filter_by(username=username).first() if user: if user.type == "Root": validation_util.error_message(message="Invalid Operation! Invalid permissions to modify a Root User") if user.type == "Admin" and current_identity.type != "Root": validation_util.error_message(message="Invalid Operation! Invalid permissions to modify a Admin User") if request.method == 'POST': password = data.get("password") user_type = data.get("type") if user_type == "Root": validation_util.error_message(message="Invalid Operation! Invalid permissions to modify a Root User") elif user_type == "Admin" and current_identity.type != "Root": validation_util.error_message(message="Invalid Operation! Invalid permissions to modify a Admin User") return jsonify(user_service.update_user(username, password, user_type, auto_create=True)) elif request.method == 'DELETE': return jsonify(user_service.delete_user(username))
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.")
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))
def get_location_object(coordinates): """ Utility function to check that given coordinates are in valid format and returns instance of Location class :param coordinates: :return: Location """ if not isinstance(coordinates, dict): return validation_util.error_message( message="Invalid Location format, expected dict of lat and lon") err = validation_util.validate({ "lat": coordinates.get("lat"), "lon": coordinates.get("lon") }) if err: return err return Location(coordinates["lat"], coordinates["lon"])
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))