Dependency Inversion w/Mongo Engine + Flask

Page defines our object’s attributes. This allows us to create an object instance with the following attributes: author, content, title, and meta_attributes.

/app/domain/page/page_model.py

from cms.services.schema.page_schema import PageSchema


class Page(PageSchema):
    """
    Page class
    """
    def __init__(self, author, content, title, meta_attributes, *args, **values):
        """
        Instantiate page object by establishing attribute values (i.e. Page.author = "Example User").

        Fields:
            author: str
                The username assigned to content
            content: str, optional
                The page's content
            title: str
                The page's title
            meta_attributes: dict
                Dict keys: account_id, page_status, create_date, last_update, template, slug, tag_list
        """
        super().__init__(*args, **values)
        self.author = author
        self.content = content
        self.title = title
        self.meta_attributes = meta_attributes

PageSchema is the parent class of Page and child class of PageDocument. This is a pass-through class which connects /app/domain/page/page_model.py to /app/adapters/mongodb/document/page_document.py.

/app/services/schema/page_schema.py

from cms.adapters.mongodb.document.page_document import PageDocument


class PageSchema(PageDocument):
    """
    Pass-through class 
    """

PageDocument establishes the MongoEngine fields along with the static method delete_page and class method update_page. This class actually utilizes our storage solution — MongoEngine.

/app/adapters/mongodb/document/page_document.py

from mongoengine import Document, StringField, DictField

from cms.adapters.mongodb.connection import connect_to_db, disconnect_from_db, save, delete_column


class PageDocument(Document):
"""
This is a mongoengine.Document subclass that establishes Page fields and class methods.

Fields:
author: str
The username assigned to content
content: str, optional
The page's content
title: str
The page's title
meta_attributes: dict
Dict keys: account_id, page_status, create_date, last_update, template, slug, tag_list
"""
meta = {'allow_inheritance': True}
author = StringField(required=True)
content = StringField(required=False)
title = StringField(required=True)
# Dictionary for miscellaneous settings
meta_attributes = DictField(required=True)

def __init__(self, *args, **values):
super().__init__(*args, **values)

@staticmethod
def delete_page(page_id: str):
connect_to_db()
delete_column(
PageDocument.objects(page_id=page_id)
)
disconnect_from_db()

@classmethod
def update_page(cls):
connect_to_db()
save(
PageDocument(
author=cls.author,
content=cls.content,
title=cls.title,
meta_attributes=cls.meta_attributes
)
)
disconnect_from_db()

Swapping Implementations

Create a new file app/adapters/mariadb/document/page_document.py with a PageDocument class utilizing MariaDB. Change the import from /app/services/schema/page_schema.py to reflect the change.

  cms.adapters.mariadb.document.page_document import PageDocument

About the author

Shane Bellone

Prefers coffee with py.

Add comment

By Shane Bellone