def serialize_draft_registration(draft, json_safe=True): if draft.branched_from is not None: url = furl(OSF_DOMAIN) url.path.add(draft.branched_from.url) node_url = url.url else: node_url = None registration_choice = draft.approval.meta.get('registration_choice', None) if registration_choice == EMBARGO: time = parser.parse(draft.approval.meta['embargo_end_date']) embargo = iso8601format(time) if json_safe else time else: embargo = IMMEDIATE return { 'pk': draft._id, 'initiator': serialize_user(draft.initiator), 'registration_metadata': draft.registration_metadata, 'registration_schema': serialize_meta_schema(draft.registration_schema), 'initiated': iso8601format(draft.datetime_initiated) if json_safe else draft.datetime_initiated, 'updated': iso8601format(draft.datetime_updated) if json_safe else draft.datetime_updated, 'submitted': iso8601format(draft.approval.initiation_date) if json_safe else draft.approval.initiation_date, 'requires_approval': draft.requires_approval, 'is_pending_approval': draft.is_pending_review, 'is_approved': draft.is_approved, 'is_rejected': draft.is_rejected, 'notes': draft.notes, 'proof_of_publication': draft.flags.get('proof_of_publication'), 'payment_sent': draft.flags.get('payment_sent'), 'assignee': draft.flags.get('assignee'), 'title': draft.registration_metadata['q1']['value'], 'embargo': embargo, 'registered_node': node_url, }
def serialize_draft_registration(draft, auth=None): from website.project.utils import serialize_node # noqa node = draft.branched_from return { 'pk': draft._id, 'branched_from': serialize_node(node, auth), 'initiator': serialize_initiator(draft.initiator), 'registration_metadata': draft.registration_metadata, 'registration_schema': serialize_meta_schema(draft.registration_schema), 'initiated': utils.iso8601format(draft.datetime_initiated), 'updated': utils.iso8601format(draft.datetime_updated), 'flags': draft.flags, 'urls': { 'edit': node.web_url_for('edit_draft_registration_page', draft_id=draft._id), 'submit': node.api_url_for('submit_draft_for_review', draft_id=draft._id), 'before_register': node.api_url_for('project_before_register'), 'register': node.api_url_for('register_draft_registration', draft_id=draft._id), 'register_page': node.web_url_for('draft_before_register_page', draft_id=draft._id), 'registrations': node.web_url_for('node_registrations') }, 'requires_approval': draft.requires_approval, 'is_pending_approval': draft.is_pending_review, 'is_approved': draft.is_approved, }
def serialize_draft_registration(draft, json_safe=True): node_url = get_url(draft) embargo = get_embargo(draft, json_safe) submitted = None if draft.approval is not None: if json_safe: submitted = iso8601format(draft.approval.initiation_date) else: submitted = draft.approval.initiation_date return { 'pk': draft._id, 'initiator': serialize_user(draft.initiator), 'registration_metadata': draft.registration_metadata, 'registration_schema': serialize_meta_schema(draft.registration_schema), 'initiated': iso8601format(draft.datetime_initiated) if json_safe else draft.datetime_initiated, 'updated': iso8601format(draft.datetime_updated) if json_safe else draft.datetime_updated, 'submitted': submitted, 'requires_approval': draft.requires_approval, 'is_pending_approval': draft.is_pending_review, 'is_approved': draft.is_approved, 'is_rejected': draft.is_rejected, 'notes': draft.notes, 'proof_of_publication': draft.flags.get('proof_of_publication'), 'payment_sent': draft.flags.get('payment_sent'), 'assignee': draft.flags.get('assignee'), 'title': draft.registration_metadata['q1']['value'], 'embargo': embargo, 'registered_node': node_url, 'status': get_approval_status(draft), 'logs': list(map(serialize_draft_logs, draft.status_logs)), }
def serialize_draft_registration(draft, json_safe=True): node_url = get_url(draft) embargo = get_embargo(draft, json_safe) submitted = None if draft.approval is not None: if json_safe: submitted = iso8601format(draft.approval.initiation_date) else: submitted = draft.approval.initiation_date return { 'pk': draft._id, 'initiator': serialize_user(draft.initiator), 'registration_metadata': draft.registration_metadata, 'registration_schema': serialize_meta_schema(draft.registration_schema), 'initiated': iso8601format(draft.datetime_initiated) if json_safe else draft.datetime_initiated, 'updated': iso8601format(draft.datetime_updated) if json_safe else draft.datetime_updated, 'submitted': submitted, 'requires_approval': draft.requires_approval, 'is_pending_approval': draft.is_pending_review, 'is_approved': draft.is_approved, 'is_rejected': draft.is_rejected, 'notes': draft.notes, 'proof_of_publication': draft.flags.get('proof_of_publication'), 'payment_sent': draft.flags.get('payment_sent'), 'assignee': draft.flags.get('assignee'), 'title': draft.registration_metadata['q1']['value'], 'embargo': embargo, 'registered_node': node_url, 'status': get_approval_status(draft), 'logs': map(serialize_draft_logs, draft.status_logs), }
def serialize_draft_registration_approval(approval, auth=None): return { '_id': approval._id, 'end_date': iso8601format(approval.end_date), '_version': approval._version, # list of user ids for authorizers with tokens 'approval_state': approval.approval_state.keys(), 'state': approval.state, 'initiation_date': iso8601format(approval.initiation_date) }
def serialize_log(node_log, anonymous=False): '''Return a dictionary representation of the log.''' return { 'id': str(node_log._primary_key), 'user': node_log.user.serialize() if isinstance(node_log.user, User) else { 'fullname': node_log.foreign_user }, 'contributors': [ node_log._render_log_contributor(c) for c in node_log.params.get("contributors", []) ], 'api_key': node_log.api_key.label if node_log.api_key else '', 'action': node_log.action, 'params': node_log.params, 'date': utils.iso8601format(node_log.date), 'node': node_log.node.serialize() if node_log.node else None, 'anonymous': anonymous }
def _render_node(node, auth=None): """ :param node: :return: """ perm = None # NOTE: auth.user may be None if viewing public project while not # logged in if auth and auth.user and node.get_permissions(auth.user): perm_list = node.get_permissions(auth.user) perm = permissions.reduce_permissions(perm_list) return { 'title': node.title, 'id': node._primary_key, 'url': node.url, 'api_url': node.api_url, 'primary': node.primary, 'date_modified': utils.iso8601format(node.date_modified), 'category': node.category, 'permissions': perm, # A string, e.g. 'admin', or None, 'archiving': node.archiving, }
def _render_node(node, auth=None): """ :param node: :return: """ perm = None # NOTE: auth.user may be None if viewing public project while not # logged in if auth and auth.user and node.get_permissions(auth.user): perm_list = node.get_permissions(auth.user) perm = permissions.reduce_permissions(perm_list) return { "title": node.title, "id": node._primary_key, "url": node.url, "api_url": node.api_url, "primary": node.primary, "date_modified": utils.iso8601format(node.date_modified), "category": node.category, "permissions": perm, # A string, e.g. 'admin', or None, "archiving": node.archiving, }
def serialize_log(node_log, auth=None, anonymous=False): '''Return a dictionary representation of the log.''' return { 'id': str(node_log._primary_key), 'user': node_log.user.serialize() if isinstance(node_log.user, User) else { 'fullname': node_log.foreign_user }, 'contributors': [ node_log._render_log_contributor(c) for c in node_log.params.get("contributors", []) ], 'action': node_log.action, 'params': sanitize.unescape_entities(node_log.params), 'date': utils.iso8601format(node_log.date), 'node': node_log.original_node.serialize(auth) if node_log.original_node else None, 'anonymous': anonymous }
def get_embargo(draft, json_safe): if draft.approval is not None: registration_choice = draft.approval.meta.get('registration_choice', None) if registration_choice == EMBARGO: time = parser.parse(draft.approval.meta['embargo_end_date']) return iso8601format(time) if json_safe else time return IMMEDIATE else: return CANCELED
def test_render_node(self): node = ProjectFactory() res = _render_node(node) assert_equal(res["title"], node.title) assert_equal(res["id"], node._primary_key) assert_equal(res["url"], node.url) assert_equal(res["api_url"], node.api_url) assert_equal(res["primary"], node.primary) assert_equal(res["date_modified"], framework_utils.iso8601format(node.date_modified)) assert_equal(res["category"], "project")
def test_render_node(self): node = ProjectFactory() res = _render_node(node) assert_equal(res['title'], node.title) assert_equal(res['id'], node._primary_key) assert_equal(res['url'], node.url) assert_equal(res['api_url'], node.api_url) assert_equal(res['primary'], node.primary) assert_equal(res['date_modified'], framework_utils.iso8601format(node.date_modified)) assert_equal(res['category'], 'project')
def serialize_draft_registration(draft, json_safe=True): return { 'pk': draft._id, 'initiator': serialize_user(draft.initiator), 'registration_metadata': draft.registration_metadata, 'registration_schema': serialize_meta_schema(draft.registration_schema), 'initiated': iso8601format(draft.datetime_initiated) if json_safe else draft.datetime_initiated, 'updated': iso8601format(draft.datetime_updated) if json_safe else draft.datetime_updated, 'submitted': iso8601format(draft.approval.initiation_date) if json_safe else draft.approval.initiation_date, 'requires_approval': draft.requires_approval, 'is_pending_approval': draft.is_pending_review, 'is_approved': draft.is_approved, 'is_rejected': draft.is_rejected, 'notes': draft.notes, 'proof_of_publication': draft.flags.get('proof_of_publication'), 'payment_sent': draft.flags.get('payment_sent'), 'assignee': draft.flags.get('assignee'), 'title': draft.registration_metadata['q1']['value'], }
def serialize_log(node_log, auth=None, anonymous=False): """Return a dictionary representation of the log.""" return { "id": str(node_log._primary_key), "user": node_log.user.serialize() if isinstance(node_log.user, User) else {"fullname": node_log.foreign_user}, "contributors": [node_log._render_log_contributor(c) for c in node_log.params.get("contributors", [])], "action": node_log.action, "params": sanitize.unescape_entities(node_log.params), "date": utils.iso8601format(node_log.date), "node": node_log.node.serialize(auth) if node_log.node else None, "anonymous": anonymous, }
def to_json(self): return { 'id': self._id, 'date_created': iso8601format(self.created), 'key': self.key, 'name': sanitize.unescape_entities(self.name), 'creator': {'fullname': self.creator.fullname, 'url': self.creator.profile_url}, 'nodes': [{'title': x.title, 'url': x.url, 'scale': str(self.node_scale(x)) + 'px', 'category': x.category} for x in self.nodes.filter(is_deleted=False)], 'anonymous': self.anonymous }
def to_json(self): return { 'id': self._id, 'date_created': iso8601format(self.created), 'key': self.key, 'name': unescape_entities(self.name), 'creator': {'fullname': self.creator.fullname, 'url': self.creator.profile_url}, 'nodes': [{'title': x.title, 'url': x.url, 'scale': str(self.node_scale(x)) + 'px', 'category': x.category} for x in self.nodes.filter(is_deleted=False)], 'anonymous': self.anonymous }
def serialize_draft_registration(draft, auth=None): from website.project.utils import serialize_node # noqa from api.base.utils import absolute_reverse node = draft.branched_from return { 'pk': draft._id, 'branched_from': serialize_node(node, auth), 'initiator': serialize_initiator(draft.initiator), 'registration_metadata': draft.registration_metadata, 'registration_schema': serialize_meta_schema(draft.registration_schema), 'initiated': utils.iso8601format(draft.datetime_initiated), 'updated': utils.iso8601format(draft.datetime_updated), 'flags': draft.flags, 'urls': { 'edit': node.web_url_for('edit_draft_registration_page', draft_id=draft._id, _guid=True), 'before_register': node.api_url_for('project_before_register'), 'register': absolute_reverse('nodes:node-registrations', kwargs={ 'node_id': node._id, 'version': 'v2' }), 'register_page': node.web_url_for('draft_before_register_page', draft_id=draft._id, _guid=True), 'registrations': node.web_url_for('node_registrations', _guid=True) }, 'requires_approval': draft.requires_approval, 'is_pending_approval': draft.is_pending_review, 'is_approved': draft.is_approved, }
def prereg_draft_registrations(auth, **kwargs): """API endpoint; returns prereg draft registrations the user can resume""" drafts = drafts_for_user(auth.user) return { 'draftRegistrations': [ { 'dateUpdated': iso8601format(draft.datetime_updated), 'dateInitiated': iso8601format(draft.datetime_initiated), 'node': { 'title': draft.branched_from.title, }, 'initiator': { 'name': draft.initiator.fullname, }, 'url': draft.branched_from.web_url_for( 'edit_draft_registration_page', draft_id=draft._id, ), } for draft in drafts ], }
def prereg_draft_registrations(auth, **kwargs): """API endpoint; returns prereg draft registrations the user can resume""" drafts = drafts_for_user(auth.user) return { 'draftRegistrations': [{ 'dateUpdated': iso8601format(draft.datetime_updated), 'dateInitiated': iso8601format(draft.datetime_initiated), 'node': { 'title': draft.branched_from.title, }, 'initiator': { 'name': draft.initiator.fullname, }, 'url': draft.branched_from.web_url_for( 'edit_draft_registration_page', draft_id=draft._id, ), } for draft in drafts], }
def draft_registrations(auth, **kwargs): """API endpoint; returns various draft registrations the user can resume their draft""" campaign = kwargs.get('campaign', None) drafts = utils.drafts_for_user(auth.user, campaign) return { 'draftRegistrations': [ { 'dateUpdated': iso8601format(draft.datetime_updated), 'dateInitiated': iso8601format(draft.datetime_initiated), 'node': { 'title': draft.branched_from.title, }, 'initiator': { 'name': draft.initiator.fullname, }, 'url': draft.branched_from.web_url_for( 'edit_draft_registration_page', draft_id=draft._id, ), } for draft in drafts ], }
def serialize_log(node_log, auth=None, anonymous=False): '''Return a dictionary representation of the log.''' return { 'id': str(node_log._primary_key), 'user': node_log.user.serialize() if isinstance(node_log.user, User) else {'fullname': node_log.foreign_user}, 'contributors': [node_log._render_log_contributor(c) for c in node_log.params.get("contributors", [])], 'action': node_log.action, 'params': sanitize.unescape_entities(node_log.params), 'date': utils.iso8601format(node_log.date), 'node': node_log.node.serialize(auth) if node_log.node else None, 'anonymous': anonymous }
def draft_registrations(auth, **kwargs): """API endpoint; returns various draft registrations the user can resume their draft""" campaign = kwargs.get('campaign', None) drafts = utils.drafts_for_user(auth.user, campaign) return { 'draftRegistrations': [{ 'dateUpdated': iso8601format(draft.datetime_updated), 'dateInitiated': iso8601format(draft.datetime_initiated), 'node': { 'title': draft.branched_from.title, }, 'initiator': { 'name': draft.initiator.fullname, }, 'url': draft.branched_from.web_url_for( 'edit_draft_registration_page', draft_id=draft._id, ), } for draft in drafts], }
def test_serialize_log(self): node = NodeFactory(category="hypothesis") node.save() log = NodeLogFactory(params={"node": node._id}, node=node, original_node=node) d = serialize_log(log) assert_equal(d["action"], log.action) assert_equal(d["node"]["node_type"], "component") assert_equal(d["node"]["category"], "Hypothesis") assert_equal(d["node"]["url"], log.node.url) assert_equal(d["date"], framework_utils.iso8601format(log.date)) assert_in("contributors", d) assert_equal(d["user"]["fullname"], log.user.fullname) assert_equal(d["user"]["url"], log.user.url) assert_equal(d["params"], log.params) assert_equal(d["node"]["title"], log.node.title)
def test_serialize_log(self): node = NodeFactory(category='hypothesis') log = NodeLogFactory(params={'node': node._primary_key}) node.logs.append(log) node.save() d = serialize_log(log) assert_equal(d['action'], log.action) assert_equal(d['node']['node_type'], 'component') assert_equal(d['node']['category'], 'Hypothesis') assert_equal(d['node']['url'], log.node.url) assert_equal(d['date'], framework_utils.iso8601format(log.date)) assert_in('contributors', d) assert_equal(d['user']['fullname'], log.user.fullname) assert_equal(d['user']['url'], log.user.url) assert_equal(d['params'], log.params) assert_equal(d['node']['title'], log.node.title)
def _render_node(node, user=None): """ :param node: :return: """ perm = None if user: perm_list = node.get_permissions(user) perm = permissions.reduce_permissions(perm_list) return { 'title': node.title, 'id': node._primary_key, 'url': node.url, 'api_url': node.api_url, 'primary': node.primary, 'date_modified': utils.iso8601format(node.date_modified), 'category': node.category, 'permissions': perm, # A string, e.g. 'admin', or None }
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
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
def serialize_node(node, auth, primary=False): """Build a JSON object containing everything needed to render project.view.mako. """ user = auth.user parent = node.parent_node 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 '', 'url': node.url, 'api_url': node.api_url, 'absolute_url': node.absolute_url, 'display_absolute_url': node.display_absolute_url, '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), 'is_registration': node.is_registration, 'is_retracted': node.is_retracted, 'pending_retraction': node.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, 'pending_embargo': node.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_schema': serialize_meta_schema(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], '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'), }, }, '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_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), }, # TODO: Namespace with nested dicts 'addons_enabled': node.get_addon_names(), 'node_categories': Node.CATEGORY_MAP, } return data
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.order_by('-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).order_by('-forked_date').annotate(nlogs=Count('logs')) ] return data
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, dismissible=False) data = { 'node': { 'id': node._primary_key, 'title': node.title, 'category': node.category_display, 'node_type': node.project_or_component, 'description': node.description or '', 'url': node.url, 'api_url': node.api_url, 'absolute_url': node.absolute_url, 'redirect_url': redirect_url, 'display_absolute_url': node.display_absolute_url, 'in_dashboard': in_dashboard, 'citations': { 'apa': node.citation_apa, 'mla': node.citation_mla, 'chicago': node.citation_chicago, } if not anonymous else '', 'is_public': node.is_public, '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), 'is_registration': node.is_registration, 'registered_from_url': node.registered_from.url if node.is_registration else '', 'registered_date': iso8601format(node.registered_date) if node.is_registration else '', 'registered_meta': [ { 'name_no_ext': from_mongo(meta), 'name_clean': clean_template_name(meta), } for meta in node.registered_meta or [] ], '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.node__forked.find(Q('is_deleted', 'eq', False))), '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)), }, 'parent_node': { 'id': parent._primary_key if parent else '', 'title': parent.title 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 '', 'is_public': parent.is_public if parent else '', 'is_contributor': parent.is_contributor(user) if parent else '', 'can_view': (auth.private_key in parent.private_link_keys_active) if parent else False }, 'user': { 'is_contributor': node.is_contributor(user), 'can_edit': (node.can_edit(auth) and not node.is_registration), '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, } return data
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
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 = AbstractNode.objects.filter(pk=node.pk).include('contributor__user__guids').get() user = auth.user try: contributor = node.contributor_set.get(user=user) except Contributor.DoesNotExist: contributor = None 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.guid_links.filter(_id=node._id).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) addons = list(node.get_addons()) widgets, configs, js, css = _render_addons(addons) 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 addons: messages = addon.before_page_load(node, user) or [] for message in messages: status.push_status_message(message, kind='info', dismissible=False, trust=True) NodeRelation = apps.get_model('osf.NodeRelation') is_registration = node.is_registration 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.created), 'date_modified': iso8601format(node.last_logged) if node.last_logged else '', 'tags': list(node.tags.filter(system=False).values_list('name', flat=True)), 'children': node.nodes_active.exists(), 'child_exists': Node.objects.get_children(node, active=True).exists(), 'is_supplemental_project': node.has_linked_published_preprints, 'is_registration': is_registration, 'is_pending_registration': node.is_pending_registration if is_registration else False, 'is_retracted': node.is_retracted if is_registration else False, 'is_pending_retraction': node.is_pending_retraction if is_registration else False, 'retracted_justification': getattr(node.retraction, 'justification', None) if is_registration else None, 'date_retracted': iso8601format(getattr(node.retraction, 'date_retracted', None)) if is_registration else '', 'embargo_end_date': node.embargo_end_date.strftime('%A, %b %d, %Y') if is_registration and node.embargo_end_date else '', 'is_pending_embargo': node.is_pending_embargo if is_registration else False, 'is_embargoed': node.is_embargoed if is_registration else False, 'is_pending_embargo_termination': is_registration and node.is_embargoed and ( node.embargo_termination_approval and node.embargo_termination_approval.is_pending_approval ), 'registered_from_url': node.registered_from.url if is_registration else '', 'registered_date': iso8601format(node.registered_date) if 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())) if is_registration else False, 'is_fork': node.is_fork, 'is_collected': node.is_collected, 'collections': serialize_collections(node.collecting_metadata_list, auth), '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.exclude(type='osf.registration').filter(is_deleted=False).count(), 'private_links': [x.to_json() for x in node.private_links_active], 'link': view_only_link, 'templated_count': node.templated_list.count(), 'linked_nodes_count': NodeRelation.objects.filter(child=node, is_node_link=True).exclude(parent__type='osf.collection').count(), 'anonymous': anonymous, 'comment_level': node.comment_level, 'has_comments': node.comment_set.exists(), 'identifiers': { 'doi': node.get_identifier_value('doi'), 'ark': node.get_identifier_value('ark'), }, 'visible_preprints': serialize_preprints(node, user), 'institutions': get_affiliated_institutions(node) if node else [], 'has_draft_registrations': node.has_active_draft_registrations, 'access_requests_enabled': node.access_requests_enabled, 'storage_location': node.osfstorage_region.name, 'waterbutler_url': node.osfstorage_region.waterbutler_url, 'mfr_url': node.osfstorage_region.mfr_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', _guid=True) 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': bool(contributor), 'is_admin': bool(contributor) and contributor.admin, 'is_admin_parent': parent.is_admin_parent(user) if parent else False, 'can_edit': bool(contributor) and contributor.write and not node.is_registration, 'can_edit_tags': bool(contributor) and contributor.write, 'has_read_permissions': node.has_permission(user, READ), 'permissions': get_contributor_permissions(contributor, as_list=True) if contributor else [], 'id': user._id if user else None, 'username': user.username if user else None, 'fullname': user.fullname if user else '', 'can_comment': bool(contributor) or node.can_comment(auth), 'show_wiki_widget': _should_show_wiki_widget(node, contributor), 'dashboard_id': bookmark_collection_id, 'institutions': get_affiliated_institutions(user) if user else [], }, # TODO: Namespace with nested dicts 'addons_enabled': [each.short_name for each in addons], '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.items() ] } # Default should be at top of list for UI and for the project overview page the default region # for a component is that of the it's parent node. region_list = get_storage_region_list(user, node=node) data.update({'storage_regions': region_list}) data.update({'storage_flag_is_active': storage_i18n_flag_active()}) if embed_contributors and not anonymous: data['node']['contributors'] = utils.serialize_visible_contributors(node) else: data['node']['contributors'] = list(node.contributors.values_list('guids___id', flat=True)) 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.order_by('-registered_date').exclude(is_deleted=True) ] 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).order_by('-forked_date') ] return data
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, dismissible=False) 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 '', '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, '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), 'is_registration': node.is_registration, 'registered_from_url': node.registered_from.url if node.is_registration else '', 'registered_date': iso8601format(node.registered_date) if node.is_registration else '', 'registered_meta': [ { 'name_no_ext': from_mongo(meta), 'name_clean': clean_template_name(meta), } for meta in node.registered_meta or [] ], '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'), }, }, '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_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
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 = AbstractNode.objects.filter( pk=node.pk).include('contributor__user__guids').get() user = auth.user try: contributor = node.contributor_set.get(user=user) except Contributor.DoesNotExist: contributor = None 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) addons = list(node.get_addons()) widgets, configs, js, css = _render_addons(addons) redirect_url = node.url + '?view_only=None' node_linked_preprint = node.linked_preprint 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 addons: messages = addon.before_page_load(node, user) or [] for message in messages: status.push_status_message(message, kind='info', dismissible=False, trust=True) NodeRelation = apps.get_model('osf.NodeRelation') is_registration = node.is_registration 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.created), 'date_modified': iso8601format(node.last_logged) if node.last_logged else '', 'tags': list( node.tags.filter(system=False).values_list('name', flat=True)), 'children': node.nodes_active.exists(), 'child_exists': Node.objects.get_children(node, active=True).exists(), 'is_registration': is_registration, 'is_pending_registration': node.is_pending_registration if is_registration else False, 'is_retracted': node.is_retracted if is_registration else False, 'is_pending_retraction': node.is_pending_retraction if is_registration else False, 'retracted_justification': getattr(node.retraction, 'justification', None) if is_registration else None, 'date_retracted': iso8601format(getattr(node.retraction, 'date_retracted', None)) if is_registration else '', 'embargo_end_date': node.embargo_end_date.strftime('%A, %b %d, %Y') if is_registration and node.embargo_end_date else '', 'is_pending_embargo': node.is_pending_embargo if is_registration else False, 'is_embargoed': node.is_embargoed if is_registration else False, 'is_pending_embargo_termination': is_registration and node.is_embargoed and (node.embargo_termination_approval and node.embargo_termination_approval.is_pending_approval), 'registered_from_url': node.registered_from.url if is_registration else '', 'registered_date': iso8601format(node.registered_date) if 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())) if is_registration else False, '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.exclude(type='osf.registration').filter( is_deleted=False).count(), 'private_links': [x.to_json() for x in node.private_links_active], 'link': view_only_link, 'templated_count': node.templated_list.count(), 'linked_nodes_count': NodeRelation.objects.filter(child=node, is_node_link=True).exclude( parent__type='osf.collection').count(), 'anonymous': anonymous, 'comment_level': node.comment_level, 'has_comments': node.comment_set.exists(), 'identifiers': { 'doi': node.get_identifier_value('doi'), 'ark': node.get_identifier_value('ark'), }, 'institutions': get_affiliated_institutions(node) if node else [], 'has_draft_registrations': node.has_active_draft_registrations, 'is_preprint': node.is_preprint, 'has_moderated_preprint': node_linked_preprint.provider.reviews_workflow if node_linked_preprint else '', 'preprint_state': node_linked_preprint.machine_state if node_linked_preprint else '', 'preprint_word': node_linked_preprint.provider.preprint_word if node_linked_preprint else '', 'preprint_provider': { 'name': node_linked_preprint.provider.name, 'workflow': node_linked_preprint.provider.reviews_workflow } if node_linked_preprint else {}, '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': bool(contributor), 'is_admin': bool(contributor) and contributor.admin, 'is_admin_parent': parent.is_admin_parent(user) if parent else False, 'can_edit': bool(contributor) and contributor.write and not node.is_registration, 'can_edit_tags': bool(contributor) and contributor.write, 'has_read_permissions': node.has_permission(user, READ), 'permissions': get_contributor_permissions(contributor, as_list=True) if contributor else [], 'id': user._id if user else None, 'username': user.username if user else None, 'fullname': user.fullname if user else '', 'can_comment': bool(contributor) or node.can_comment(auth), 'show_wiki_widget': _should_show_wiki_widget(node, contributor), 'dashboard_id': bookmark_collection_id, 'institutions': get_affiliated_institutions(user) if user else [], }, # TODO: Namespace with nested dicts 'addons_enabled': [each.short_name for each in addons], '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) else: data['node']['contributors'] = list( node.contributors.values_list('guids___id', flat=True)) 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.order_by('-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).order_by('-forked_date').annotate( nlogs=Count('logs')) ] return data