Esempio n. 1
0
 def test_serialize_node_license_record(self):
     serialized = serialize_node_license_record(self.node.node_license)
     assert_equal(serialized['name'], self.LICENSE_NAME)
     assert_equal(serialized['id'], self.node_license.id)
     assert_equal(serialized['text'], self.node_license.text)
     assert_equal(serialized['year'], self.YEAR)
     assert_equal(serialized['copyright_holders'], self.COPYRIGHT_HOLDERS)
Esempio n. 2
0
 def test_serialize_node_license_record(self):
     serialized = serialize_node_license_record(self.node.node_license)
     assert_equal(serialized['name'], self.LICENSE_NAME)
     assert_equal(serialized['id'], self.node_license.id)
     assert_equal(serialized['text'], self.node_license.text)
     assert_equal(serialized['year'], self.YEAR)
     assert_equal(serialized['copyright_holders'], self.COPYRIGHT_HOLDERS)
Esempio n. 3
0
def update_node(node, index=None, bulk=False):
    index = index or INDEX
    from website.addons.wiki.model import NodeWikiPage

    category = get_doctype_from_node(node)

    elastic_document_id = node._id
    parent_id = node.parent_id

    from website.files.models.osfstorage import OsfStorageFile

    for file_ in paginated(OsfStorageFile, Q("node", "eq", node)):
        update_file(file_, index=index)

    if node.is_deleted or not node.is_public or node.archiving:
        delete_doc(elastic_document_id, node, index=index)
    else:
        try:
            normalized_title = six.u(node.title)
        except TypeError:
            normalized_title = node.title
        normalized_title = unicodedata.normalize("NFKD", normalized_title).encode("ascii", "ignore")

        elastic_document = {
            "id": elastic_document_id,
            "contributors": [
                {"fullname": x.fullname, "url": x.profile_url if x.is_active else None}
                for x in node.visible_contributors
                if x is not None
            ],
            "title": node.title,
            "normalized_title": normalized_title,
            "category": category,
            "public": node.is_public,
            "tags": [tag._id for tag in node.tags if tag],
            "description": node.description,
            "url": node.url,
            "is_registration": node.is_registration,
            "is_pending_registration": node.is_pending_registration,
            "is_retracted": node.is_retracted,
            "is_pending_retraction": node.is_pending_retraction,
            "embargo_end_date": node.embargo_end_date.strftime("%A, %b. %d, %Y") if node.embargo_end_date else False,
            "is_pending_embargo": node.is_pending_embargo,
            "registered_date": node.registered_date,
            "wikis": {},
            "parent_id": parent_id,
            "date_created": node.date_created,
            "license": serialize_node_license_record(node.license),
            "affiliated_institutions": [inst.name for inst in node.affiliated_institutions],
            "boost": int(not node.is_registration) + 1,  # This is for making registered projects less relevant
        }
        if not node.is_retracted:
            for wiki in [NodeWikiPage.load(x) for x in node.wiki_pages_current.values()]:
                elastic_document["wikis"][wiki.page_name] = wiki.raw_text(node)

        if bulk:
            return elastic_document
        else:
            es.index(index=index, doc_type=category, id=elastic_document_id, body=elastic_document, refresh=True)
Esempio n. 4
0
def serialize_node(node, category):
    from website.addons.wiki.model import NodeWikiPage

    elastic_document = {}
    parent_id = node.parent_id

    try:
        normalized_title = six.u(node.title)
    except TypeError:
        normalized_title = node.title
    normalized_title = unicodedata.normalize('NFKD', normalized_title).encode('ascii', 'ignore')
    elastic_document = {
        'id': node._id,
        'contributors': [
            {
                'fullname': x.fullname,
                'url': x.profile_url if x.is_active else None
            }
            for x in node.visible_contributors
            if x is not None
        ],
        'title': node.title,
        'normalized_title': normalized_title,
        'category': category,
        'public': node.is_public,
        'tags': [tag._id for tag in node.tags if tag],
        'description': node.description,
        'url': node.url,
        'is_registration': node.is_registration,
        'is_pending_registration': node.is_pending_registration,
        'is_retracted': node.is_retracted,
        'is_pending_retraction': node.is_pending_retraction,
        'embargo_end_date': node.embargo_end_date.strftime('%A, %b. %d, %Y') if node.embargo_end_date else False,
        'is_pending_embargo': node.is_pending_embargo,
        'registered_date': node.registered_date,
        'wikis': {},
        'parent_id': parent_id,
        'date_created': node.date_created,
        'license': serialize_node_license_record(node.license),
        'affiliated_institutions': [inst.name for inst in node.affiliated_institutions],
        'boost': int(not node.is_registration) + 1,  # This is for making registered projects less relevant
        'extra_search_terms': clean_splitters(node.title),
    }
    if not node.is_retracted:
        for wiki in [
            NodeWikiPage.load(x)
            for x in node.wiki_pages_current.values()
        ]:
            elastic_document['wikis'][wiki.page_name] = wiki.raw_text(node)

    return elastic_document
