Skip to main content

Object Model Mapper for Python-based web apps and/or APIs

Project description

Object Model Mapper

CircleCI Maintainability Test Coverage PyPI version

What This?

This script enables to flat/bump the model depth.

Why I Make This?

When I used MongoEngine, I needed to design the model like this:

import mongoengine as db

class RecentPrevAmount(db.EmbeddedDocument):
  recent = db.FloatField()
  prev = db.FloatField()

class AssetInfo(db.Document):
  assets = db.EmbeddedDocumentField(RecentPrevAmount)
  cash = db.EmbeddedDocumentField(RecentPrevAmount)
  receivable = db.EmbeddedDocumentField(RecentPrevAmount)
  revenue = db.EmbeddedDocumentField(RecentPrevAmount)
  cogs = db.EmbeddedDocumentField(RecentPrevAmount)

And creating RestAPI in above order without "Undefiend property" error, it is needed to create 5 CRUD resources:

  • [GET, POST, PUT, DELETE] /assets
  • [GET, POST, PUT, DELETE] /cash
  • [GET, POST, PUT, DELETE] /receivable
  • [GET, POST, PUT, DELETE] /revenue
  • [GET, POST, PUT, DELETE] /cogs

For each resource, the text like following is output/input:

{"recent": 10.0, "prev": 14.0}

This means, sending tons of requests is needed to file asset information and it may have frontend slow. To avoid the problem, the number of request to send should be reduced as far as possible. The ideal resource is one resource with the following format:

{
  "assets_recent": 10.0,
  "assets_prev": 11.0,
  "cash_recent": 12.0,
  "receivable_prev": 13.0,
  "revenue_recent": 14.0,
  "cogs_prev": 15.0
}

Of course, above is not good for public api (On public api, recent and prev should be embedded in a field), but above structure reduces the number of requests.

How to use

I think you must need example code than the doc.

Flat the model

#!/usr/bin/env python
# coding=utf-8

"""
Example code.

Note that this is just an example.
"""
import mongoengine as db
import omm
import wtforms.forms as forms
import wtforms.fields as fld
import wtforms.validators as vld
from ..api import api, render, login_only

# First, let's define the target models as usual.
class Address(db.EmbeddedDocument):
  street = db.ListField(db.StringField, required=True)
  city = db.StringField(required=True)
  state = db.StringField(required=True)
  country = db.StringField(required=True)

class User(db.Document):
  email = EmailField(primary_key=True)
  first_name = StringField(required=True)
  last_name = StringField(required=True)
  address = db.EmbeddedDocument(Address, required=True)

  @property
  def full_name(self):
    return (" ").join([self.first_name, self.last_name])

  @full_name.setter
  def full_name(self, value):
    try:
      (self.first_name, self.last_name) = value.split(" ")
    except ValueError:
      pass

# Then, define the map.
class UserMapper(omm.Mapper):
  # Note that set_cast can be non-list, i.e. str. However, in this case,
  # we use User because the root object type is User.
  fullname = omm.MapField("full_name", set_cast=[User, str])
  email = omm.MapField("email", set_cast=[User, str])
  # For third element should be list or any class that inherits list because
  # the target is typed as list.
  street1 = omm.MapField(
    "address.street[0]", set_cast=[User, Address, list, str]
  )
  street2 = omm.MapField(
    "address.street[1]", set_cast=[User, Address, list, str]
  )
  # Note that dot-notation is used to specify the member.
  city = omm.MapField("address.city", set_cast=[User, Address, str])
  state = omm.MapField("address.state", set_cast=[User, Address, str])
  country = omm.MapField("address.country", set_cast=[User, Address, str])

class UserForm(forms.Form):
  fullname = fld.StringField(validators=[vld.DataRequired()])
  email = fld.StringField(validators=[vld.Email()])
  street1 = fld.StringField(validators=[vld.DataRequired()])
  street2 = fld.StringField(validators=[vld.Optional()])
  city = fld.StringField(validators=[vld.DataRequired()])
  state = fld.StringField(validators=[vld.DataRequired()])
  country = fld.StringField(validators=[vld.DataRequired()])

class UserController(object):
  # User API

  def get(self, id):
    # GET request.
    user = User.objects()
    mapper = UserMapper(user)
    return render(mapper.to_json(), mimetype="application/json")

  def post(self):
    # POST request.
    form = UserForm(api.request.json())
    if not form.validate():
      return render(
        api.jsonify(form.errors),
        mimetype="application/json",
        code=417
      )
    mapper = UserMapper()
    form.populate_obj(mapper)
    mapper.connected_object.save()

  @login_only
  def put(self, uid):
    # PUT request.
    form = UserForm(api.request.json())
    if not form.validate():
      return render(
        api.jsonify(form.errors),
        mimetype="application/json",
        code=417
      )
    mapper = UserMapper(User.objects(uid))
    form.populate_obj(mapper)
    mapper.connected_object.save()

api.register(UserController)

Increment the model depth

Currently, I'm considering (and designing) the way to access multiple models thru the mapper. Unfortunately the functionalities are not implemented yet.

Other functionalities

Read the code

License (MIT License)

Copyright (c) 2016 Hiroaki Yamamoto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

OMM-1.0.1.tar.gz (20.9 kB view details)

Uploaded Source

File details

Details for the file OMM-1.0.1.tar.gz.

File metadata

  • Download URL: OMM-1.0.1.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for OMM-1.0.1.tar.gz
Algorithm Hash digest
SHA256 211ced97c1e3cb50d6cbd6bf51c250eba59b9c5d7f492c5c2c05338781da886c
MD5 f562e1e40c9b0a8a5bc9b05ca10cc23b
BLAKE2b-256 3f6a534932b28c02b557f902989b5efb62db22f6c1ea89f3ffb545f4d96b685b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page