Last active
October 7, 2017 18:45
-
-
Save amcgregor/9db56ae4562b0c882001ceac1c43c89f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)), | |
... | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _render(kind): | |
attr = '__{}__'.format(kind) | |
def render(thing): | |
return getattr(thing, attr)() | |
html = _render('html') | |
xml = _render('xml') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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