Esempio n. 5
0
def serialize_node(node, category):
    NodeWikiPage = apps.get_model('addons_wiki.NodeWikiPage')

    elastic_document = {}
    parent_id = node.parent_id

    try:
        normalized_title = six.u(node.title)
    except TypeError:
        normalized_title = node.title
    normalized_title = unicodedata.normalize('NFKD', normalized_title).encode('ascii', 'ignore')
    elastic_document = {
        'id': node._id,
        'contributors': [
            {
                'fullname': x['fullname'],
                'url': '/{}/'.format(x['guids___id']) if x['is_active'] else None
            }
            for x in node._contributors.filter(contributor__visible=True).order_by('contributor___order')
            .values('fullname', 'guids___id', 'is_active')
        ],
        'title': node.title,
        'normalized_title': normalized_title,
        'category': category,
        'public': node.is_public,
        'tags': list(node.tags.filter(system=False).values_list('name', flat=True)),
        'description': node.description,
        'url': node.url,
        'is_registration': node.is_registration,
        'is_pending_registration': node.is_pending_registration,
        'is_retracted': node.is_retracted,
        'is_pending_retraction': node.is_pending_retraction,
        'embargo_end_date': node.embargo_end_date.strftime('%A, %b. %d, %Y') if node.embargo_end_date else False,
        'is_pending_embargo': node.is_pending_embargo,
        'registered_date': node.registered_date,
        'wikis': {},
        'parent_id': parent_id,
        'date_created': node.date_created,
        'license': serialize_node_license_record(node.license),
        'affiliated_institutions': list(node.affiliated_institutions.values_list('name', flat=True)),
        'boost': int(not node.is_registration) + 1,  # This is for making registered projects less relevant
        'extra_search_terms': clean_splitters(node.title),
    }
    if not node.is_retracted:
        for wiki in NodeWikiPage.objects.filter(guids___id__in=node.wiki_pages_current.values()):
            # '.' is not allowed in field names in ES2
            elastic_document['wikis'][wiki.page_name.replace('.', ' ')] = wiki.raw_text(node)

    return elastic_document
Esempio n. 6
0
 def test_serialize_node_license_record_None(self):
     self.node.node_license = None
     serialized = serialize_node_license_record(self.node.node_license)
     assert_equal(serialized, {})
Esempio n. 7
0
def update_node(node, index=None, bulk=False):
    index = index or INDEX
    from website.addons.wiki.model import NodeWikiPage

    category = get_doctype_from_node(node)

    elastic_document_id = node._id
    parent_id = node.parent_id

    from website.files.models.osfstorage import OsfStorageFile
    for file_ in paginated(OsfStorageFile, Q('node', 'eq', node)):
        update_file(file_, index=index)

    if node.is_deleted or not node.is_public or node.archiving:
        delete_doc(elastic_document_id, node)
    else:
        try:
            normalized_title = six.u(node.title)
        except TypeError:
            normalized_title = node.title
        normalized_title = unicodedata.normalize('NFKD',
                                                 normalized_title).encode(
                                                     'ascii', 'ignore')

        elastic_document = {
            'id':
            elastic_document_id,
            'contributors': [{
                'fullname': x.fullname,
                'url': x.profile_url if x.is_active else None
            } for x in node.visible_contributors if x is not None],
            'title':
            node.title,
            'normalized_title':
            normalized_title,
            'category':
            category,
            'public':
            node.is_public,
            'tags': [tag._id for tag in node.tags if tag],
            'description':
            node.description,
            'url':
            node.url,
            'is_registration':
            node.is_registration,
            'is_pending_registration':
            node.is_pending_registration,
            'is_retracted':
            node.is_retracted,
            'is_pending_retraction':
            node.is_pending_retraction,
            'embargo_end_date':
            node.embargo_end_date.strftime("%A, %b. %d, %Y")
            if node.embargo_end_date else False,
            'is_pending_embargo':
            node.is_pending_embargo,
            'registered_date':
            node.registered_date,
            'wikis': {},
            'parent_id':
            parent_id,
            'date_created':
            node.date_created,
            'license':
            serialize_node_license_record(node.license),
            'affiliated_institutions':
            [inst.name for inst in node.affiliated_institutions],
            'boost':
            int(not node.is_registration) +
            1,  # This is for making registered projects less relevant
        }
        if not node.is_retracted:
            for wiki in [
                    NodeWikiPage.load(x)
                    for x in node.wiki_pages_current.values()
            ]:
                elastic_document['wikis'][wiki.page_name] = wiki.raw_text(node)

        if bulk:
            return elastic_document
        else:
            es.index(index=index,
                     doc_type=category,
                     id=elastic_document_id,
                     body=elastic_document,
                     refresh=True)
