09:ビュークラスによるビューの整理(09: Organizing Views With View Classes)

ビュー・ファンクションをビュー・クラスのメソッドに変更し、宣言のいくつかをクラスレベルに移動します。

背景(Background)

これまでのビューは単純で独立した関数でした。多くの場合、ビューはお互いに関連しています。同じデータを表示または操作するさまざまな方法やまたは複数の操作を処理するREST APIで構成されている場合があります。これらのビューを view class としてグループ化することは意味があります

  • グループビュー。
  • 反復的なデフォルトをまとめる。
  • ステートやヘルパーを共有する。

このステップでは既存のビューをビュー・クラスに変換するために必要最小限の変更を行います。後のチュートリアルのステップではビュークラスについて熟慮します。

目標(Objectives)

  • 関連するビューをビュー・クラスにグループ化します。
  • クラスレベル @view_defaults で構成をまとめします。

手順(Steps)

  1. 最初に前の手順の結果をコピーします:

    $ cd ..; cp -r templating view_classes; cd view_classes
    $ $VENV/bin/pip install -e .
    
  2. view_classes/tutorial/views.py のビュークラスにはは2つのビューがあります:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    from pyramid.view import (
        view_config,
        view_defaults
        )
    
    @view_defaults(renderer='home.pt')
    class TutorialViews:
        def __init__(self, request):
            self.request = request
    
        @view_config(route_name='home')
        def home(self):
            return {'name': 'Home View'}
    
        @view_config(route_name='hello')
        def hello(self):
            return {'name': 'Hello View'}
    
  3. view_classes/tutorial/tests.py のユニットテストはy実行されないので、ビュークラスをインポートするように変更してレスポンスを取得する前にインスタンスを作成しましょう:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    import unittest
    
    from pyramid import testing
    
    
    class TutorialViewTests(unittest.TestCase):
        def setUp(self):
            self.config = testing.setUp()
    
        def tearDown(self):
            testing.tearDown()
    
        def test_home(self):
            from .views import TutorialViews
    
            request = testing.DummyRequest()
            inst = TutorialViews(request)
            response = inst.home()
            self.assertEqual('Home View', response['name'])
    
        def test_hello(self):
            from .views import TutorialViews
    
            request = testing.DummyRequest()
            inst = TutorialViews(request)
            response = inst.hello()
            self.assertEqual('Hello View', response['name'])
    
    
    class TutorialFunctionalTests(unittest.TestCase):
        def setUp(self):
            from tutorial import main
            app = main({})
            from webtest import TestApp
    
            self.testapp = TestApp(app)
    
        def test_home(self):
            res = self.testapp.get('/', status=200)
            self.assertIn(b'<h1>Hi Home View', res.body)
    
        def test_hello(self):
            res = self.testapp.get('/howdy', status=200)
            self.assertIn(b'<h1>Hi Hello View', res.body)
    
  4. テストを実行します:

    $ $VENV/bin/py.test tutorial/tests.py -q
    ....
    4 passed in 0.34 seconds
    
  5. Pyramidアプリケーションを以下のように実行します:

    $ $VENV/bin/pserve development.ini --reload
    
  6. ブラウザで http://localhost:6543/http://localhost:6543/howdy を開きます。

分析(Analysis)

ビュークラスへの移行を簡単にしたかったので新しい機能は導入してません。ビュー関数をビュークラスのメソッドに変更してテストを更新しました。

TutorialViews ビュークラスでは、2つのビュークラスが共通のクラスのメソッドとして論理的にグループ化されていることがわかります。2つのビューは同じテンプレートを共有しているため、 @view_defaults デコレータのクラスレベルに移動できます。

テストを変更する必要がありました。明らかにビュークラスをインポートする必要がありました。しかしながら最初にダミーリクエストでビュークラスをインスタンス化して、次にテストされているビューメソッドを呼び出すテストでパターンを見れます。