コード例 #1
0
ファイル: book.py プロジェクト: miyamotok0105/python_sample
def get_book_and_check(book_id):
    """書籍の取得と存在チェックのための関数"""

    # 書籍データの取得
    db = get_db()
    book = db.execute(
        'SELECT * FROM book WHERE id = ? ', (book_id,)
    ).fetchone()

    if book is None:
        abort(404, 'There is no such book!!')

    return book
コード例 #2
0
ファイル: auth.py プロジェクト: miyamotok0105/python_sample
def load_logged_in_user():
    """
    どのURLが要求されても、ビュー関数の前で実行される関数
    ログインしているか確認し、ログインされていればユーザー情報を取得する
    """
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        db = get_db()
        g.user = db.execute(
            'SELECT * FROM user WHERE id = ?', (user_id,)
        ).fetchone()
コード例 #3
0
ファイル: auth.py プロジェクト: miyamotok0105/python_sample
def create_user():
    """
    GET :ユーザー登録画面に遷移
    POST:ユーザー登録処理を実施
    """
    if request.method == 'GET':
        # ユーザー登録画面に遷移
        return render_template('auth/create_user.html',
                               title='ユーザー登録',
                               year=datetime.now().year)


    # ユーザー登録処理

    # 登録フォームから送られてきた、ユーザー名とパスワードを取得
    username = request.form['username']
    password = request.form['password']

    # DBと接続
    db = get_db()

    # エラーチェック
    error_message = None

    if not username:
        error_message = 'ユーザー名の入力は必須です'
    elif not password:
        error_message = 'パスワードの入力は必須です'
    elif db.execute('SELECT id FROM user WHERE username = ?', (username,)).fetchone() is not None:
        error_message = 'ユーザー名 {} はすでに使用されています'.format(username)


    if error_message is not None:
        # エラーがあれば、それを画面に表示させる
        flash(error_message, category='alert alert-danger')
        return redirect(url_for('auth.create_user'))


    # エラーがなければテーブルに登録する
    # パスワードはハッシュ化したものを登録
    db.execute(
        'INSERT INTO user (username, password) VALUES (?, ?)',
        (username, generate_password_hash(password))
    )
    db.commit()

    # ログイン画面へ遷移
    flash('ユーザー登録が完了しました。登録した内容でログインしてください', category='alert alert-info')
    return redirect(url_for('auth.login'))
コード例 #4
0
ファイル: book.py プロジェクト: miyamotok0105/python_sample
def update_book(book_id):
    """
    GET :書籍更新画面に遷移
    POST:書籍更新処理を実施
    """

    # 書籍データの取得と存在チェック
    book = get_book_and_check(book_id)

    if request.method == 'GET':
        # 書籍更新画面に遷移
        return render_template('book/update_book.html',
                               book=book,
                               title='書籍の編集',
                               year=datetime.now().year)

    # 書籍編集処理

    # 登録フォームから送られてきた値を取得
    title = request.form['title']
    auther = request.form['auther']
    publisher = request.form['publisher']

    # DBと接続
    db = get_db()

    # エラーチェック
    error_message = None

    if not title:
        error_message = '書籍タイトルの入力は必須です'

    if error_message is not None:
        # エラーがあれば、それを画面に表示させる
        flash(error_message, category='alert alert-danger')
        return redirect(url_for('book.update_book'))


    # エラーがなければテーブルに登録する
    db.execute(
        'UPDATE book SET title=?, auther=?, publisher=? WHERE id=?',
        (title, auther, publisher, book_id)
    )
    db.commit()

    # 書籍一覧画面へ遷移
    flash('書籍が編集されました', category='alert alert-info')
    return redirect(url_for('book.index_book'))
コード例 #5
0
ファイル: book.py プロジェクト: miyamotok0105/python_sample
def create_book():
    """
    GET :書籍登録画面に遷移
    POST:書籍登録処理を実施
    """
    if request.method == 'GET':
        # 書籍登録画面に遷移
        return render_template('book/create_book.html',
                               title='書籍の追加',
                               year=datetime.now().year)


    # 書籍登録処理

    # ユーザーIDを取得
    user_id = session.get('user_id')

    # 登録フォームから送られてきた値を取得
    title = request.form['title']
    auther = request.form['auther']
    publisher = request.form['publisher']

    # DBと接続
    db = get_db()

    # エラーチェック
    error_message = None

    if not title:
        error_message = '書籍タイトルの入力は必須です'

    if error_message is not None:
        # エラーがあれば、それを画面に表示させる
        flash(error_message, category='alert alert-danger')
        return redirect(url_for('book.create_book'))

    # エラーがなければテーブルに登録する
    db.execute(
        'INSERT INTO book (user_id, title, auther, publisher) VALUES (?, ?, ?, ?)',
        (user_id, title, auther, publisher)
    )
    db.commit()

    # 書籍一覧画面へ遷移
    flash('書籍が追加されました', category='alert alert-info')
    return redirect(url_for('book.index_book'))
コード例 #6
0
ファイル: book.py プロジェクト: miyamotok0105/python_sample
def index_book():
    """書籍の一覧を取得する"""

    # DBと接続
    db = get_db()

    # 書籍データを取得
    user_id = session.get('user_id')
    books = db.execute(
        'SELECT * FROM book WHERE user_id = ? ORDER BY id DESC', (user_id,)
    ).fetchall()

    # 書籍一覧画面へ遷移
    return render_template('book/index_book.html',
                           books=books,
                           title='ログイン',
                           year=datetime.now().year)
コード例 #7
0
ファイル: auth.py プロジェクト: miyamotok0105/python_sample
def login():
    """
    GET :ログイン画面に遷移
    POST:ログイン処理を実施
    """
    if request.method == 'GET':
        # ログイン画面に遷移
        return render_template('auth/login.html',
                               title='ログイン',
                               year=datetime.now().year)


    # ログイン処理

    # ログインフォームから送られてきた、ユーザー名とパスワードを取得
    username = request.form['username']
    password = request.form['password']

    # DBと接続
    db = get_db()

    # ユーザー名とパスワードのチェック
    error_message = None

    user = db.execute(
        'SELECT * FROM user WHERE username = ?', (username,)
    ).fetchone()

    if user is None:
        error_message = 'ユーザー名が正しくありません'
    elif not check_password_hash(user['password'], password):
        error_message = 'パスワードが正しくありません'

    if error_message is not None:
        # エラーがあればそれを表示したうえでログイン画面に遷移
        flash(error_message, category='alert alert-danger')
        return redirect(url_for('auth.login'))


    # エラーがなければ、セッションにユーザーIDを追加してインデックスページへ遷移
    session.clear()
    session['user_id'] = user['id']
    flash('{}さんとしてログインしました'.format(username), category='alert alert-info')
    return redirect(url_for('home'))
コード例 #8
0
ファイル: book.py プロジェクト: miyamotok0105/python_sample
def delete_book(book_id):
    """
    GET :書籍削除確認画面に遷移
    POST:書籍削除処理を実施
    """
    # 書籍データの取得と存在チェック
    book = get_book_and_check(book_id)

    if request.method == 'GET':
        # 書籍削除確認画面に遷移
        return render_template('book/delete_book.html',
                               book=book,
                               title='書籍の削除',
                               year=datetime.now().year)

    # 書籍の削除処理

    db = get_db()
    db.execute('DELETE FROM book WHERE id = ?', (book_id,))
    db.commit()

    # 書籍一覧画面へ遷移
    flash('書籍が削除されました', category='alert alert-info')
    return redirect(url_for('book.index_book'))