Esempio n. 8
0
def update_node(node, index=None, bulk=False):
    index = index or INDEX
    from website.addons.wiki.model import NodeWikiPage

    category = get_doctype_from_node(node)

    elastic_document_id = node._id
    parent_id = node.parent_id

    from website.files.models.osfstorage import OsfStorageFile
    for file_ in paginated(OsfStorageFile, Q('node', 'eq', node)):
        update_file(file_, index=index)

    if node.is_deleted or not node.is_public or node.archiving:
        delete_doc(elastic_document_id, node)
    else:
        try:
            normalized_title = six.u(node.title)
        except TypeError:
            normalized_title = node.title
        normalized_title = unicodedata.normalize('NFKD', normalized_title).encode('ascii', 'ignore')

        elastic_document = {
            'id': elastic_document_id,
            'contributors': [
                {
                    'fullname': x.fullname,
                    'url': x.profile_url if x.is_active else None
                }
                for x in node.visible_contributors
                if x is not None
            ],
            'title': node.title,
            'normalized_title': normalized_title,
            'category': category,
            'public': node.is_public,
            'tags': [tag._id for tag in node.tags if tag],
            'description': node.description,
            'url': node.url,
            'is_registration': node.is_registration,
            'is_pending_registration': node.is_pending_registration,
            'is_retracted': node.is_retracted,
            'is_pending_retraction': node.is_pending_retraction,
            'embargo_end_date': node.embargo_end_date.strftime("%A, %b. %d, %Y") if node.embargo_end_date else False,
            'is_pending_embargo': node.is_pending_embargo,
            'registered_date': node.registered_date,
            'wikis': {},
            'parent_id': parent_id,
            'date_created': node.date_created,
            'license': serialize_node_license_record(node.license),
            'primary_institution': node.primary_institution.name if node.primary_institution else None,
            'boost': int(not node.is_registration) + 1,  # This is for making registered projects less relevant
        }
        if not node.is_retracted:
            for wiki in [
                NodeWikiPage.load(x)
                for x in node.wiki_pages_current.values()
            ]:
                elastic_document['wikis'][wiki.page_name] = wiki.raw_text(node)

        if bulk:
            return elastic_document
        else:
            es.index(index=index, doc_type=category, id=elastic_document_id, body=elastic_document, refresh=True)
Esempio n. 9
0
 def test_serialize_node_license_record_None(self):
     self.node.node_license = None
     serialized = serialize_node_license_record(self.node.node_license)
     assert_equal(serialized, {})
