def test_generate_notifications_empty_if_action_not_create(): """If the action is not 'create', no notifications should be generated.""" annotation = storage.annotation_from_dict({}) request = DummyRequest() notifications = rt.generate_notifications(request, annotation, 'update') assert list(notifications) == []
def send_notifications(message): data = json.loads(message.body) action = data['action'] annotation = storage.annotation_from_dict(data['annotation']) mailer = get_mailer(request) notifications = generate_notifications(request, annotation, action) for (subject, body, html, recipients) in notifications: m = Message(subject=subject, recipients=recipients, body=body, html=html) mailer.send_immediately(m)
def _read_group(request, group): """Return the rendered "Share this group" page. This is the page that's shown when a user who is already a member of a group visits the group's URL. """ url = request.route_url('group_read', pubid=group.pubid, slug=group.slug) result = search.search(request, private=False, params={ "group": group.pubid, "limit": 1000 }) annotations = [ presenters.AnnotationHTMLPresenter(storage.annotation_from_dict(a)) for a in result['rows'] ] # Group the annotations by URI. # Create a dict mapping the (normalized) URIs of the annotated documents # to the most recent annotation of each document. annotations_by_uri = collections.OrderedDict() for annotation in annotations: normalized_uri = uri.normalize(annotation.uri) if normalized_uri not in annotations_by_uri: annotations_by_uri[normalized_uri] = annotation if len(annotations_by_uri) >= 25: break document_links = [ annotation.document_link for annotation in annotations_by_uri.values() ] template_data = { 'group': group, 'group_url': url, 'document_links': document_links } return renderers.render_to_response( renderer_name='h:templates/groups/share.html.jinja2', value=template_data, request=request)
def handle_annotation_event(message, socket): """ Get message about annotation event `message` to be sent to `socket`. Inspects the embedded annotation event and decides whether or not the passed socket should receive notification of the event. Returns None if the socket should not receive any message about this annotation event, otherwise a dict containing information about the event. """ action = message['action'] annotation = storage.annotation_from_dict(message['annotation']) if action == 'read': return None if message['src_client_id'] == socket.client_id: return None if annotation.get('nipsa') and (socket.request.authenticated_userid != annotation.get('user', '')): return None if not _authorized_to_read(socket.request, annotation): return None # We don't send anything until we have received a filter from the client if socket.filter is None: return None if not socket.filter.match(annotation, action): return None return { 'payload': [annotation], 'type': 'annotation-notification', 'options': { 'action': action }, }
def _read_group(request, group): """Return the rendered "Share this group" page. This is the page that's shown when a user who is already a member of a group visits the group's URL. """ url = request.route_url('group_read', pubid=group.pubid, slug=group.slug) result = search.search(request, private=False, params={"group": group.pubid, "limit": 1000}) annotations = [presenters.AnnotationHTMLPresenter( storage.annotation_from_dict(a)) for a in result['rows']] # Group the annotations by URI. # Create a dict mapping the (normalized) URIs of the annotated documents # to the most recent annotation of each document. annotations_by_uri = collections.OrderedDict() for annotation in annotations: normalized_uri = uri.normalize(annotation.uri) if normalized_uri not in annotations_by_uri: annotations_by_uri[normalized_uri] = annotation if len(annotations_by_uri) >= 25: break document_links = [annotation.document_link for annotation in annotations_by_uri.values()] template_data = { 'group': group, 'group_url': url, 'document_links': document_links } return renderers.render_to_response( renderer_name='h:templates/groups/share.html.jinja2', value=template_data, request=request)
def handle_annotation_event(message, socket): """ Get message about annotation event `message` to be sent to `socket`. Inspects the embedded annotation event and decides whether or not the passed socket should receive notification of the event. Returns None if the socket should not receive any message about this annotation event, otherwise a dict containing information about the event. """ action = message['action'] annotation = storage.annotation_from_dict(message['annotation']) if action == 'read': return None if message['src_client_id'] == socket.client_id: return None if annotation.get('nipsa') and ( socket.request.authenticated_userid != annotation.get('user', '')): return None if not _authorized_to_read(socket.request, annotation): return None # We don't send anything until we have received a filter from the client if socket.filter is None: return None if not socket.filter.match(annotation, action): return None return { 'payload': [annotation], 'type': 'annotation-notification', 'options': {'action': action}, }
def _annotations(request): """Return the annotations from the search API.""" rows = search.search(request, request.params)['rows'] return [storage.annotation_from_dict(a) for a in rows]
class Meta: model = storage.annotation_from_dict({}).__class__ exclude = ["username", "random_number", "num_tags", "exact_text", "document_title"]
def _present_searchdict(request, mapping): """Run an object returned from search through a presenter.""" ann = storage.annotation_from_dict(mapping) return AnnotationJSONPresenter(request, ann).asdict()
def _fake_anno(id): try: offset = int(id) except TypeError: return None return storage.annotation_from_dict(store_fake_data[offset])