Esempio n. 1
0
def domain_notification(event):
    if event.action == 'create':
        try:
            annotation = event.annotation

            # Check for authorization. Send notification only for public
            # annotation
            # XXX: This can be changed and fine grained when user
            # groups will be introduced
            read = annotation['permissions']['read']
            if "group:__world__" not in read:
                return

            uri = annotation['uri']
            # TODO: Fetching the page should be done via a webproxy
            r = requests.get(uri)
            emails = get_document_owners(r.text)

            # Now send the notifications
            url_struct = urlparse(annotation['uri'])
            domain = url_struct.hostname or url_struct.path
            domain = re.sub(r'^www.', '', domain)
            notifier = AnnotationNotifier(event.request)
            for email in emails:
                # Domain matching
                mail_domain = email.split('@')[-1]
                if mail_domain == domain:
                    try:
                        # Send notification to owners
                        notifier.send_notification_to_owner(
                            annotation,
                            {'email': email},
                            'document_owner'
                        )
                    except:
                        log.exception('Problem sending email')
        except:
            log.exception('Problem with domain notification')
Esempio n. 2
0
            'text': annotation['text'],
            'tags': tags,
            'user_profile': user_profile_url(request, annotation['user']),
            'user': user,
            'path': standalone_url(request, annotation['id']),
            'timestamp': annotation['created'],
            'selection': annotation['quote']
        }

    @staticmethod
    def get_recipients(request, annotation, data):
        return [data['email']]


AnnotationNotifier.register_template(
    'document_owner',
    DocumentOwnerTemplate.generate_notification
)


# TODO: Introduce proper cache for content parsing
def get_document_owners(content):
    parsed_data = BeautifulSoup(content)
    documents = parsed_data.select('a[rel="reply-to"]')
    hrefs = []
    for d in documents:
        if re.match(r'^mailto:', d['href'], re.IGNORECASE):
            hrefs.append(d['href'][7:])

    return hrefs