Using mixins

Now we are going to use AnyBlok mixins to:

  • Refactor our code in order to use IdColumn mixin to create the id primary key column on the room model
  • Use the TrackModel mixin that adds 2 fields to track creation and last modification date:

Note: You can use III-03_create-model directory from AnyBlok/anyblok-book-examples repository to get ready to start with this chapter.

Install anyblok_mixins

Add [anyblok_mixins][pypi_anyblok_mixins] to your package requirements and install the package.

 # file: setup.py

 requirements = [
     'anyblok',
     'psycopg2',
     'anyblok_pyramid',
     'anyblok_address',
+    'anyblok_mixins',
 ]

Declare anyblok-mixins blok as required in the Room blok init file.

  # file: rooms_booking/room/__init__.py

  class Room(Blok):
     """Room's Blok class definition
     """
     version = "0.1.0"
     author = "Pierre Verkest"
-    required = ['anyblok-core', 'address']
+    required = ['anyblok-core', 'address', 'anyblok-mixins']

Add test

In the following test we make sure as requested by our customer that edit_date field is properly updated after a change occurs on a room.

# file: rooms_booking/room/tests/test_room.py

from datetime import datetime
import time
import pytz


class TestRoom:
    """Test Room model"""

    [...]

    def test_track_modification_date(self, rollback_registry):
        registry = rollback_registry
        before_create = datetime.now(tz=pytz.timezone(time.tzname[0]))
        room = registry.Room.insert(
            name="A1",
            capacity=25,
        )
        room.refresh()
        after_create = datetime.now(tz=pytz.timezone(time.tzname[0]))
        room.name = "A2"
        registry.flush()
        after_edit = datetime.now(tz=pytz.timezone(time.tzname[0]))
        assert before_create <= room.create_date <= after_create
        assert after_create <= room.edit_date <= after_edit

Note: room.refresh() is used to clear cache on the ORM side to request data from the database. It will perform a flush before if necessary.

Note: registry.flush() is used here to force sending data to the database as edit_date field is configured with an auto_update option wich is called before sending data to the database.

Those methods are rarely used in your code but can be tricky while playing with some kind of fields.

Reuse mixins

Using multiple inheritance capability we inherits from Declarations.Mixin.IdColumn and Declarations.Mixin.TrackModel. Diff result:

 from anyblok import Declarations
 from anyblok.column import String, Integer

 Model = Declarations.Model
+Mixin = Declarations.Mixin
+
 register = Declarations.register


 @register(Model)
-class Room:
+class Room(Mixin.IdColumn, Mixin.TrackModel):

-    id = Integer(primary_key=True)
     name = String(label="Room name", nullable=False, index=True)
     capacity = Integer(label="Capacity", nullable=False)

You should now be able to launch previously added tests and make them pass.

results matching ""

    No results matching ""