Last active
February 16, 2021 01:45
-
-
Save amcgregor/64e9b0113b1e1d224e98cd6a6fb56818 to your computer and use it in GitHub Desktop.
An example "trivial" Contentment component. Note in particular that this is all "data model" — HTML is merely one possible serialization of the data. View is not defined here, there are no presentation details, but a method is provided to permit introspection of the view capabilities or requirements.
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 Document, utcnow | |
from marrow.mongo.field import Date, Link | |
from ..asset import Depend | |
class Address(Document): | |
"""An embedded document representing a verified e-mail address. | |
Intended for use in an Array() embedding these sub-documents. | |
""" | |
__pk__ = 'address' | |
__vary__ = ('address', 'verified') # The attributes to fetch for cache key generation. | |
address = Link(required=True, protocols={'mailto'}) # The e-mail address itself. | |
added = Date(default=utcnow, write=False) # When was this address added? | |
verified = Date(default=None, write=False) # Has the user verified the address, and if so, when? | |
@Depend.declare # self may actually be the class, not an instance! | |
def __depends__(self, only=Depend.ALL): | |
"""Declare bundle, themeing, and interaction points for this widget.""" | |
if Depend.STYLE in only: | |
# Hard theme selectors; this is always present. | |
yield Depend.STYLE, '.email' | |
# Conditional theme selectors; announce all anyway, but hint. | |
if issubclass(self, Address): # Class-level identification. | |
yield Depend.STYLE, '.verified' | |
yield Depend.STYLE, '.unverified' | |
else: # Conditional actual use when querying an instance specifically. | |
yield Depend.STYLE, '.verified', bool(self.verified) | |
yield Depend.STYLE, '.unverified', not self.verified | |
if Depend.EVENT in only: | |
# Client- and server-side events that may fire. | |
yield Depend.EVENT, 'com.illico.common.email.verified' | |
def __str__(self): | |
"""Return a plain text representation of this location; just the name, if present.""" | |
return self.address.path.name | |
def __html__(self): | |
"""Return a microdata-compliant e-mail link. | |
If the e-mail address is fully-formed with a name component, the name will be used for presentation. | |
""" | |
return f'<a href="mailto:{self.address}" class="email {'' if self.verified else 'un'}verified">{self}</a>' | |
def __link__(self): | |
"""Return a valid URI representitive of this widget.""" | |
return self.address |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment