def generate_role_digest(role): """Generate notification digest emails for the given user.""" # TODO: get and use the role's locale preference. since = datetime.utcnow() - timedelta(hours=26) result = get_notifications(role, since=since) hits = result.get("hits", {}) total_count = hits.get("total", {}).get("value") log.info("Daily digest: %r (%s notifications)", role, total_count) if total_count == 0: return notifications = [render_notification(role, n) for n in hits.get("hits")] notifications = [n for n in notifications if n is not None] params = dict( notifications=notifications, role=role, total_count=total_count, manage_url=ui_url("notifications"), ui_url=settings.APP_UI_URL, app_title=settings.APP_TITLE, ) plain = render_template("email/notifications.txt", **params) html = render_template("email/notifications.html", **params) log.info("Notification: %s", plain) subject = "%s notifications" % total_count email_role(role, subject, html=html, plain=plain)
def oauth_callback(): require(settings.OAUTH) try: token = oauth.provider.authorize_access_token() except AuthlibBaseError as err: log.warning("Failed OAuth: %r", err) raise Unauthorized(gettext("Authentication has failed.")) if token is None or isinstance(token, AuthlibBaseError): log.warning("Failed OAuth: %r", token) raise Unauthorized(gettext("Authentication has failed.")) role = handle_oauth(oauth.provider, token) if role is None: log.error("No OAuth handler was installed.") raise Unauthorized(gettext("Authentication has failed.")) if role.is_blocked: raise Unauthorized(gettext("Your account is blocked.")) db.session.commit() update_role(role) log.info("Logged in: %r", role) request.authz = Authz.from_role(role) token = request.authz.to_token(role=role) token = token.decode('utf-8') next_path = get_url_path(request.args.get('state')) next_url = ui_url(settings.OAUTH_UI_CALLBACK, next=next_path) next_url = '%s#token=%s' % (next_url, token) return redirect(next_url)
def oauth_callback(): require(settings.OAUTH) err = Unauthorized(gettext("Authentication has failed.")) state = cache.get_complex(_oauth_session(request.args.get("state"))) if state is None: raise err try: oauth.provider.framework.set_session_data(request, "state", state.get("state")) uri = state.get("redirect_uri") token = oauth.provider.authorize_access_token(redirect_uri=uri) except AuthlibBaseError as err: log.warning("Failed OAuth: %r", err) raise err if token is None or isinstance(token, AuthlibBaseError): log.warning("Failed OAuth: %r", token) raise err role = handle_oauth(oauth.provider, token) if role is None: raise err db.session.commit() update_role(role) log.debug("Logged in: %r", role) request.authz = Authz.from_role(role) next_path = get_url_path(state.get("next_url")) next_url = ui_url("oauth", next=next_path) next_url = "%s#token=%s" % (next_url, request.authz.to_token()) session.clear() return redirect(next_url)
def oauth_callback(): require(settings.OAUTH) resp = oauth.provider.authorized_response() if resp is None or isinstance(resp, OAuthException): log.warning("Failed OAuth: %r", resp) raise Unauthorized(gettext("Authentication has failed.")) response = signals.handle_oauth_session.send(provider=oauth.provider, oauth=resp) for (_, role) in response: if role is None: continue db.session.commit() update_role(role) log.info("Logged in: %r", role) request.authz = Authz.from_role(role) token = request.authz.to_token(role=role) token = token.decode('utf-8') next_path = get_url_path(request.args.get('state')) next_url = ui_url(settings.OAUTH_UI_CALLBACK, next=next_path) next_url = '%s#token=%s' % (next_url, token) return redirect(next_url) log.error("No OAuth handler for %r was installed.", oauth.provider.name) raise Unauthorized(gettext("Authentication has failed."))
def export_collection(collection): uri = URIRef(ui_url('collections', collection.id)) g = Graph() g.add((uri, RDF.type, DCMI.Collection)) g.add((uri, RDFS.label, Literal(collection.label))) g.add((uri, DCMI.identifier, Literal(collection.foreign_id))) g.add((uri, ALEPH.category, ALEPH[collection.category])) yield from itergraph(g) for entity in iter_proxies(collection_id=collection.id): yield from itergraph(export_entity(entity, uri))
def export_collection(collection): uri = URIRef(ui_url('collections', collection.id)) g = Graph() g.add((uri, RDF.type, DCMI.Collection)) g.add((uri, RDFS.label, Literal(collection.label))) g.add((uri, DCMI.identifier, Literal(collection.foreign_id))) g.add((uri, ALEPH.category, ALEPH[collection.category])) yield from itergraph(g) for entity in iter_proxies(collection_id=collection.id): yield from itergraph(export_entity(entity, uri))
def export_collection(collection): uri = URIRef(ui_url('collections', collection.id)) g = Graph() g.add((uri, RDF.type, DCMI.Collection)) g.add((uri, RDFS.label, Literal(collection.label))) g.add((uri, DCMI.identifier, Literal(collection.foreign_id))) g.add((uri, ALEPH.category, ALEPH[collection.category])) for line in itergraph(g): yield line entities = iter_entities(collection_id=collection.id, excludes=['text']) for entity in entities: g = export_entity(entity, uri) for line in itergraph(g): yield line
def generate_role_digest(role): """Generate notification digest emails for the given user.""" # TODO: get and use the role's locale preference. since = datetime.utcnow() - timedelta(hours=25) q = Notification.by_role(role, since=since) total_count = q.count() if total_count == 0: return notifications = [] for notification in q.limit(25): notifications.append(render_notification(notification)) subject = '%s notifications' % total_count notify_role(role, subject, 'email/notifications.html', total_count=total_count, notifications=notifications, manage_url=ui_url('notifications'))
def oauth_callback(): require(settings.OAUTH) err = Unauthorized(gettext("Authentication has failed.")) state = cache.get_complex(_oauth_session(request.args.get("state"))) if state is None: raise err try: oauth.provider.framework.set_session_data(request, "state", state.get("state")) uri = state.get("redirect_uri") oauth_token = oauth.provider.authorize_access_token(redirect_uri=uri) except AuthlibBaseError as err: log.warning("Failed OAuth: %r", err) raise err if oauth_token is None or isinstance(oauth_token, AuthlibBaseError): log.warning("Failed OAuth: %r", oauth_token) raise err role = handle_oauth(oauth.provider, oauth_token) if role is None: raise err # Determine session duration based on OAuth settings expire = oauth_token.get("expires_in", Authz.EXPIRE) expire = oauth_token.get("refresh_expires_in", expire) db.session.commit() update_role(role) log.debug("Logged in: %r", role) request.authz = Authz.from_role(role, expire=expire) token = request.authz.to_token() # Store id_token to generate logout URL later id_token = oauth_token.get("id_token") if id_token is not None: cache.set(_token_session(token), id_token, expires=expire) next_path = get_url_path(state.get("next_url")) next_url = ui_url("oauth", next=next_path) next_url = "%s#token=%s" % (next_url, token) session.clear() return redirect(next_url)
def send_export_notification(export): download_url = archive_url( export.content_hash, file_name=export.file_name, mime_type=export.mime_type, expire=export.expires_at, ) params = dict( role=export.creator, export_label=export.label, download_url=download_url, expiration_date=export.expires_at.strftime("%Y-%m-%d"), exports_url=ui_url("exports"), ui_url=settings.APP_UI_URL, app_title=settings.APP_TITLE, ) plain = render_template("email/export.txt", **params) html = render_template("email/export.html", **params) log.info("Notification: %s", plain) subject = "Export ready for download" email_role(export.creator, subject, html=html, plain=plain)
def generate_role_digest(role): """Generate notification digest emails for the given user.""" # TODO: get and use the role's locale preference. since = datetime.utcnow() - timedelta(hours=25) q = Notification.by_role(role, since=since) total_count = q.count() if total_count == 0: return notifications = [render_notification(role, n) for n in q.limit(30)] notifications = [n for n in notifications if n is not None] params = dict(notifications=notifications, role=role, total_count=total_count, manage_url=ui_url('notifications'), ui_url=settings.APP_UI_URL, app_title=settings.APP_TITLE) plain = render_template('email/notifications.txt', **params) html = render_template('email/notifications.html', **params) log.info("Notification: %s", plain) subject = '%s notifications' % total_count email_role(role, subject, html=html, plain=plain)
def generate_role_digest(role): """Generate notification digest emails for the given user.""" # TODO: get and use the role's locale preference. since = datetime.utcnow() - timedelta(hours=25) q = Notification.by_channels(get_role_channels(role), since=since, exclude_actor_id=role.id) total_count = q.count() if total_count == 0: return notifications = [render_notification(role, n) for n in q.limit(30)] notifications = [n for n in notifications if n is not None] params = dict(notifications=notifications, role=role, total_count=total_count, manage_url=ui_url('notifications'), ui_url=settings.APP_UI_URL, app_title=settings.APP_TITLE) plain = render_template('email/notifications.txt', **params) html = render_template('email/notifications.html', **params) log.info("Notification: %s", plain) subject = '%s notifications' % total_count email_role(role, subject, html=html, plain=plain)
def send_export_notification(export): role = Role.by_id(export.creator_id) authz = Authz.from_role(role) download_url = url_for( "exports_api.download", export_id=export.id, _authz=authz, _expire=export.expires_at, ) params = dict( role=role, export_label=export.label, download_url=download_url, expiration_date=export.expires_at.strftime("%Y-%m-%d"), exports_url=ui_url("exports"), ui_url=settings.APP_UI_URL, app_title=settings.APP_TITLE, ) plain = render_template("email/export.txt", **params) html = render_template("email/export.html", **params) log.info("Notification: %s", plain) subject = "Export ready for download" email_role(role, subject, html=html, plain=plain)
def collection_uri(value): return URIRef(ui_url('collections', value))
def document_uri(value): return URIRef(ui_url('documents', value))
def entity_uri(value): return URIRef(ui_url('entities', value))
def collection_url(collection_id=None, **query): return ui_url('collections', id=collection_id, **query)
def document_url(document_id=None, **query): return ui_url('documents', id=document_id, **query)
def entity_url(entity_id=None, **query): return ui_url('entities', id=entity_id, **query)