def _handle_patch_request(field: str): try: logging.info(f"User requested [{field}] change") if not _required_params(field): return make_response("Invalid parameter(s)", 406) if field=='votes': update_value='votes + 1' else: update_value = request.args.get('update') song_name = request.args.get('song_name') artist = request.args.get('artist') template = load_db_template(defaults.update_songs_column) query = template.render(field=field, update_value=update_value, song_name=song_name, artist=artist) connect_execute_query(query) logging.info(f"[{field}] successfully changed to {update_value}.") return make_response(f"Successfully changed [{field}] field.", 200) except Exception as e: logging.error(f"Unexpected error happened: {e}") return make_response("Unprocessable entity", 430)
def check_if_column_exists(cursor, table_name: str, column: str) -> bool: try: template = load_db_template(defaults.check_for_column) query = template.render(table_name=table_name, column=column) logging.info( f"Checking if column [{column}] in table [{table_name}] exists.") cursor.execute(query) if [result[0] for result in cursor]: logging.warning(f"Column [{column}] already exist.") return True return False except Exception as e: logging.exception(f"Exception while checking if [{column}] exists.") return None
def create_table(cursor, table_name, template_name: str, primary_key: str, foreign_key: str = "") -> bool: try: if not check_if_table_exists(cursor, table_name): template = load_db_template(template_name) query = template.render(primary_key=primary_key, foreign_key=foreign_key) logging.info(f"Executing query: {query}") cursor.execute(query) logging.info(f"Table {table_name} created") else: logging.warning(f"Table {table_name} already exists.") return True except Exception as e: logging.error(f"Error happened while creating a table [{table_name}]: {e}") return False
def input_user(my_db, cursor) -> bool: if not check_if_table_exists(cursor, "users"): logging.error("Table [users] doesn't exist. Can't input users.") return False template = load_db_template(defaults.user_insert) users = create_users() for user in users: insert_query = template.render(user=user['user'], password=user['password'], token=user['token']) cursor.execute(insert_query) my_db.commit() logging.info(f"Users successfully added.") return True
def input_song(self) -> bool: if self.check_if_song_exists(): logging.info( f"Adding [{self.artist.upper()}:{self.song_name}] song into db." ) template = load_db_template(defaults.song_insert) insert_query = template.render(song_name=self.song_name, artist=self.artist, genre=self.genre, yt_link=self.yt_link, token=self.token) connect_execute_query(insert_query) return True return False
def add_table_column(cursor, table_name: str, column: str, data_type: str, default: str = ""): try: if not check_if_column_exists(cursor, table_name, column): template = load_db_template(defaults.add_column) query = template.render(table_name=table_name, column=column, data_type=data_type, default=default) logging.info(f"Executing query: [{query}]") cursor.execute(query) logging.info(f"Added column [{column}] to table [{table_name}]") except Exception as e: logging.exception( f"Exception [{e}] was made while adding column [{column}] in [{table_name}]." )
def input_songs(my_db, cursor) -> bool: if not check_if_table_exists(cursor, 'songs'): logging.warning("Table [songs] doesn't exist. Can't input songs.") return False template = load_db_template(defaults.song_insert) songs = create_songs() for song in songs: insert_query = template.render(song_name=song['song_name'], artist=song['artist'], genre=song['genre'], yt_link=song['yt_link'], token=song['token']) cursor.execute(insert_query) my_db.commit() logging.info("Songs successfully added to DB.") return True
def input_user(self) -> bool: if len(self.name) <= 3: logging.info("Username must have more than 3 characters.") return False if len(self.password) <= 4: logging.info("Password must have more than 4 characters.") return False if self.validate_user(): logging.info(f"Adding {self.name} user into db.") token = str(uuid.uuid4()) template = load_db_template(defaults.user_insert) insert_query = template.render(user=self.name, password=generate_hash( self.password), token=token) connect_execute_query(insert_query) return True return False