Exemplo n.º 1
0
 def _serialize(self, obj):
     pk = obj.get('id')
     obj['links'] = {
         'self':
         url_for('collections_api.view', id=pk),
         'xref':
         url_for('xref_api.index', id=pk),
         'xref_csv':
         url_for('xref_api.csv_export', id=pk,
                 _authorize=obj.get('secret')),
         'reconcile':
         url_for('reconcile_api.reconcile',
                 collection_id=pk,
                 _authorize=obj.get('secret')),
         'ui':
         collection_url(pk)
     }
     obj['writeable'] = request.authz.can(pk, request.authz.WRITE)
     creator_id = obj.pop('creator_id', None)
     obj['creator'] = self.resolve(Role, creator_id, RoleSerializer)
     obj['team'] = []
     for role_id in ensure_list(obj.get('team_id')):
         role = self.resolve(Role, role_id, RoleSerializer)
         if role is not None:
             obj['team'].append(role)
     obj.pop('_index', None)
     return self._clean_response(obj)
Exemplo n.º 2
0
def render_notification(stub, notification):
    """Generate a text version of the notification, suitable for use
    in an email or text message."""
    from aleph.logic import resolver
    for name, clazz, value in notification.iterparams():
        resolver.queue(stub, clazz, value)
    resolver.resolve(stub)

    plain = str(notification.event.template)
    html = str(notification.event.template)
    for name, clazz, value in notification.iterparams():
        data = resolver.get(stub, clazz, value)
        if data is None:
            return
        link, title = None, None
        if clazz == Role:
            title = data.get('label')
        elif clazz == Alert:
            title = data.get('query')
        elif clazz == Collection:
            title = data.get('label')
            link = collection_url(value)
        elif clazz == Entity:
            title = data.get('name')
            link = entity_url(value)

        template = '{{%s}}' % name
        html = html.replace(template, html_link(title, link))
        plain = plain.replace(template, "'%s'" % title)
        if name == notification.event.link_to:
            plain = '%s (%s)' % (plain, link)
    return {'plain': plain, 'html': html}
Exemplo n.º 3
0
def render_notification(stub, notification):
    """Generate a text version of the notification, suitable for use
    in an email or text message."""
    from aleph.logic import resolver
    for name, clazz, value in notification.iterparams():
        resolver.queue(stub, clazz, value)
    resolver.resolve(stub)

    plain = str(notification.event.template)
    html = str(notification.event.template)
    for name, clazz, value in notification.iterparams():
        data = resolver.get(stub, clazz, value)
        if data is None:
            return
        link, title = None, None
        if clazz == Role:
            title = data.get('label')
        elif clazz == Alert:
            title = data.get('query')
        elif clazz == Collection:
            title = data.get('label')
            link = collection_url(value)
        elif clazz == Entity:
            title = data.get('name')
            link = entity_url(value)

        template = '{{%s}}' % name
        html = html.replace(template, html_link(title, link))
        plain = plain.replace(template, "'%s'" % title)
        if name == notification.event.link_to:
            plain = '%s (%s)' % (plain, link)
    return {'plain': plain, 'html': html}
Exemplo n.º 4
0
 def _serialize(self, obj):
     pk = obj.get("id")
     authz = request.authz if obj.get("secret") else None
     obj["links"] = {
         "self":
         url_for("collections_api.view", collection_id=pk),
         "xref":
         url_for("xref_api.index", collection_id=pk),
         "xref_export":
         url_for("xref_api.export", collection_id=pk, _authz=authz),
         "reconcile":
         url_for(
             "reconcile_api.reconcile",
             collection_id=pk,
             _authz=authz,
         ),
         "ui":
         collection_url(pk),
     }
     obj["shallow"] = obj.get("shallow", True)
     obj["writeable"] = request.authz.can(pk, request.authz.WRITE)
     creator_id = obj.pop("creator_id", None)
     obj["creator"] = self.resolve(Role, creator_id, RoleSerializer)
     obj["team"] = []
     for role_id in ensure_list(obj.pop("team_id", [])):
         if request.authz.can_read_role(role_id):
             role = self.resolve(Role, role_id, RoleSerializer)
             obj["team"].append(role)
     return obj
