def get_user_by_username(username: str): user = UserModel.find_by_username(username) if user: return response.model_to_response(user) return response.empty_response()
def get_users(): all_users: List[UserModel] = UserModel.get_all() if all_users: return response.model_to_response(all_users) return response.empty_response()
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()
def user_self_update(user: dict): # update user updated_user = UserModel.update_user(user, current_user.id, is_self_update=True) if not updated_user: raise PermissionDenied( "Either, this user don't exists or you are trying to update someone that is not you." ) if updated_user.save(): return response.model_to_response(updated_user) return response.empty_response()
def attach_file(file: FileStorage, station_id): print(file) if file.content_type != 'application/pdf': raise AppException("File is not a valid pdf.") station_attached = StationAttachedFileModel.find_by(station_id=station_id, filename=file.filename, get_first=True) if not station_attached: station_attached = StationAttachedFileModel.create( file.filename, station_id) if station_attached.save(): try: station_attached.write_file(file) except IOError as e: app_logger.error(str(e)) file.close() return response.empty_response()
def get_rights(): rights: List[RightModel] = RightModel.get_all(order_by=RightModel.label) if rights: return response.model_to_response(rights) return response.empty_response()
def get_roles(): roles: List[RoleModel] = RoleModel.get_all(order_by=RoleModel.label) if roles: return response.model_to_response(roles) return response.empty_response()
def get_user(user_id: str): user: UserModel = UserModel.find_by_id(user_id) if user: return response.model_to_response(user) return response.empty_response()
def download_file(sd: SeismicDataModel): if sd.is_public(): return flask.send_file(sd.file_path, mimetype="mseed", attachment_filename=sd.filename) return response.empty_response()
def get_attached_to_station(station_id: str): attached_list = StationAttachedFileModel.find_by(station_id=station_id, get_first=False) if attached_list: return response.model_to_response(attached_list) return response.empty_response()
def get_channel(channel_id: str): channel = ChannelModel.find_by_id(channel_id) if channel: return response.model_to_response(channel) return response.empty_response()
def get_location_model(location_id: str): location = LocationModel.find_by_id(location_id) if location: return response.model_to_response(location) return response.empty_response()
def get_station(station_id: str): station = StationModel.find_by_id(station_id) if station: return response.model_to_response(station) return response.empty_response()