def test_create(app, document_json): """Test creating a record.""" record = DocumentRecord.create(document_json) assert DocumentRecord.get_record_by_pid( record['pid'])['pid'] == record['pid'] DocumentRecord.create(document_json, dbcommit=True) assert DocumentRecord.get_record_by_pid( record['pid'])['pid'] == record['pid']
def test_get_record_by_pid(app, document_json): """Test get record by PID.""" assert DocumentRecord.get_record_by_pid('not-existing') is None record = DocumentRecord.create(document_json) assert DocumentRecord.get_record_by_pid( record['pid'])['pid'] == record['pid'] record.delete() assert DocumentRecord.get_record_by_pid(record['pid']) is None
def test_get_record_by_pid(app): """Test get record by PID.""" assert DocumentRecord.get_record_by_pid('ABCD') is None record = DocumentRecord.create({ "pid": "ABCD", "title": "The title of the record" }) assert DocumentRecord.get_record_by_pid('ABCD')['pid'] == 'ABCD' record.delete() assert DocumentRecord.get_record_by_pid('ABCD') is None
def read(cls, user, record): """Read permission check. :param user: Current user record. :param recor: Record to check. :returns: True is action can be done. """ # Only for moderator users. if not user or not user.is_moderator: return False # Superuser is allowed. if user.is_superuser: return True document = DocumentRecord.get_record_by_pid(record['pid']) document = document.replace_refs() # For admin or moderators users, they can access only to their # organisation's documents. for organisation in document['organisation']: if current_organisation['pid'] == organisation['pid']: return True return False
def test_dbcommit(app, document_json): """Test record commit to db.""" record = DocumentRecord.create(document_json) record.dbcommit() assert DocumentRecord.get_record_by_pid( record['pid'])['pid'] == record['pid']
def detail(pid_value, view='global'): """Document detail page.""" record = DocumentRecord.get_record_by_pid(pid_value) if not record or record.get('hiddenFromPublic'): abort(404) # Add restriction, link and thumbnail to files if record.get('_files'): # Check if organisation's record forces to point file to an external # url record['external_url'] = has_external_urls_for_files(record) populate_files_properties(record) # Import is here to avoid a circular reference error. from sonar.modules.documents.serializers import google_scholar_v1, \ schemaorg_v1 # Get schema org data schema_org_data = json.dumps( schemaorg_v1.transform_record(record['pid'], record)) # Get scholar data google_scholar_data = google_scholar_v1.transform_record( record['pid'], record) # Resolve $ref properties record = record.replace_refs() return render_template('documents/record.html', pid=pid_value, record=record, schema_org_data=schema_org_data, google_scholar_data=google_scholar_data)
def detail(pid_value): """Document detail page.""" record = DocumentRecord.get_record_by_pid(pid_value) if not record: abort(404) return render_template('documents/record.html', pid=pid_value, record=record)
def read(cls, user, record): """Read permission check. :param user: Current user record. :param record: Record to check. :returns: True is action can be done. """ # Superuser is allowed. if user and user.is_superuser: return True document = DocumentRecord.get_record_by_pid(record['pid']) document = document.replace_refs() # Moderator can read their own documents. if user and user.is_moderator: if document.has_organisation(current_organisation['pid']): return True return not document.is_masked
def test_file_listener(db, document_with_file): """Test file listener when file is modified.""" # Remove files document_with_file['_files'] = [] document_with_file.commit() db.session.commit() # Reload record record = DocumentRecord.get_record_by_pid(document_with_file['pid']) assert not record['_files'] object_version = ObjectVersion.get_by_bucket(document_with_file['_bucket']) file_uploaded_listener(object_version) assert len(document_with_file.files) == 3 object_version = ObjectVersion.get_by_bucket(document_with_file['_bucket']) file_deleted_listener(object_version)
def detail(pid_value, view='global'): """Document detail page.""" record = DocumentRecord.get_record_by_pid(pid_value) if not record: abort(404) # Add restriction, link and thumbnail to files if record.get('_files'): # Check if organisation's record forces to point file to an external # url record['external_url'] = has_external_urls_for_files(record) populate_files_properties(record) return render_template('documents/record.html', pid=pid_value, record=record)
def update(cls, user, record): """Update permission check. :param user: Current user record. :param record: Record to check. :returns: True is action can be done. """ if not user or not user.is_moderator: return False if user.is_superuser: return True document = DocumentRecord.get_record_by_pid(record['pid']) document = document.replace_refs() # Moderator can update their own documents. if not document.has_organisation(current_organisation['pid']): return False user = user.replace_refs() return document.has_subdivision(user.get('subdivision', {}).get('pid'))
def rerodoc_redirection(pid, filename=None): """Redirection to document with identifier from RERODOC. :param pid: PID from RERODOC. :returns: A redirection to record's detail page or 404 if not found. """ try: pid = PersistentIdentifier.get('rerod', pid) except Exception: abort(404) # Files URLs does not contains view if filename: return redirect( url_for('invenio_records_ui.doc_files', pid_value=pid.get_redirect().pid_value, filename=filename)) doc_pid = pid.get_redirect().pid_value doc = DocumentRecord.get_record_by_pid(doc_pid) if doc: doc = doc.replace_refs() orgs = doc.get('organisation', []) # In case of multiple organisations we redirect to the global view if len(orgs) == 1: org = orgs.pop() # Only for dedicated or shared if org.get('isDedicated') or org.get('isShared'): return redirect( url_for('invenio_records_ui.doc', view=org.get('code'), pid_value=pid.get_redirect().pid_value)) global_view = current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION') return redirect( url_for('invenio_records_ui.doc', view=global_view, pid_value=pid.get_redirect().pid_value))
def get_document(cls, parent_record): """Get the parent document.""" return DocumentRecord.get_record_by_pid(parent_record.get('pid'))