Exemplo n.º 5
0
def sitemap():
    """
    ---
    get:
      summary: Get a sitemap
      description: >-
        Returns a site map for search engine robots. This lists each
        published collection on the current instance.
      responses:
        '200':
          description: OK
          content:
            text/xml:
              schema:
                type: object
      tags:
      - System
    """
    enable_cache(vary_user=False)
    request.rate_limit = None
    collections = []
    for collection in Collection.all_authz(Authz.from_role(None)):
        updated_at = collection.updated_at.date().isoformat()
        updated_at = max(settings.SITEMAP_FLOOR, updated_at)
        collections.append({
            'url': collection_url(collection.id),
            'updated_at': updated_at
        })
    return render_xml('sitemap.xml', collections=collections)
Exemplo n.º 6
0
 def hypermedia(self, data):
     pk = str(data.get('id'))
     data['links'] = {
         'self': url_for('collections_api.view', id=pk),
         'ui': collection_url(pk)
     }
     data['writeable'] = request.authz.can_write(pk)
     return data
Exemplo n.º 7
0
def sitemap(id):
    collection = get_db_collection(id, request.authz.READ)
    url = collection_url(collection_id=collection.id)
    updated_at = collection.updated_at.date().isoformat()
    return render_xml('sitemap.xml',
                      url=url,
                      updated_at=updated_at,
                      entries=generate_sitemap(id))
Exemplo n.º 8
0
Arquivo: export.py Projeto: pudo/aleph
def export_entity_excel(workbook, collection, entity):
    fields = {
        'url': entity_url(entity.id),
        'collection': collection.get('label'),
        'collection_url': collection_url(collection.get('id'))
    }
    write_entity_excel(workbook, entity,
                       extra_fields=fields,
                       extra_headers=EXTRA_HEADERS)
Exemplo n.º 9
0
def export_entity_excel(workbook, collection, entity):
    fields = {
        'url': entity_url(entity.id),
        'collection': collection.get('label'),
        'collection_url': collection_url(collection.get('id'))
    }
    write_entity_excel(workbook,
                       entity,
                       extra_fields=fields,
                       extra_headers=EXTRA_HEADERS)
Exemplo n.º 10
0
 def hypermedia(self, data):
     pk = str(data.get('id'))
     data['links'] = {
         'self': url_for('collections_api.view', id=pk),
         'xref': url_for('xref_api.index', id=pk),
         'xref_csv': url_for('xref_api.csv_export', id=pk, _authorize=True),
         'ui': collection_url(pk)
     }
     data['writeable'] = request.authz.can(pk, request.authz.WRITE)
     return data
Exemplo n.º 11
0
Arquivo: export.py Projeto: pudo/aleph
def export_entity_csv(handlers, collection, entity):
    fh = handlers.get(entity.schema.plural)
    if fh is None:
        handlers[entity.schema.plural] = fh = io.StringIO()
        write_headers(fh, entity.schema,
                      extra_headers=EXTRA_HEADERS)
    write_entity_csv(fh, entity, extra_fields={
        'url': entity_url(entity.id),
        'collection': collection.get('label'),
        'collection_url': collection_url(collection.get('id'))
    })
Exemplo n.º 12
0
def sitemap():
    enable_cache(vary_user=False)
    collections = []
    for collection in Collection.all_authz(Authz.from_role(None)):
        updated_at = collection.updated_at.date().isoformat()
        updated_at = max(settings.SITEMAP_FLOOR, updated_at)
        collections.append({
            'url': collection_url(collection.id),
            'updated_at': updated_at
        })
    return render_xml('sitemap.xml', collections=collections)
Exemplo n.º 13
0
def sitemap():
    enable_cache(vary_user=False)
    collections = []
    for collection in Collection.all_authz(Authz.from_role(None)):
        updated_at = collection.updated_at.date().isoformat()
        updated_at = max(settings.SITEMAP_FLOOR, updated_at)
        collections.append({
            'url': collection_url(collection.id),
            'updated_at': updated_at
        })
    return render_xml('sitemap.xml', collections=collections)
Exemplo n.º 14
0
def export_entity_csv(handlers, collection, entity):
    fh = handlers.get(entity.schema.plural)
    if fh is None:
        handlers[entity.schema.plural] = fh = io.StringIO()
        write_headers(fh, entity.schema, extra_headers=EXTRA_HEADERS)
    write_entity_csv(fh,
                     entity,
                     extra_fields={
                         'url': entity_url(entity.id),
                         'collection': collection.get('label'),
                         'collection_url': collection_url(collection.get('id'))
                     })