Esempio n. 10
0
def _view_project(node, auth, primary=False):
    """Build a JSON object containing everything needed to render
    project.view.mako.
    """
    user = auth.user

    parent = node.parent_node
    if user:
        dashboard = find_dashboard(user)
        dashboard_id = dashboard._id
        in_dashboard = dashboard.pointing_at(node._primary_key) is not None
    else:
        in_dashboard = False
        dashboard_id = ''
    view_only_link = auth.private_key or request.args.get('view_only',
                                                          '').strip('/')
    anonymous = has_anonymous_link(node, auth)
    widgets, configs, js, css = _render_addon(node)
    redirect_url = node.url + '?view_only=None'

    # Before page load callback; skip if not primary call
    if primary:
        for addon in node.get_addons():
            messages = addon.before_page_load(node, user) or []
            for message in messages:
                status.push_status_message(message,
                                           kind='info',
                                           dismissible=False,
                                           trust=True)
    data = {
        'node': {
            'id':
            node._primary_key,
            'title':
            node.title,
            'category':
            node.category_display,
            'category_short':
            node.category,
            'node_type':
            node.project_or_component,
            'description':
            node.description or '',
            'license':
            serialize_node_license_record(node.license),
            'url':
            node.url,
            'api_url':
            node.api_url,
            'absolute_url':
            node.absolute_url,
            'redirect_url':
            redirect_url,
            'display_absolute_url':
            node.display_absolute_url,
            'update_url':
            node.api_url_for('update_node'),
            'in_dashboard':
            in_dashboard,
            'is_public':
            node.is_public,
            'is_archiving':
            node.archiving,
            'date_created':
            iso8601format(node.date_created),
            'date_modified':
            iso8601format(node.logs[-1].date) if node.logs else '',
            'tags': [tag._primary_key for tag in node.tags],
            'children':
            bool(node.nodes_active),
            'is_registration':
            node.is_registration,
            'is_pending_registration':
            node.is_pending_registration,
            'is_retracted':
            node.is_retracted,
            'is_pending_retraction':
            node.is_pending_retraction,
            'retracted_justification':
            getattr(node.retraction, 'justification', None),
            'embargo_end_date':
            node.embargo_end_date.strftime("%A, %b. %d, %Y")
            if node.embargo_end_date else False,
            'is_pending_embargo':
            node.is_pending_embargo,
            'registered_from_url':
            node.registered_from.url if node.is_registration else '',
            'registered_date':
            iso8601format(node.registered_date)
            if node.is_registration else '',
            'root_id':
            node.root._id,
            'registered_meta':
            node.registered_meta,
            'registered_schemas':
            serialize_meta_schemas(node.registered_schema),
            'registration_count':
            len(node.node__registrations),
            'is_fork':
            node.is_fork,
            'forked_from_id':
            node.forked_from._primary_key if node.is_fork else '',
            'forked_from_display_absolute_url':
            node.forked_from.display_absolute_url if node.is_fork else '',
            'forked_date':
            iso8601format(node.forked_date) if node.is_fork else '',
            'fork_count':
            len(node.forks),
            'templated_count':
            len(node.templated_list),
            'watched_count':
            len(node.watchconfig__watched),
            'private_links': [x.to_json() for x in node.private_links_active],
            'link':
            view_only_link,
            'anonymous':
            anonymous,
            'points':
            len(node.get_points(deleted=False, folders=False)),
            'piwik_site_id':
            node.piwik_site_id,
            'comment_level':
            node.comment_level,
            'has_comments':
            bool(getattr(node, 'commented', [])),
            'has_children':
            bool(getattr(node, 'commented', False)),
            'identifiers': {
                'doi': node.get_identifier_value('doi'),
                'ark': node.get_identifier_value('ark'),
            },
            'alternative_citations':
            [citation.to_json() for citation in node.alternative_citations],
            'has_draft_registrations':
            node.has_active_draft_registrations,
        },
        'parent_node': {
            'exists':
            parent is not None,
            'id':
            parent._primary_key if parent else '',
            'title':
            parent.title if parent else '',
            'category':
            parent.category_display if parent else '',
            'url':
            parent.url if parent else '',
            'api_url':
            parent.api_url if parent else '',
            'absolute_url':
            parent.absolute_url if parent else '',
            'registrations_url':
            parent.web_url_for('node_registrations') if parent else '',
            'is_public':
            parent.is_public if parent else '',
            'is_contributor':
            parent.is_contributor(user) if parent else '',
            'can_view':
            parent.can_view(auth) if parent else False
        },
        'user': {
            'is_contributor': node.is_contributor(user),
            'is_admin': node.has_permission(user, ADMIN),
            'is_admin_parent':
            parent.is_admin_parent(user) if parent else False,
            'can_edit': (node.can_edit(auth) and not node.is_registration),
            'has_read_permissions': node.has_permission(user, READ),
            'permissions': node.get_permissions(user) if user else [],
            'is_watching': user.is_watching(node) if user else False,
            'piwik_token': user.piwik_token if user else '',
            'id': user._id if user else None,
            'username': user.username if user else None,
            'fullname': user.fullname if user else '',
            'can_comment': node.can_comment(auth),
            'show_wiki_widget': _should_show_wiki_widget(node, user),
            'dashboard_id': dashboard_id,
        },
        'badges': _get_badge(user),
        # TODO: Namespace with nested dicts
        'addons_enabled': node.get_addon_names(),
        'addons': configs,
        'addon_widgets': widgets,
        'addon_widget_js': js,
        'addon_widget_css': css,
        'node_categories': Node.CATEGORY_MAP
    }
    return data
