Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Last active May 26, 2017 15:41
Show Gist options
  • Save amcgregor/86b9ddd6c3a35d09ba803b8d3a6f6a38 to your computer and use it in GitHub Desktop.
Save amcgregor/86b9ddd6c3a35d09ba803b8d3a6f6a38 to your computer and use it in GitHub Desktop.
Verified e-mail address model/widget and a snippet of a location model.
"""Illico Hodes verified e-mail address model."""
from marrow.mongo import Document, utcnow
from marrow.mongo.field import Link, Date
from web.asset import Depend
class VerifiedEmail(Document):
"""An embedded document representing a verified e-mail address.
Intended for use in an Array() embedding these sub-documents.
"""
# Marrow Mongo Metadata
__pk__ = 'address'
# Fields
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?
# Python Protocols
def __str__(self):
"""Return a plain text representation of this location."""
return self.address.path.name
# Self-Rendering Protocols
def __depends__(self, only):
"""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.
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 __html__(self):
"""Return a microdata-compliant e-mail link."""
return '<a href="{link}" class="email {verified}">{address}</a>'.format(
link = 'mailto:' + self.address,
verified = 'verified' if self.verified else 'unverified',
address = self.address,
)
def __link__(self):
"""Return a valid URI representitive of this widget."""
return self[~VerifiedEmail.address]
class Location(Document):
# ...
def __str__(self):
"""Return a plain text representation of this location."""
parts = [getattr(self, name, None) for name in self.__fields__ if name != 'kind']
return "\n".join(part for part in parts if part)
# Content Portability
__text__ = __str__ # Full-text extraction is this easy.
# Self-Rendering Protocols
# __depends__ ...
def __html__(self):
"""Return an h-adr microdata-compliant address widget."""
return self.__html_format__('address')
def __html_format__(self, spec=None):
"""Return various microdata-compliant HTML representations of this location."""
# TODO: Split out
spec = spec or 'address'
if spec == 'address': # http://microformats.org/wiki/h-adr
return ''
elif spec == 'geo': # http://microformats.org/wiki/h-geo see TODO on __link__ below.
return ''
elif spec == 'map': # Embed a Google map. For real-real, not play-play.
return ''
elif spec == 'link': # TODO: Microdata.
parts = (self.address, ) if self.address else (self.city, self.region)
return '''<a href="{}">{}</a>'''.format(self.__link__(), ''.join(part for part in parts if part))
raise ValueError("Invalid format specification for {}: {}".format(self.__class__.__name__, spec))
def __link__(self): # TODO: Extract the geo bit to marrow.mongo.document.Point.
"""Return a valid URI representitive of this widget."""
if self.geo:
return "geo:{},{}".format(self.latitude, self.longitude)
# TODO: Return a Google Maps link. ;)
raise NotImplementedError()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment