def try_connect_db(): try: connection = create_connection(db_name="war2") return connection except Exception as err: print("Database connection failed: {}".format(err)) return False,
def set_db(config): Base, db_session, engine = create_connection(config) Package, InstallMethod = register_models(Base) init_db(Base, engine) return Base, db_session, Package, InstallMethod @app.teardown_appcontext def shutdown_session(exception=None): db_session.remove()
def delete_user(username: User, password) -> None: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): user.delete(cursor) cursor.close() connection.close() else: raise WrongParameterError("Wrong login or password!")
def login_user(username: str, hash_password: str) -> Union[User, None]: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) cursor.close() connection.close() if user and user.check_password(hash_password): return True else: raise WrongParameterError("Wrong login or password!")
def create_user(username: str, password: str) -> User: connection = create_connection() cursor = get_cursor(connection) if not User.get_by_username(cursor, username): user = User._create_user_object(username, password, username) user.save(cursor) cursor.close() connection.close() else: raise WrongParameterError("This user already exist!")
def print_all_users() -> List[Union[User, None]]: """Print all users which are in database""" connection = create_connection() cursor = get_cursor(connection) all_users = User.get_all(cursor) cursor.close() connection.close() if all_users: return all_users else: raise WrongParameterError("There is no users!")
def change_password(username: User, password: str, new_password: str) -> None: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): user.set_password(new_password) user.save(cursor) cursor.close() connection.close() else: raise WrongParameterError("Wrong login or password!")
def list_messages_to_user(username, password) -> List[Union[Message, None]]: connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): messages = Message.load_all_messages_for_user( cursor, user.username) cursor.close() connection.close() return messages else: raise WrongParameterError("Wrong login or password!")
def send_message(username, password, to_id, text) -> Message: """Create message to adress (User) to sender (User) into database.""" connection = create_connection() cursor = get_cursor(connection) user = User.get_by_username(cursor, username) if user and user.check_password(password): message = Message() message._id = -1 message.from_id = user.id message.to_id = to_id message.tekst = text message.creation_date = datetime.now() message.save(cursor) cursor.close() connection.close() else: raise WrongParameterError("Wrong login or password!")
parser.add_argument('--edit', '-e', help='Edit', action='store_true') parser.add_argument('--delete', '-d', help='Delete user', action='store_true') parser.add_argument('--list', '-l', help='List of user or massages', action='store_true') parser.add_argument('--send', '-s', help='Send', action='store') parser.add_argument('--to', '-t', help='Address of message', action='store') if __name__ == '__main__': args = parser.parse_args() print(args) dispacher = Dispacher() salt = generate_salt() connection = create_connection() cursor = get_cursor(connection) option_handler = OptionsHandler(args.password, args.username, args.new_password, args.edit, args.delete, args.list, args.send, args.to) if option_handler.create_user: print(dispacher.create_user(args.username, args.password)) elif option_handler.list_all_users: print(dispacher.print_all_users()) elif option_handler.list_all_messages_for_user: print(dispacher.list_messages_to_user(args.username, args.password)) elif option_handler.change_password: print( dispacher.change_password(args.username, args.password,
from models import create_connection, close_connection from models.parser import run (cnx, cursor) = create_connection() run(cursor) close_connection(cnx, cursor)
def set_db(config): Base, db_session, engine = create_connection(config) Package, InstallMethod = register_models(Base) init_db(Base, engine) return Base, db_session, Package, InstallMethod