def __getitem__(self, key, validator_cls=None, model_cls=None): """Lookup the context by matching the ``key`` against the story's hashtag value. """ logger.debug('{0}: {1}'.format(self.__class__.__name__, key)) # Test jig. if validator_cls is None: validator_cls = ValidHashtag if model_cls is None: model_cls = self.model_cls # Lookup the context by hashtag value. try: value = validator_cls.to_python(key) except Invalid: pass else: hashtag = Hashtag.get_or_create(value) context = model_cls.query.filter_by(hashtag=hashtag).first() # XXX if the story doesn't exist, create it. if not context: context = model_cls(hashtag=hashtag) event = StoryAdded(self.request, context) self.request.registry.notify(event) save(context) return context raise KeyError
def send_email_from_notification_dispatch(request, notification_dispatch_id): """Boilerplate to extract information from the notification dispatch and send an email. Please note that no verification if it should be sent is made prior to sending. """ lookup = repo.LookupNotificationDispatch() dotted_name_resolver = path.DottedNameResolver() notification_dispatch = lookup(notification_dispatch_id) if not notification_dispatch: return False # Get our spec. spec = notification_dispatch.single_spec # Get our Address to send to. send_to = notification_dispatch.address # Get our view to render the spec. view = dotted_name_resolver.resolve(notification_dispatch.view) # Get the context. context = notification_dispatch.notification.event.parent # Send the email. view(request, context, spec, send_to) # Set the sent info in our db. notification_dispatch.sent = datetime.datetime.now() bm.save(notification_dispatch) return True
def revoke(self, should_save=True, save=None): """Revoke this access token.""" # Compose. if save is None: save = Session.add # Flag as revoked. self.has_been_revoked = True # Save to db. if should_save: save(self)
def touch(self): """Touch the modified date of this story.""" self.modified = datetime.datetime.utcnow() save(self)
def send_from_notification_dispatch(request, notification_dispatch_id): """Boilerplate to extract information from the notification dispatch and send an email. Please note that no verification if it should be sent is made prior to sending. """ lookup = repo.LookupNotificationDispatch() dotted_name_resolver = path.DottedNameResolver() notification_dispatch = lookup(notification_dispatch_id) if not notification_dispatch: return False # Extract information from the notification dispatch. spec = notification_dispatch.single_spec send_to = notification_dispatch.address view = dotted_name_resolver.resolve(notification_dispatch.view) event = notification_dispatch.notification.event bcc = notification_dispatch.bcc context = event.parent channel = notification_dispatch.category # Get the template vars. tmpl_vars = view(request, context, send_to, event, event.action) # Set some defaults for the template vars. tmpl_vars.setdefault('data', context) tmpl_vars.setdefault('to', send_to) tmpl_vars.setdefault('from', util.extract_from(request)) tmpl_vars.setdefault('state_or_action', event.action) tmpl_vars.setdefault('event', event) tmpl_vars.setdefault('subject', '{0} {1}'.format(event.target, event.action)) # Check if we should add bcc. if bcc: tmpl_vars.setdefault('bcc', bcc) # Extract form tmpl_vars and remove. subject = tmpl_vars['subject'] to_ = tmpl_vars['to'] from_ = tmpl_vars['from'] del tmpl_vars['subject'] del tmpl_vars['to'] del tmpl_vars['from'] # Send emails / sms. if channel == 'email': email = request.render_email(from_, to_, subject, spec, tmpl_vars, **tmpl_vars) request.send_email(email) elif channel == 'sms': pass else: raise Exception('Unknown channel to send the notification') # Set the sent info in our db. notification_dispatch.sent = datetime.datetime.now() bm.save(notification_dispatch) return True