Esempio n. 11
0
def _view_project(node, auth, primary=False):
    """Build a JSON object containing everything needed to render
    project.view.mako.
    """
    user = auth.user

    parent = node.parent_node
    if user:
        dashboard = find_dashboard(user)
        dashboard_id = dashboard._id
        in_dashboard = dashboard.pointing_at(node._primary_key) is not None
    else:
        in_dashboard = False
        dashboard_id = ""
    view_only_link = auth.private_key or request.args.get("view_only", "").strip("/")
    anonymous = has_anonymous_link(node, auth)
    widgets, configs, js, css = _render_addon(node)
    redirect_url = node.url + "?view_only=None"

    # Before page load callback; skip if not primary call
    if primary:
        for addon in node.get_addons():
            messages = addon.before_page_load(node, user) or []
            for message in messages:
                status.push_status_message(message, kind="info", dismissible=False, trust=True)
    data = {
        "node": {
            "id": node._primary_key,
            "title": node.title,
            "category": node.category_display,
            "category_short": node.category,
            "node_type": node.project_or_component,
            "description": node.description or "",
            "license": serialize_node_license_record(node.license),
            "url": node.url,
            "api_url": node.api_url,
            "absolute_url": node.absolute_url,
            "redirect_url": redirect_url,
            "display_absolute_url": node.display_absolute_url,
            "update_url": node.api_url_for("update_node"),
            "in_dashboard": in_dashboard,
            "is_public": node.is_public,
            "is_archiving": node.archiving,
            "date_created": iso8601format(node.date_created),
            "date_modified": iso8601format(node.logs[-1].date) if node.logs else "",
            "tags": [tag._primary_key for tag in node.tags],
            "children": bool(node.nodes_active),
            "is_registration": node.is_registration,
            "is_pending_registration": node.is_pending_registration,
            "is_retracted": node.is_retracted,
            "is_pending_retraction": node.is_pending_retraction,
            "retracted_justification": getattr(node.retraction, "justification", None),
            "embargo_end_date": node.embargo_end_date.strftime("%A, %b. %d, %Y") if node.embargo_end_date else False,
            "is_pending_embargo": node.is_pending_embargo,
            "registered_from_url": node.registered_from.url if node.is_registration else "",
            "registered_date": iso8601format(node.registered_date) if node.is_registration else "",
            "root_id": node.root._id,
            "registered_meta": node.registered_meta,
            "registered_schemas": serialize_meta_schemas(node.registered_schema),
            "registration_count": len(node.node__registrations),
            "is_fork": node.is_fork,
            "forked_from_id": node.forked_from._primary_key if node.is_fork else "",
            "forked_from_display_absolute_url": node.forked_from.display_absolute_url if node.is_fork else "",
            "forked_date": iso8601format(node.forked_date) if node.is_fork else "",
            "fork_count": len(node.forks),
            "templated_count": len(node.templated_list),
            "watched_count": len(node.watchconfig__watched),
            "private_links": [x.to_json() for x in node.private_links_active],
            "link": view_only_link,
            "anonymous": anonymous,
            "points": len(node.get_points(deleted=False, folders=False)),
            "piwik_site_id": node.piwik_site_id,
            "comment_level": node.comment_level,
            "has_comments": bool(getattr(node, "commented", [])),
            "has_children": bool(getattr(node, "commented", False)),
            "identifiers": {"doi": node.get_identifier_value("doi"), "ark": node.get_identifier_value("ark")},
            "has_draft_registrations": bool(node.draft_registrations_active),
        },
        "parent_node": {
            "exists": parent is not None,
            "id": parent._primary_key if parent else "",
            "title": parent.title if parent else "",
            "category": parent.category_display if parent else "",
            "url": parent.url if parent else "",
            "api_url": parent.api_url if parent else "",
            "absolute_url": parent.absolute_url if parent else "",
            "registrations_url": parent.web_url_for("node_registrations") if parent else "",
            "is_public": parent.is_public if parent else "",
            "is_contributor": parent.is_contributor(user) if parent else "",
            "can_view": parent.can_view(auth) if parent else False,
        },
        "user": {
            "is_contributor": node.is_contributor(user),
            "is_admin": node.has_permission(user, ADMIN),
            "is_admin_parent": parent.is_admin_parent(user) if parent else False,
            "can_edit": (node.can_edit(auth) and not node.is_registration),
            "has_read_permissions": node.has_permission(user, READ),
            "permissions": node.get_permissions(user) if user else [],
            "is_watching": user.is_watching(node) if user else False,
            "piwik_token": user.piwik_token if user else "",
            "id": user._id if user else None,
            "username": user.username if user else None,
            "fullname": user.fullname if user else "",
            "can_comment": node.can_comment(auth),
            "show_wiki_widget": _should_show_wiki_widget(node, user),
            "dashboard_id": dashboard_id,
        },
        "badges": _get_badge(user),
        # TODO: Namespace with nested dicts
        "addons_enabled": node.get_addon_names(),
        "addons": configs,
        "addon_widgets": widgets,
        "addon_widget_js": js,
        "addon_widget_css": css,
        "node_categories": Node.CATEGORY_MAP,
    }
    return data
