Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Last active October 7, 2017 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcgregor/9db56ae4562b0c882001ceac1c43c89f to your computer and use it in GitHub Desktop.
Save amcgregor/9db56ae4562b0c882001ceac1c43c89f to your computer and use it in GitHub Desktop.
# In your app, make sure to tell the serializer that your model objects are serializable...
# Helps if your model classes share a common base class you can register.
from collections import Mapping
app = Application(..., extensions=[
...
SerializationExtension(types=(list, Mapping, YourModel)),
...
])
# Your data model. Of some kind.
# Updated to use templates and stuff.
from marrow.package.loader import load # one way...
from .template.artist import render_html # another...
class Artist(Document):
name = String()
bio = String()
...
def __json__(self):
return "{json...}"
#def __html__(self):
# return render_html()
# ^ lame
__html__ = render_html # baws
__xml__ = load('web.app.MYAPP.template.artist:render_xml') # clean
def _render(kind):
attr = '__{}__'.format(kind)
def render(thing):
return getattr(thing, attr)()
html = _render('html')
xml = _render('xml')
# Add this stuff to your own, and make sure to run "pip install -e ." or "setup.py develop" again.
...
setup(
...,
entry_points = {
'web.serialize': [
'html = web.app.APPNAME.render:html',
'text/html = web.app.APPNAME.render:html',
'xml = web.app.APPNAME.render:html',
'application/xml = web.app.APPNAME.render:html',
]
},
...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment