def __init__(self): # use to get location from lat, log coordinates. (https://opencagedata.com/tutorials/geocode-in-python) try: root_path = os.path.dirname(os.path.abspath(__file__)) with open(file=os.path.join(root_path, "GeoKey.txt"), mode="r") as f: key = f.read() self.__geocoder = OpenCageGeocode(key) except FileNotFoundError: app_logger.warning("File GeoKey.txt not found") raise AppException("File contain Key for geocoder not found.")
def delete_user(user_id): user: UserModel = UserModel.find_by_id(user_id) if not user: raise UserNotFound("The user id {} doesn't exist".format(user_id)) deleted = user.delete() if deleted: app_logger.info("User {} has been deleted".format(user.username)) else: app_logger.warning("User {} could't be deleted.".format(user.username)) return response.bool_to_response(deleted)
def load_user(request): # X-Access-Token come from the client side. # This header is add to requests at the token.interceptor at the Angular side. token = request.headers.get('X-Access-Token') if token: token_model = TokenModel.find_by(token=token) if token_model: return token_model.user # This is possible because of backref = user. else: app_logger.warning( "The toke {} don't exist in the database.".format(token)) return None app_logger.warning("Header request don't have a token.") return None
def delete_location(loc_id): loc: LocationModel = LocationModel.find_by_id(loc_id) if not loc: raise EntityNotFound("The location id {} doesn't exist".format(loc_id)) deleted = loc.delete() if deleted: app_logger.info("Location {} has been deleted at station {}.".format( loc.name, loc.station_id)) else: app_logger.warning( "Location {} could't be deleted at station {}.".format( loc.name, loc.station_id)) return response.bool_to_response(deleted)
def delete_station(station_id): station: StationModel = StationModel.find_by_id(station_id) if not station: raise EntityNotFound( "The station id {} doesn't exist".format(station_id)) deleted = station.delete() if deleted: app_logger.info("Station {} - {} has been deleted".format( station.name, station.creation_date)) else: app_logger.warning("Station {} - {} could't be deleted.".format( station.name, station.creation_date)) return response.bool_to_response(deleted)
def delete_attached_file(attached_id): attached: StationAttachedFileModel = StationAttachedFileModel.find_by_id( attached_id) if not attached: raise EntityNotFound( "The file id {} doesn't exist".format(attached_id)) deleted = attached.delete() if deleted: app_logger.info("Attached file {} has been deleted".format( attached.filename)) else: app_logger.warning("Channel {} could't be deleted.".format( attached.filename)) return response.bool_to_response(deleted)
def delete_channel(channel_id): channel: ChannelModel = ChannelModel.find_by_id(channel_id) if not channel: raise EntityNotFound( "The channel id {} doesn't exist".format(channel_id)) deleted = channel.delete() if deleted: app_logger.info("Channel {}-{}-{} has been deleted".format( channel.get_station().name, channel.name, DateUtils.convert_datetime_to_utc(channel.start_time))) else: app_logger.warning("Channel {}-{}-{} could't be deleted.".format( channel.get_station().name, channel.name, DateUtils.convert_datetime_to_utc(channel.start_time))) return response.bool_to_response(deleted)
def login(username=None, password=None): user = UserModel.find_by_username(username) if user and user.has_valid_password(password): # create a token for user. token_model = TokenModel.find_by_user_id(user.id) if not token_model: # create if token don't exist. token_model = TokenModel.create_token(user.id) if token_model.save(): app_logger.info("User {} logged in successfully.".format(username)) return response.model_to_response(user) else: app_logger.warning("User {} fail to login.".format(username)) return response.empty_response() app_logger.info("User {} has bad credentials.".format(username)) return response.empty_response()