Esempio n. 12
0
def _view_project(node, auth, primary=False):
    """Build a JSON object containing everything needed to render
    project.view.mako.
    """
    user = auth.user

    parent = node.parent_node
    if user:
        bookmark_collection = find_bookmark_collection(user)
        bookmark_collection_id = bookmark_collection._id
        in_bookmark_collection = bookmark_collection.pointing_at(node._primary_key) is not None
    else:
        in_bookmark_collection = False
        bookmark_collection_id = ''
    view_only_link = auth.private_key or request.args.get('view_only', '').strip('/')
    anonymous = has_anonymous_link(node, auth)
    widgets, configs, js, css = _render_addon(node)
    redirect_url = node.url + '?view_only=None'

    disapproval_link = ''
    if (node.is_pending_registration and node.has_permission(user, ADMIN)):
        disapproval_link = node.root.registration_approval.stashed_urls.get(user._id, {}).get('reject', '')

    # Before page load callback; skip if not primary call
    if primary:
        for addon in node.get_addons():
            messages = addon.before_page_load(node, user) or []
            for message in messages:
                status.push_status_message(message, kind='info', dismissible=False, trust=True)
    data = {
        'node': {
            'disapproval_link': disapproval_link,
            'id': node._primary_key,
            'title': node.title,
            'category': node.category_display,
            'category_short': node.category,
            'node_type': node.project_or_component,
            'description': node.description or '',
            'license': serialize_node_license_record(node.license),
            'url': node.url,
            'api_url': node.api_url,
            'absolute_url': node.absolute_url,
            'redirect_url': redirect_url,
            'display_absolute_url': node.display_absolute_url,
            'update_url': node.api_url_for('update_node'),
            'in_dashboard': in_bookmark_collection,
            'is_public': node.is_public,
            'is_archiving': node.archiving,
            'date_created': iso8601format(node.date_created),
            'date_modified': iso8601format(node.logs[-1].date) if node.logs else '',
            'tags': [tag._primary_key for tag in node.tags],
            'children': bool(node.nodes_active),
            'is_registration': node.is_registration,
            'is_pending_registration': node.is_pending_registration,
            'is_retracted': node.is_retracted,
            'is_pending_retraction': node.is_pending_retraction,
            'retracted_justification': getattr(node.retraction, 'justification', None),
            'embargo_end_date': node.embargo_end_date.strftime("%A, %b. %d, %Y") if node.embargo_end_date else False,
            'is_pending_embargo': node.is_pending_embargo,
            'is_embargoed': node.is_embargoed,
            'is_pending_embargo_termination': node.is_embargoed and (
                node.embargo_termination_approval and
                node.embargo_termination_approval.is_pending_approval
            ),
            'registered_from_url': node.registered_from.url if node.is_registration else '',
            'registered_date': iso8601format(node.registered_date) if node.is_registration else '',
            'root_id': node.root._id if node.root else None,
            'registered_meta': node.registered_meta,
            'registered_schemas': serialize_meta_schemas(node.registered_schema),
            'registration_count': node.registrations_all.count(),
            'is_fork': node.is_fork,
            'forked_from_id': node.forked_from._primary_key if node.is_fork else '',
            'forked_from_display_absolute_url': node.forked_from.display_absolute_url if node.is_fork else '',
            'forked_date': iso8601format(node.forked_date) if node.is_fork else '',
            'fork_count': node.forks.count(),
            'templated_count': node.templated_list.count(),
            'watched_count': node.watches.count(),
            'private_links': [x.to_json() for x in node.private_links_active],
            'link': view_only_link,
            'anonymous': anonymous,
            'points': len(node.get_points(deleted=False, folders=False)),
            'piwik_site_id': node.piwik_site_id,
            'comment_level': node.comment_level,
            'has_comments': bool(Comment.find(Q('node', 'eq', node))),
            'has_children': bool(Comment.find(Q('node', 'eq', node))),
            'identifiers': {
                'doi': node.get_identifier_value('doi'),
                'ark': node.get_identifier_value('ark'),
            },
            'institution': {
                'name': node.primary_institution.name if node.primary_institution else None,
                'logo_path': node.primary_institution.logo_path if node.primary_institution else None,
                'id': node.primary_institution._id if node.primary_institution else None
            },
            'alternative_citations': [citation.to_json() for citation in node.alternative_citations],
            'has_draft_registrations': node.has_active_draft_registrations,
            'contributors': [contributor._id for contributor in node.contributors]
        },
        'parent_node': {
            'exists': parent is not None,
            'id': parent._primary_key if parent else '',
            'title': parent.title if parent else '',
            'category': parent.category_display if parent else '',
            'url': parent.url if parent else '',
            'api_url': parent.api_url if parent else '',
            'absolute_url': parent.absolute_url if parent else '',
            'registrations_url': parent.web_url_for('node_registrations') if parent else '',
            'is_public': parent.is_public if parent else '',
            'is_contributor': parent.is_contributor(user) if parent else '',
            'can_view': parent.can_view(auth) if parent else False
        },
        'user': {
            'is_contributor': node.is_contributor(user),
            'is_admin': node.has_permission(user, ADMIN),
            'is_admin_parent': parent.is_admin_parent(user) if parent else False,
            'can_edit': (node.can_edit(auth)
                         and not node.is_registration),
            'has_read_permissions': node.has_permission(user, READ),
            'permissions': node.get_permissions(user) if user else [],
            'is_watching': user.is_watching(node) if user else False,
            'piwik_token': user.piwik_token if user else '',
            'id': user._id if user else None,
            'username': user.username if user else None,
            'fullname': user.fullname if user else '',
            'can_comment': node.can_comment(auth),
            'show_wiki_widget': _should_show_wiki_widget(node, user),
            'dashboard_id': bookmark_collection_id,
            'institutions': get_affiliated_institutions(user) if user else [],
        },
        'badges': _get_badge(user),
        # TODO: Namespace with nested dicts
        'addons_enabled': node.get_addon_names(),
        'addons': configs,
        'addon_widgets': widgets,
        'addon_widget_js': js,
        'addon_widget_css': css,
        'node_categories': Node.CATEGORY_MAP
    }
    return data