Exemplo n.º 15
0
def render_notification(stub, notification):
    """Generate a text version of the notification, suitable for use
    in an email or text message."""
    from aleph.logic import resolver

    notification = unpack_result(notification)
    event = Events.get(notification.get("event"))
    if event is None:
        return

    for name, clazz, value in _iter_params(notification, event):
        resolver.queue(stub, clazz, value)
    resolver.resolve(stub)
    plain = str(event.template)
    html = str(event.template)
    for name, clazz, value in _iter_params(notification, event):
        data = resolver.get(stub, clazz, value)
        if data is None:
            return
        link, title = None, None
        if clazz == Role:
            title = data.get("label")
        elif clazz == Alert:
            title = data.get("query")
        elif clazz == Collection:
            title = data.get("label")
            link = collection_url(value)
        elif clazz == Entity:
            proxy = model.get_proxy(data)
            title = proxy.caption
            link = entity_url(value)
        elif clazz == EntitySet:
            title = data.label
            link = entityset_url(data.id)
        elif clazz == Export:
            title = data.get("label")
            link = archive_url(
                data.get("content_hash"),
                file_name=data.get("file_name"),
                mime_type=data.get("file_name"),
            )
            link = url_for("exports_api.download", export_id=data.get("id"))

        template = "{{%s}}" % name
        html = html.replace(template, html_link(title, link))
        plain = plain.replace(template, "'%s'" % title)
        if name == event.link_to:
            plain = "%s (%s)" % (plain, link)
    return {"plain": plain, "html": html}
Exemplo n.º 16
0
def render_notification(stub, notification):
    """Generate a text version of the notification, suitable for use
    in an email or text message."""
    from aleph.logic import resolver
    notification = unpack_result(notification)
    event = Events.get(notification.get('event'))
    if event is None:
        return

    for name, clazz, value in _iter_params(notification, event):
        resolver.queue(stub, clazz, value)
    resolver.resolve(stub)
    plain = str(event.template)
    html = str(event.template)
    for name, clazz, value in _iter_params(notification, event):
        data = resolver.get(stub, clazz, value)
        if data is None:
            return
        link, title = None, None
        if clazz == Role:
            title = data.get('label')
        elif clazz == Alert:
            title = data.get('query')
        elif clazz == Collection:
            title = data.get('label')
            link = collection_url(value)
        elif clazz == Entity:
            proxy = model.get_proxy(data)
            title = proxy.caption
            link = entity_url(value)
        elif clazz == Diagram:
            title = data.label
            link = diagram_url(data.id)

        template = '{{%s}}' % name
        html = html.replace(template, html_link(title, link))
        plain = plain.replace(template, "'%s'" % title)
        if name == event.link_to:
            plain = '%s (%s)' % (plain, link)
    return {'plain': plain, 'html': html}
Exemplo n.º 17
0
def resolve_id(object_id, clazz):
    """From an object ID and class type, generate a human-readable
    label and a link that can be rendered into the notification.
    """
    if clazz == Role:
        role = Role.by_id(object_id)
        return role.name, None
    elif clazz == Alert:
        alert = Alert.by_id(object_id)
        return alert.query, None
    elif clazz == Collection:
        collection = Collection.by_id(object_id)
        if collection is not None:
            return collection.label, collection_url(object_id)
    elif clazz in [Document, Entity]:
        entity = get_entity(object_id)
        if entity is not None:
            if Document.SCHEMA in entity.get('schemata'):
                title = entity.get('title', entity.get('file_name'))
                return title, document_url(object_id)
            else:
                return entity.get('name'), entity_url(object_id)
    return None, None
Exemplo n.º 18
0
def sitemap(id):
    """Generate entries for a collection-based sitemap.xml file."""
    # cf. https://www.sitemaps.org/protocol.html
    collection = get_db_collection(id, request.authz.READ)
    document = model.get(Document.SCHEMA)
    entries = []
    for entity in get_sitemap_entities(id):
        updated_at = entity.get('updated_at', '').split('T', 1)[0]
        updated_at = max(settings.SITEMAP_FLOOR, updated_at)
        schema = model.get(entity.get('schema'))
        if schema is None:
            continue
        if schema.is_a(document):
            url = document_url(entity.get('id'))
        else:
            url = entity_url(entity.get('id'))
        entries.append((url, updated_at))
    url = collection_url(collection_id=collection.id)
    updated_at = collection.updated_at.date().isoformat()
    return render_xml('sitemap.xml',
                      url=url,
                      updated_at=updated_at,
                      entries=entries)