def index(): """Frontpage.""" records = FrontpageRecordsSearch()[:5].sort("-created").execute() for r in records: r = UIJSONSerializer().serialize_object_to_dict(r.to_dict()) return render_template("invenio_theme_tugraz/index.html", records=records)
def deposit_edit(draft=None, pid_value=None): """Edit an existing deposit.""" # TODO: should be embedded in record service files_service = RDMDraftFilesService() files_list = files_service.list_files( id_=pid_value, identity=g.identity, links_config=RDMDraftFilesResourceConfig.links_config, ) serializer = UIJSONSerializer() record = serializer.serialize_object_to_dict(draft.to_dict()) # TODO: get the `is_published` field when reading the draft from invenio_pidstore.errors import PIDUnregistered try: service().draft_cls.pid.resolve(pid_value, registered_only=True) record["is_published"] = True except PIDUnregistered: record["is_published"] = False return render_template( "invenio_app_rdm/records/deposit.html", forms_config=get_form_config(apiUrl=f"/api/records/{pid_value}/draft"), record=record, files=files_list.to_dict(), searchbar_config=dict(searchUrl=get_search_url()), )
def test_ui_serializer(app, full_record): """Test UI serializer.""" expected_data = { # 'access_right': {}, 'contributors': { 'affiliations': [[1, 'CERN']], 'contributors': [{ 'affiliations': [[1, 'CERN']], 'family_name': 'Nielsen', 'given_name': 'Lars Holm', 'identifiers': { 'orcid': '0000-0001-8135-3489' }, 'name': 'Nielsen, Lars Holm', 'role': 'other', 'type': 'personal' }] }, 'creators': { 'affiliations': [[1, 'CERN']], 'creators': [{ 'affiliations': [[1, 'CERN']], 'family_name': 'Nielsen', 'given_name': 'Lars Holm', 'identifiers': { 'orcid': '0000-0001-8135-3489' }, 'name': 'Nielsen, Lars Holm', 'type': 'personal' }] }, 'publication_date_l10n_long': 'January 2018 – September 2020', 'publication_date_l10n_medium': 'Jan 2018 – Sep 2020', 'resource_type': 'Journal article', 'languages': [{ 'id': 'da', 'title_l10n': "Danish" }, { 'id': 'en', 'title_l10n': "English" }], 'description_stripped': 'Test', } with app.app_context(): serialized_record = UIJSONSerializer().serialize_object_to_dict( full_record) assert serialized_record['ui'] == expected_data serialized_records = UIJSONSerializer().serialize_object_list( {"hits": { "hits": [full_record] }}) assert json.loads(serialized_records)['hits']['hits'][0]['ui'] == \ expected_data
def test_ui_serializer(app, full_record_ui): """Test UI serializer.""" # full_record_ui = full_record full_record_ui['access']['status'] = 'embargoed' expected_data = { 'access_status': { 'description_l10n': 'The files will be made publicly available on ' 'January 1, 2131.', 'icon': 'outline clock', 'id': 'embargoed', 'title_l10n': 'Embargoed', 'embargo_date_l10n': 'January 1, 2131', 'message_class': 'warning', }, 'contributors': { 'affiliations': [[1, 'CERN']], 'contributors': [{ 'affiliations': [[1, 'CERN']], 'family_name': 'Nielsen', 'given_name': 'Lars Holm', 'identifiers': { 'orcid': '0000-0001-8135-3489' }, 'name': 'Nielsen, Lars Holm', 'role': 'other', 'type': 'personal' }] }, 'creators': { 'affiliations': [[1, 'CERN']], 'creators': [{ 'affiliations': [[1, 'CERN']], 'family_name': 'Nielsen', 'given_name': 'Lars Holm', 'identifiers': { 'orcid': '0000-0001-8135-3489'}, 'name': 'Nielsen, Lars Holm', 'type': 'personal' }] }, 'publication_date_l10n_long': 'January 2018 – September 2020', 'publication_date_l10n_medium': 'Jan 2018 – Sep 2020', 'resource_type': 'Journal article', 'languages': [ {'id': 'da', 'title_l10n': "Danish"}, {'id': 'en', 'title_l10n': "English"} ], 'description_stripped': 'Test', 'version': 'v1.0', } with app.app_context(): serialized_record = UIJSONSerializer().serialize_object_to_dict( full_record_ui) assert serialized_record['ui'] == expected_data serialized_records = UIJSONSerializer().serialize_object_list( {"hits": {"hits": [full_record_ui]}}) assert json.loads(serialized_records)['hits']['hits'][0]['ui'] == \ expected_data
def test_ui_serializer(minimal_record): """Test UI serializer.""" expected_data = {"access_right": {"category": "open"}} serialized_record = UIJSONSerializer().serialize_object(minimal_record) assert json.loads(serialized_record)['ui'] == expected_data serialized_records = UIJSONSerializer().serialize_object_list( {"hits": {"hits": [minimal_record]}}) assert json.loads(serialized_records)['hits']['hits'][0]['ui'] == \ expected_data
def deposit_edit(draft=None, draft_files=None, pid_value=None): """Edit an existing deposit.""" serializer = UIJSONSerializer() record = serializer.serialize_object_to_dict(draft.to_dict()) return render_template( "invenio_app_rdm/records/deposit.html", forms_config=get_form_config(apiUrl=f"/api/records/{pid_value}/draft"), record=record, files=draft_files.to_dict(), searchbar_config=dict(searchUrl=get_search_url()), permissions=draft.has_permissions_to(['new_version']))
def record_detail(record=None, files=None, pid_value=None, is_preview=False): """Record detail page (aka landing page).""" files_dict = None if files is None else files.to_dict() record_ui = UIJSONSerializer().serialize_object_to_dict(record.to_dict()) is_draft = record_ui["is_draft"] if is_preview and is_draft: try: current_rdm_records.records_service.validate_draft( g.identity, record.id ) except ValidationError: abort(404) community_uuid = get_community_uuid(record_ui) return render_template( "invenio_app_rdm/records/detail.html", record=record_ui, pid=pid_value, files=files_dict, permissions=record.has_permissions_to(['edit', 'new_version', 'manage', 'update_draft', 'read_files']), is_preview=is_preview, is_draft=is_draft, community=community_uuid, )
def record_export(record=None, export_format=None, pid_value=None, permissions=None): """Export page view.""" # Get the configured serializer exporter = current_app.config.get("APP_RDM_RECORD_EXPORTERS", {}).get(export_format) if exporter is None: abort(404) serializer = obj_or_import_string( exporter["serializer"])(options={ "indent": 2, "sort_keys": True, }) exported_record = serializer.serialize_object(record.to_dict()) return render_template( "invenio_app_rdm/records/export.html", export_format=exporter.get("name", export_format), exported_record=exported_record, record=UIJSONSerializer().serialize_object_to_dict(record.to_dict()), permissions=record.has_permissions_to(['update_draft']), )
def _resolve_topic_draft(request): """Resolve the record in the topic when it is a draft.""" user_owns_request = \ str(request["expanded"]["created_by"]["id"]) == str(current_user.id) if request["is_closed"] and not user_owns_request: return dict(permissions={}, record_ui=None) recid = ResolverRegistry.resolve_entity_proxy( request["topic"])._parse_ref_dict_id() try: record = current_rdm_records_service.read_draft(g.identity, recid, expand=True) record_ui = UIJSONSerializer().serialize_object_to_dict( record.to_dict()) permissions = record.has_permissions_to([ "edit", "new_version", "manage", "update_draft", "read_files", "review", ]) return dict(permissions=permissions, record_ui=record_ui) except NoResultFound: # record tab not displayed when the record is not found # the request is probably not open anymore pass return dict(permissions={}, record_ui=None)
def records_serializer(records=None): """Serialize list of records.""" record_list = [] for record in records: record_list.append(UIJSONSerializer().serialize_object_to_dict( record.to_dict())) return record_list
def deposit_edit(draft=None, pid_value=None): """Edit an existing deposit.""" files_list = current_rdm_records.draft_files_service.list_files( id_=pid_value, identity=g.identity, links_config=RDMDraftFilesResourceConfig.links_config, ) serializer = UIJSONSerializer() record = serializer.serialize_object_to_dict(draft.to_dict()) return render_template( "invenio_app_rdm/records/deposit.html", forms_config=get_form_config(apiUrl=f"/api/records/{pid_value}/draft"), record=record, files=files_list.to_dict(), searchbar_config=dict(searchUrl=get_search_url()), permissions=draft.has_permissions_to(['new_version']))
def record_detail(record=None, files=None, pid_value=None): """Record detail page (aka landing page).""" files_dict = None if files is None else files.to_dict() return render_template( "invenio_app_rdm/records/detail.html", record=UIJSONSerializer().serialize_object_to_dict(record.to_dict()), pid=pid_value, files=files_dict, permissions=record.has_permissions_to(['edit', 'new_version']), )
def record_detail(record=None, files=None, pid_value=None, is_preview=False): """Record detail page (aka landing page).""" files_dict = None if files is None else files.to_dict() return render_template( "invenio_theme_tugraz/landingpage/detail.html", record=UIJSONSerializer().serialize_object_to_dict(record.to_dict()), pid=pid_value, files=files_dict, permissions=record.has_permissions_to(['edit', 'new_version', 'manage', 'update_draft', 'read_files']), is_preview=is_preview, )
def deposits_edit(pid_value): """Deposit edit page.""" links_config = RDMDraftResourceConfig.links_config draft = service.read_draft(id_=pid_value, identity=g.identity, links_config=links_config) files_service = RDMDraftFilesService() files_list = files_service.list_files( id_=pid_value, identity=g.identity, links_config=RDMDraftFilesResourceConfig.links_config) forms_config = dict(apiUrl=f"/api/records/{pid_value}/draft", vocabularies=Vocabularies.dump(), current_locale=str(current_i18n.locale)) # Dereference relations (languages, licenses, etc.) draft._record.relations.dereference() serializer = UIJSONSerializer() record = serializer.serialize_object_to_dict(draft.to_dict()) # TODO: get the `is_published` field when reading the draft from invenio_pidstore.errors import PIDUnregistered try: service.draft_cls.pid.resolve(pid_value, registered_only=True) record["is_published"] = True except PIDUnregistered: record["is_published"] = False return render_template( current_app.config['DEPOSITS_FORMS_BASE_TEMPLATE'], forms_config=forms_config, record=record, files=files_list.to_dict(), searchbar_config=dict(searchUrl=search_url))
def _metadata_builder(metadata: Dict, scheme) -> Dict: """Generate a standardized metadata for all retrievers used. Args: metadata (Dict): Metadata from related resources Returns: Dict: Dictionary with standard metadata """ # ToDo: Change the nomenclature and use the idea of serializers here. metadata_field_for_scheme = metadata_field_by_scheme.get(scheme) return { **{ metadata_field: py_.get(metadata, metadata_field_for_scheme[metadata_field], "") for metadata_field in metadata_field_for_scheme.keys() }, "ui": UIJSONSerializer().serialize_object_to_dict(metadata).get("ui"), }
def record_detail_page_render(record=None, files=None, pid_value=None, is_preview=False): """Function to render landing page (Record detail page) """ files_dict = None if files is None else files.to_dict() related_records_informations = get_related_resources_metadata(record.to_dict()["metadata"]) current_user_profile = current_user_invenio_profile() return render_template( "gkhext/records/detail.html", record=UIJSONSerializer().serialize_object_to_dict(record.to_dict()), pid=pid_value, files=files_dict, permissions=record.has_permissions_to(['edit', 'new_version', 'manage', 'update_draft', 'read_files']), is_preview=is_preview, current_user_profile=current_user_profile, related_records_informations=related_records_informations )
def _metadata_builder(metadata: Dict, scheme) -> Dict: """Generate a standardized metadata for all retrievers used Args: metadata (Dict): Metadata from related resources Returns: Dict: Dictionary with standard metadata """ metadata_field_for_scheme = metadata_field_by_scheme.get(scheme) _config = kaptan.Kaptan() _config.import_config(metadata) return { **{ metadata_field: _config.get(metadata_field_for_scheme[metadata_field]) for metadata_field in metadata_field_for_scheme.keys() }, "ui": UIJSONSerializer().serialize_object_to_dict(metadata).get("ui") }
def record_detail(record=None, files=None, pid_value=None, is_preview=False): # This function renders the records page. It acts as a proxy. When the rendering is requested, # the call passes through this function, which can make changes to the requested object. # These modifications can be adding new content, removing fields, and so on files_dict = None if files is None else files.to_dict() import json print(dir(record)) print(record.to_dict()) metadata = record.to_dict()["metadata"] if "related_identifiers" in metadata: print(record.to_dict()["metadata"]["related_identifiers"]) return render_template( "gkhext/detail.html", record=UIJSONSerializer().serialize_object_to_dict(record.to_dict()), pid=pid_value, files=files_dict, permissions=record.has_permissions_to(['edit', 'new_version', 'manage', 'update_draft', 'read_files']), is_preview=is_preview, )
def serialize_ui(record): """Returns the UI serialization of a record.""" serializer = UIJSONSerializer() # We need a dict not a string return serializer.serialize_object_to_dict(record)
def test_ui_serializer(app, full_to_dict_record): expected_data = { 'access_status': { 'description_l10n': 'The files will be made publicly available on ' 'January 1, 2131.', 'icon': 'outline clock', 'id': 'embargoed', 'title_l10n': 'Embargoed', 'embargo_date_l10n': 'January 1, 2131', 'message_class': 'warning', }, 'contributors': { 'affiliations': [[1, 'CERN', 'cern']], 'contributors': [{ 'affiliations': [[1, 'CERN']], 'person_or_org': { 'family_name': 'Nielsen', 'given_name': 'Lars Holm', "identifiers": [ { "identifier": "0000-0001-8135-3489", "scheme": "orcid" } ], 'name': 'Nielsen, Lars Holm', 'type': 'personal' }, 'role': {'id': 'other', 'title': 'other'}, }] }, 'creators': { 'affiliations': [[1, 'CERN', 'cern'], [2, 'free-text', None]], 'creators': [{ 'affiliations': [[1, 'CERN'], [2, 'free-text']], 'person_or_org': { 'family_name': 'Nielsen', 'given_name': 'Lars Holm', "identifiers": [ { "identifier": "0000-0001-8135-3489", "scheme": "orcid" } ], 'name': 'Nielsen, Lars Holm', 'type': 'personal' } }] }, 'publication_date_l10n_long': 'January 2018 – September 2020', 'publication_date_l10n_medium': 'Jan 2018 – Sep 2020', 'resource_type': { 'id': 'publication-article', 'title_l10n': 'Journal article' }, 'additional_titles': [ { 'lang': {'id': 'eng'}, 'title': 'a research data management platform', 'type': {'id': 'subtitle'} } ], 'additional_descriptions': [ { 'description': 'Bla bla bla', 'lang': {'id': 'eng'}, 'type': {'id': 'methods'} } ], 'languages': [ {'id': 'dan', 'title_l10n': "Danish"}, {'id': 'eng', 'title_l10n': "English"} ], 'dates': [ {'date': '1939/1945', 'description': 'A date', 'type': {'id': 'other'} } ], 'rights': [ { 'description_l10n': 'A description', 'link': 'https://customlicense.org/licenses/by/4.0/', 'title_l10n': 'A custom license' }, { 'id': 'cc-by-4.0' } ], 'related_identifiers': [ {'identifier': '10.1234/foo.bar', 'relation_type': {'id': 'iscitedby', 'title_l10n': 'Is cited by'}, 'resource_type': {'id': 'dataset', 'title_l10n': 'Dataset'}, 'scheme': 'doi'} ], 'description_stripped': 'A description \nwith HTML tags', 'version': 'v1.0', } serialized_record = UIJSONSerializer().serialize_object_to_dict( full_to_dict_record) assert serialized_record['ui'] == expected_data serialized_records = UIJSONSerializer().serialize_object_list( {"hits": {"hits": [full_to_dict_record]}}) assert json.loads(serialized_records)['hits']['hits'][0]['ui'] == \ expected_data
def requests_detail(request=None, pid_value=None): """Requests detail page.""" request_dict = request.to_dict() default_query_config = dict( size=current_app.config['REQUESTS_TIMELINE_PAGE_SIZE']) # temporarily, until serializers and avatars implemented try: user_email = request_dict["created_by"]["user"] request_dict["created_by"].update({ "avatar": "/static/images/placeholder.png", "full_name": user_email }) except KeyError: request_dict["created_by"].update({ "avatar": "/static/images/placeholder.png", "full_name": request_dict["created_by"]["community"] }) topic_record = None is_draft = None permissions = [] is_topic_record_type = 'record' in request_dict["topic"] if is_topic_record_type: try: topic_record = current_rdm_records_service.read_draft( id_=request_dict["topic"]["record"], identity=g.identity) except NoResultFound: topic_record = current_rdm_records_service.read( id_=request_dict["topic"]["record"], identity=g.identity) permissions = topic_record.has_permissions_to( ['edit', 'new_version', 'manage', 'update_draft', 'read_files']) topic_record = UIJSONSerializer().serialize_object_to_dict( topic_record.data) is_draft = topic_record["is_draft"] request_dict["topic"] = topic_record # end temporary block try: return render_template( f"invenio_requests/{request_dict['type']}/index.html", # TODO: implement and use request UI serializer request=request_dict, record=topic_record, is_preview=True, is_draft=is_draft, permissions=permissions, files=[], default_query_config=default_query_config) except TemplateNotFound: return render_template( "invenio_requests/details/index.html", # TODO: implement and use request UI serializer request=request_dict, record=topic_record, default_query_config=default_query_config)