Esempio n. 13
0
def serialize_node(node, category):
    from website.addons.wiki.model import NodeWikiPage

    elastic_document = {}
    parent_id = node.parent_id

    try:
        normalized_title = six.u(node.title)
    except TypeError:
        normalized_title = node.title
    normalized_title = unicodedata.normalize('NFKD', normalized_title).encode(
        'ascii', 'ignore')
    elastic_document = {
        'id':
        node._id,
        'contributors': [{
            'fullname': x.fullname,
            'url': x.profile_url if x.is_active else None
        } for x in node.visible_contributors if x is not None],
        'title':
        node.title,
        'normalized_title':
        normalized_title,
        'category':
        category,
        'public':
        node.is_public,
        'tags': [tag._id for tag in node.tags if tag],
        'description':
        node.description,
        'url':
        node.url,
        'is_registration':
        node.is_registration,
        'is_pending_registration':
        node.is_pending_registration,
        'is_retracted':
        node.is_retracted,
        'is_pending_retraction':
        node.is_pending_retraction,
        'embargo_end_date':
        node.embargo_end_date.strftime('%A, %b. %d, %Y')
        if node.embargo_end_date else False,
        'is_pending_embargo':
        node.is_pending_embargo,
        'registered_date':
        node.registered_date,
        'wikis': {},
        'parent_id':
        parent_id,
        'date_created':
        node.date_created,
        'license':
        serialize_node_license_record(node.license),
        'affiliated_institutions':
        [inst.name for inst in node.affiliated_institutions],
        'boost':
        int(not node.is_registration) +
        1,  # This is for making registered projects less relevant
        'extra_search_terms':
        clean_splitters(node.title),
    }
    if not node.is_retracted:
        for wiki in [
                NodeWikiPage.load(x) for x in node.wiki_pages_current.values()
        ]:
            elastic_document['wikis'][wiki.page_name] = wiki.raw_text(node)

    return elastic_document
