def update_admin(admin_id, username, password): cur = get_db().cursor() cur.execute( 'UPDATE admin SET username = %s, password=%s WHERE id=%s', (username, generate_password_hash(password), admin_id) ) get_db().commit()
def register_new_admin(username, password): cur = get_db().cursor() cur.execute( 'INSERT INTO admin (id, username, password) VALUES (%s, %s, %s)', (get_uuid(), username, generate_password_hash(password)) ) get_db().commit()
def delete_model(model_name, model_id): """ :param model_name: :type model_name: str :param model_id: :type model_id: str :return: :rtype: None """ cur = get_db().cursor() cur.execute(build_delete(model_name), (model_id,)) get_db().commit()
def insert_model(model_name, attributes): """ Simple INSERT of new model to db :param model_name: :type model_name: str :param attributes: :type attributes: dict :return: :rtype: None """ attributes.pop('csrf_token', None) cur = get_db().cursor() insert_query = build_insert(model_name, attributes) values = tuple(n for n in [get_uuid()] + list(attributes.values())) cur.execute(insert_query, values) get_db().commit()
def update_model(model_name, model_id, attributes): """ :param model_name: :type model_name: str :param model_id: :type model_id: str :param attributes: :type attributes: dict :return: :rtype: """ attributes.pop('csrf_token', None) attributes.pop('id', None) cur = get_db().cursor() update_query = build_update(model_name, attributes) values = tuple(n for n in list(attributes.values()) + [model_id]) cur.execute(update_query, values) get_db().commit()
def get_all(model_name): """ simple get all models in db :param model_name: name of the model :type model_name: str :return: :rtype: list[dict] """ cur = get_db().cursor() cur.execute(f'SELECT * FROM `{model_name}`') return cur.fetchall()
def get_model_by_id(model_name, model_id): """ :param model_name: :type model_name: str :param model_id: :type model_id: str :return: :rtype: dict """ cur = get_db().cursor() cur.execute(f'SELECT * FROM `{model_name}` WHERE `id` = %s', (model_id,)) model = cur.fetchone() if model is None: abort(404, f"{model_name} id {0} doesn't exist.".format(model_id)) return model
def get_admins(): cur = get_db().cursor() cur.execute(f'SELECT * FROM admin') return cur.fetchall()
def get_admin_by(field_name, field_value): cur = get_db().cursor() cur.execute( f'SELECT * FROM admin WHERE {field_name} = %s', (field_value,) ) return cur.fetchone()
def get_owners(): cur = get_db().cursor() cur.execute( 'SELECT id, first_name, last_name FROM owner order by last_name') return cur.fetchall()
def get_pets(): cur = get_db().cursor() cur.execute('SELECT id, name FROM pet order by name') return cur.fetchall()