Skip to main content

ContentfulORM


A Python toolkit for Contentful to let you create/maintain your Content Type and queries in ORM style.

Install


  • To install:
    pip install git+https://github.com/Phoenix-Chen/ContentfulORM.git
    

Usage


ORM Environment


  • Create an ORM Environment:
    import contentful_management
    from contentful_orm import ORMEnvironment
    
    # First create an contentful_management.environment.Environment
    client = contentful_management.Client('CONTENTFUL_MANAGEMENT_TOKEN')
    space = client.spaces().find('CONTENTFUL_SPACE_ID')
    environment = space.environments().find('CONTENTFUL_ENVIRONMENT')
    
    # Then create ORMEnvironment use contentful_management.environment.Environment
    orm_env = ORMEnvironment.from_parent(environment)
    

Content Type Model


  • Model your content type:
    from datetime import datetime
    from contentful_orm.models import Model
    from contentful_orm.fields import ArrayField, BooleanField, DateField, DecimalField, IntegerField, MediaField, ReferenceField, SymbolField, TextField, LocationField
    from contentful_orm.fields.validations import In, Range, Unique, Size, Regex, ImageDimensions, FileSize
    
    # class name will become content type name
    # class name in camel case will become content type id
    class Person(Model):
        # docstring will become content type description
        """Person model description
        """
        __display_field__ = 'name'
    
        # Each field need to be a {SomeType}Field from contentful_orm.fields
        # Most of the fields have keyword argument: disabled, localized, omitted, required, validations and default
        # (disabled, localized, omitted and required default to False. validations defaults to []. default defaults to None)
        email = SymbolField(validations=[Unique, Regex('^\\w[\\w.-]*@([\\w-]+\\.)+[\\w-]+$', error_msg='Invalid email address.')], required=True)
        name = SymbolField(localized=True, required=True)
        # ArrayField takes an argument to specify content type
        title = ArrayField(SymbolField(validations=[In(['Manager', 'Seller'], error_msg='Invalid title')]), localized=True)
        age = IntegerField(validations=[Range(min=1, error_msg='age must be a positive integer.')])
        created_date = DateField(default=datetime.now().strftime("%Y-%m-%dT%H:%M-00:00"))
    
    class Company(Model):
        """Company model description
        """
        __display_field__ = 'name'
    
        name = SymbolField(validations=[Unique], required=True)
        employees = ArrayField(ReferenceField(model_set={Person}, error_msg='employee has to be a Person entry'))
        address = LocationField()
    
    class Product(Model):
        """Product model description
        """
        __display_field__ = 'name'
    
        name = SymbolField(validations=[Unique], required=True)
        description = TextField(localized=True)
        # ReferenceField takes an additional keyword argument model_set to restrict reference content type.
        # (Currently doesn't support reference to the content type itself)
        seller = ReferenceField(model_set={Person, Company}, error_msg="seller has to be a Person or Company entry")
        images = ArrayField(
            MediaField(
                # validations takes a list of contentful_orm.fields.validations
                # See: https://www.contentful.com/r/knowledgebase/validations/ for details
                validations=[
                    ImageDimensions(max_width=12, max_height=12),
                    FileSize(max=40000, error_msg='Maximum file size is 40000 Bytes')
                ]),
            validations=[Size(max=10, error_msg="At most 10 images")]
        )
        sponsored = BooleanField(required=True)
        price = DecimalField(required=True)
    
  • Create and publish the content type using model:
    orm_env.create(Person).publish()
    # Or
    orm_env.create(Person)
    orm_env.publish(Person)
    
  • Unpublish and delete the content type using model:
    orm_env.unpublish(Product)
    orm_env.delete(Product)
    
  • Add and publish entry:
    person1 = orm_env.add(Person(email='a@a.com', name='a', title=['Manager', 'Seller'], age=13)).publish()
    # You can specify entry id or it will randomly generate a UUID
    person2 = orm_env.add(Person(email='b@a.com', name='b', title=['Seller'], age=66), id='S9SN3JWKN3565D').publish()
    person3 = orm_env.add(Person(email='c@a.com', name='c', title=['Manager'], created_date='2019-08-11T00:00-07:00')).publish()
    company1 = orm_env.add(Company(name='Contentful', employees=[person1.to_link().to_json(), person2.to_link().to_json()])).publish()
    # Or
    product1 = Product(
        name='ContentfulORM',
        description='Write your Contentful in ORM style!',
        seller=company1.to_link().to_json(),
        sponsored=True,
        price=0.01
    )
    orm_env.add(product1).publish()
    
  • Query:
    # Query all entries of a content type
    orm_env.query(Person).all()
    
    # Filter query
    # ORMContentTypeEntriesProxy extends ContentTypeEntriesProxy with additional filter function
    from contentful_orm.operators import all_, limit, select, skip
    
    # Filter with field names as keyword arguments for exact search
    orm_env.query(Person).filter(email='c@a.com', name='c')
    
    # Filter with operators, see operators all at: https://github.com/Phoenix-Chen/ContentfulORM/blob/dev/contentful_orm/operators.py
    orm_env.query(Person).filter(all_(title=['Manager', 'seller']), skip(1), limit(100), select('fields.name'))
    
    # Use combination of both
    orm_env.query(Person).filter(limit(1), name='c')
    
  • Serialize:
    from contentful_orm.serializers import ModelSerializer
    
    class ProductSerializer(ModelSerializer):
        class Meta:
            model = Product
            # Specify fields to be serialized or use '__all__' for all the fields
            fields = [
                'name',
                'description',
                'price'
            ]
    
    # Query the entry/entries you want to serialize
    products = orm_env.query(Product).all()
    
    # Serialize single entry
    serialized_product = ProductSerializer(products[0])
    
    # Serialize multiple entries
    serialized_products = ProductSerializer(products, many=True)
    
    # Currently links won't be recursively serialized, in case of circular references.
    

Release files for contentful-orm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contentful-orm 0.1.0
File Size Uploaded
contentful_orm-0.1.0.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for contentful-orm 0.1.0
File Interpreter ABI Platform
contentful_orm-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.0 kB

Release files / contentful_orm-0.1.0.tar.gz

Download URL contentful_orm-0.1.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
472d8a396d68d235f6bcab7828b96872a42121e119b44a5b08c66bef95caabdc
BLAKE2b-256 checksum
How to use checksums
d46afa88080f4288246f40f90c82f8b53aebdbeeafa295a4c3d228a0a7c8eba3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1

Release files / contentful_orm-0.1.0-py3-none-any.whl

Download URL contentful_orm-0.1.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21da6cddb80e5615c6a4e3acfe671e5aedd9445116299b094d537e168c5dc263
BLAKE2b-256 checksum
How to use checksums
7f9da0fc901656a67824d053a6b0e4f1e0cd4b6afee4934e47e7d17365013adb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page