Esempio n. 14
0
def _view_project(node,
                  auth,
                  primary=False,
                  embed_contributors=False,
                  embed_descendants=False,
                  embed_registrations=False,
                  embed_forks=False):
    """Build a JSON object containing everything needed to render
    project.view.mako.
    """
    node = Node.objects.filter(
        pk=node.pk).include('contributor__user__guids').get()
    user = auth.user

    parent = node.find_readable_antecedent(auth)
    if user:
        bookmark_collection = find_bookmark_collection(user)
        bookmark_collection_id = bookmark_collection._id
        in_bookmark_collection = bookmark_collection.linked_nodes.filter(
            pk=node.pk).exists()
    else:
        in_bookmark_collection = False
        bookmark_collection_id = ''
    view_only_link = auth.private_key or request.args.get('view_only',
                                                          '').strip('/')
    anonymous = has_anonymous_link(node, auth)
    widgets, configs, js, css = _render_addon(node)
    redirect_url = node.url + '?view_only=None'

    disapproval_link = ''
    if (node.is_pending_registration and node.has_permission(user, ADMIN)):
        disapproval_link = node.root.registration_approval.stashed_urls.get(
            user._id, {}).get('reject', '')

    if (node.is_pending_embargo and node.has_permission(user, ADMIN)):
        disapproval_link = node.root.embargo.stashed_urls.get(user._id,
                                                              {}).get(
                                                                  'reject', '')

    # Before page load callback; skip if not primary call
    if primary:
        for addon in node.get_addons():
            messages = addon.before_page_load(node, user) or []
            for message in messages:
                status.push_status_message(message,
                                           kind='info',
                                           dismissible=False,
                                           trust=True)
    data = {
        'node': {
            'disapproval_link':
            disapproval_link,
            'id':
            node._primary_key,
            'title':
            node.title,
            'category':
            node.category_display,
            'category_short':
            node.category,
            'node_type':
            node.project_or_component,
            'description':
            node.description or '',
            'license':
            serialize_node_license_record(node.license),
            'url':
            node.url,
            'api_url':
            node.api_url,
            'absolute_url':
            node.absolute_url,
            'redirect_url':
            redirect_url,
            'display_absolute_url':
            node.display_absolute_url,
            'update_url':
            node.api_url_for('update_node'),
            'in_dashboard':
            in_bookmark_collection,
            'is_public':
            node.is_public,
            'is_archiving':
            node.archiving,
            'date_created':
            iso8601format(node.date_created),
            'date_modified':
            iso8601format(node.logs.latest().date)
            if node.logs.exists() else '',
            'tags':
            list(
                node.tags.filter(system=False).values_list('name', flat=True)),
            'children':
            bool(node.nodes_active),
            'is_registration':
            node.is_registration,
            'is_pending_registration':
            node.is_pending_registration,
            'is_retracted':
            node.is_retracted,
            'is_pending_retraction':
            node.is_pending_retraction,
            'retracted_justification':
            getattr(node.retraction, 'justification', None),
            'date_retracted':
            iso8601format(getattr(node.retraction, 'date_retracted', None)),
            'embargo_end_date':
            node.embargo_end_date.strftime('%A, %b. %d, %Y')
            if node.embargo_end_date else False,
            'is_pending_embargo':
            node.is_pending_embargo,
            'is_embargoed':
            node.is_embargoed,
            'is_pending_embargo_termination':
            node.is_embargoed
            and (node.embargo_termination_approval
                 and node.embargo_termination_approval.is_pending_approval),
            'registered_from_url':
            node.registered_from.url if node.is_registration else '',
            'registered_date':
            iso8601format(node.registered_date)
            if node.is_registration else '',
            'root_id':
            node.root._id if node.root else None,
            'registered_meta':
            node.registered_meta,
            'registered_schemas':
            serialize_meta_schemas(list(node.registered_schema.all())),
            'registration_count':
            node.registrations_all.count(),
            'is_fork':
            node.is_fork,
            'forked_from_id':
            node.forked_from._primary_key if node.is_fork else '',
            'forked_from_display_absolute_url':
            node.forked_from.display_absolute_url if node.is_fork else '',
            'forked_date':
            iso8601format(node.forked_date) if node.is_fork else '',
            'fork_count':
            node.forks.filter(is_deleted=False).count(),
            'templated_count':
            node.templated_list.count(),
            'private_links': [x.to_json() for x in node.private_links_active],
            'link':
            view_only_link,
            'anonymous':
            anonymous,
            'points':
            len(node.get_points(deleted=False, folders=False)),
            'comment_level':
            node.comment_level,
            'has_comments':
            bool(Comment.find(Q('node', 'eq', node))),
            'has_children':
            bool(Comment.find(Q('node', 'eq', node))),
            'identifiers': {
                'doi': node.get_identifier_value('doi'),
                'ark': node.get_identifier_value('ark'),
            },
            'institutions':
            get_affiliated_institutions(node) if node else [],
            'alternative_citations': [
                citation.to_json()
                for citation in node.alternative_citations.all()
            ],
            'has_draft_registrations':
            node.has_active_draft_registrations,
            'contributors':
            list(node.contributors.values_list('guids___id', flat=True)),
            'is_preprint':
            node.is_preprint,
            'is_preprint_orphan':
            node.is_preprint_orphan,
            'has_published_preprint':
            node.preprints.filter(
                is_published=True).exists() if node else False,
            'preprint_file_id':
            node.preprint_file._id if node.preprint_file else None,
            'preprint_url':
            node.preprint_url
        },
        'parent_node': {
            'exists':
            parent is not None,
            'id':
            parent._primary_key if parent else '',
            'title':
            parent.title if parent else '',
            'category':
            parent.category_display if parent else '',
            'url':
            parent.url if parent else '',
            'api_url':
            parent.api_url if parent else '',
            'absolute_url':
            parent.absolute_url if parent else '',
            'registrations_url':
            parent.web_url_for('node_registrations') if parent else '',
            'is_public':
            parent.is_public if parent else '',
            'is_contributor':
            parent.is_contributor(user) if parent else '',
            'can_view':
            parent.can_view(auth) if parent else False
        },
        'user': {
            'is_contributor': node.is_contributor(user),
            'is_admin': node.has_permission(user, ADMIN),
            'is_admin_parent':
            parent.is_admin_parent(user) if parent else False,
            'can_edit': (node.can_edit(auth) and not node.is_registration),
            'has_read_permissions': node.has_permission(user, READ),
            'permissions': node.get_permissions(user) if user else [],
            'id': user._id if user else None,
            'username': user.username if user else None,
            'fullname': user.fullname if user else '',
            'can_comment': node.can_comment(auth),
            'show_wiki_widget': _should_show_wiki_widget(node, user),
            'dashboard_id': bookmark_collection_id,
            'institutions': get_affiliated_institutions(user) if user else [],
        },
        'badges':
        _get_badge(user),
        # TODO: Namespace with nested dicts
        'addons_enabled':
        node.get_addon_names(),
        'addons':
        configs,
        'addon_widgets':
        widgets,
        'addon_widget_js':
        js,
        'addon_widget_css':
        css,
        'node_categories': [{
            'value': key,
            'display_name': value
        } for key, value in settings.NODE_CATEGORY_MAP.iteritems()]
    }
    if embed_contributors and not anonymous:
        data['node']['contributors'] = utils.serialize_visible_contributors(
            node)
    if embed_descendants:
        descendants, all_readable = _get_readable_descendants(auth=auth,
                                                              node=node)
        data['user']['can_sort'] = all_readable
        data['node']['descendants'] = [
            serialize_node_summary(node=each,
                                   auth=auth,
                                   primary=not node.has_node_link_to(each),
                                   show_path=False) for each in descendants
        ]
    if embed_registrations:
        data['node']['registrations'] = [
            serialize_node_summary(node=each, auth=auth, show_path=False)
            for each in node.registrations_all.sort('-registered_date').
            exclude(is_deleted=True).annotate(nlogs=Count('logs'))
        ]
    if embed_forks:
        data['node']['forks'] = [
            serialize_node_summary(node=each, auth=auth, show_path=False)
            for each in node.forks.exclude(type='osf.registration').exclude(
                is_deleted=True).sort('-forked_date').annotate(
                    nlogs=Count('logs'))
        ]
    return data