Last active
April 7, 2021 18:02
-
-
Save amcgregor/28228f3699cf153f1c2ce74da30ff172 to your computer and use it in GitHub Desktop.
An example self-rendering model component.
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
from marrow.mongo import utcnow | |
from marrow.mongo.trait import Identified | |
from marrow.mongo.field import Date, Markdown, Reference | |
from ..asset import Depend | |
__all__ = ('Note', ) | |
class Note(Identified): | |
"""An embedded document representing a textual reply, comment, or note.""" | |
# Fields | |
id = Identified.id.adapt(positional=False, write=False) # Exclude from constructor and prevent writes. | |
author = Reference('Account', cache={'name'}, required=True) # Keep a copy of the user's name to save a lookup. | |
message = Markdown(required=True, repr=False) | |
created = Date(default=utcnow, assign=True) # Avoid ID generation time to permit aggregation and querying. | |
modified = Date(default=None) | |
# Self-Rendering Protocols | |
@Depend.declare | |
def __depends__(Note, only): | |
"""Declare bundle, themeing, and interaction points for this widget.""" | |
if Depend.STYLE in only: # CSS selectors that a theme may wish to customize. | |
yield Depend.STYLE, 'figure.note' | |
if Depend.EVENT in only: # Client- and server-side events that may fire. | |
yield Depend.EVENT, 'com.cegid.Note:added' | |
yield Depend.EVENT, 'com.cegid.Note:updated' | |
yield Depend.EVENT, 'com.cegid.Note:removed' | |
def __html__(self): | |
"""Return a microdata-compliant HTML representation for this object.""" | |
return f'''<figure class=note> | |
<blockquote>{self.message.__html__()}</blockquote> | |
<figcaption>by {self.author.name} on <time datetime={self.created.isoformat('T', 'seconds')}>{self.created.strftime('%Y/%m/%d').replace('/0', '/')} at {self.created.strftime('%H:%M')}</time></figcaption> | |
</figure>''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment