예제 #1
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        # Add subdivision name
        for org_term in results.get('aggregations',
                                    {}).get('subdivision',
                                            {}).get('buckets', []):
            subdivision = SubdivisionRecord.get_record_by_pid(org_term['key'])
            if subdivision:
                org_term['name'] = get_language_value(subdivision['name'])

        return results
예제 #2
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        # Add subdivision name
        for org_term in results.get('aggregations',
                                    {}).get('subdivision',
                                            {}).get('buckets', []):
            subdivision = SubdivisionRecord.get_record_by_pid(org_term['key'])
            if subdivision:
                org_term['name'] = subdivision['name'][0]['value']

        return super(JSONSerializer,
                     self).post_process_serialize_search(results, pid_fetcher)
예제 #3
0
    def _make_subdivision(organisation=None):
        subdivision_json['organisation'] = {
            '$ref':
            'https://sonar.ch/api/organisations/{pid}'.format(pid=organisation)
        }

        subdivision_json.pop('pid', None)

        subdivision = SubdivisionRecord.create(subdivision_json, dbcommit=True)
        subdivision.commit()
        subdivision.reindex()
        db.session.commit()
        return subdivision
예제 #4
0
def subdivision2(app, db, es, admin, organisation, subdivision_json):
    """Second subdivision fixture."""
    json = copy.deepcopy(subdivision_json)
    json['organisation'] = {
        '$ref':
        'https://sonar.ch/api/organisations/{pid}'.format(
            pid=organisation['pid'])
    }

    subdivision = SubdivisionRecord.create(json, dbcommit=True)
    subdivision.commit()
    subdivision.reindex()
    db.session.commit()
    return subdivision
예제 #5
0
def publish(pid=None):
    """Publish a deposit or send a message for review."""
    deposit = DepositRecord.get_record_by_pid(pid)

    if not deposit or deposit[
            'step'] != DepositRecord.STEP_DIFFUSION or deposit[
                'status'] not in [
                    DepositRecord.STATUS_IN_PROGRESS,
                    DepositRecord.STATUS_ASK_FOR_CHANGES
                ]:
        abort(400)

    user = UserRecord.get_record_by_ref_link(deposit['user']['$ref'])

    # Deposit can be validated directly
    if user.is_granted(UserRecord.ROLE_MODERATOR):
        deposit['status'] = DepositRecord.STATUS_VALIDATED

        # Create document based on deposit
        deposit.create_document()
    else:
        deposit['status'] = DepositRecord.STATUS_TO_VALIDATE

        subdivision = SubdivisionRecord.get_record_by_ref_link(
            user['subdivision']['$ref']) if user.get('subdivision') else None

        moderators_emails = user.get_moderators_emails(
            subdivision['pid'] if subdivision else None)

        email_subject = _('Deposit to validate')
        if subdivision:
            email_subject += f' ({get_language_value(subdivision["name"])})'

        if moderators_emails:
            # Send an email to validators
            send_email(
                moderators_emails, email_subject, 'deposits/email/validation',
                {
                    'deposit': deposit,
                    'user': user,
                    'link': current_app.config.get('SONAR_APP_ANGULAR_URL')
                }, False)

    deposit.log_action(user, 'submit')

    deposit.commit()
    deposit.reindex()
    db.session.commit()

    return make_response()
예제 #6
0
    def post_process_serialize_search(self, results, pid_fetcher):
        """Post process the search results."""
        # Add user name
        for org_term in results.get('aggregations',
                                    {}).get('user', {}).get('buckets', []):
            user = UserRecord.get_record_by_pid(org_term['key'])
            if user:
                org_term['name'] = '{last_name}, {first_name}'.format(
                    last_name=user['last_name'], first_name=user['first_name'])

        # Add subdivision name
        for org_term in results.get('aggregations',
                                    {}).get('subdivision',
                                            {}).get('buckets', []):
            subdivision = SubdivisionRecord.get_record_by_pid(org_term['key'])
            if subdivision:
                org_term['name'] = subdivision['name'][0]['value']

        return super(JSONSerializer,
                     self).post_process_serialize_search(results, pid_fetcher)
예제 #7
0
def marc21_to_type_and_organisation(self, key, value):
    """Get document type and organisation from 980 field."""
    subdivision_name = None

    # organisation
    if value.get('b'):
        organisation = value.get('b').lower()

        # Specific transformation for `unisi`, because the real acronym is
        # `usi`.
        if organisation == 'unisi':
            organisation = 'usi'

        # Specific transformation for `hep bejune`, in order to fill out
        # custom fields `Filière` (`customField1`) and `Titre obtenu`
        # (`customField2`)
        if organisation == 'hepbejune' and value.get('f'):
            document_subtype = value.get('f').lower()
            customField1 = ''
            customField2 = ''
            if document_subtype == 'diss_bachelor':
                customField1 = 'Enseignement primaire'
                customField2 = 'Bachelor of Arts in Pre-Primary and Primary Education'
            elif document_subtype == 'diss_master':
                customField1 = 'Enseignement secondaire'
                customField2 = 'Master of Arts or of Science in Secondary Education'
            if customField1:
                self['customField1'] = [customField1]
            if customField2:
                self['customField2'] = [customField2]

        # Specific transformation for `hepfr`, which should be imported as
        # a faculty AND a subdivision of FOLIA/unifr
        if organisation == 'hepfr':
            organisation = 'unifr'
            self['organisation'] = [{
                '$ref':
                OrganisationRecord.get_ref_link('organisations', organisation)
            }]
            # `hepfr` is a faculty of FOLIA/unifr
            self['customField1'] = ['HEP|PH FR']
            # `hepfr` is a subdivision of FOLIA/unifr
            subdivision_name = 'HEP Fribourg'
            # Store subdivision
            # TODO: avoid possible clashes between subdivision
            # names in different languages
            result = RecordSearch()\
                .filter('term', organisation__pid=organisation)\
                .filter('term', name__value__raw=subdivision_name)\
                .source(includes='pid').scan()
            subdivision_pid = next(result).pid
            # If the subdivision exists, assign it to the record
            if subdivision_pid:
                self['subdivisions'] = [{
                    '$ref':
                    SubdivisionRecord.get_ref_link('subdivisions',
                                                   subdivision_pid)
                }]

        # Specific transformation for `bpuge` and `mhnge`, because the real
        # acronym is `vge`.
        subdivision_name = None

        if organisation in [
                'bpuge', 'mhnge', 'baage', 'bmuge', 'imvge', 'mhsge'
        ]:
            subdivision_name = 'bge' if organisation == 'bpuge' else organisation
            organisation = 'vge'

        if organisation not in overdo.registererd_organisations:
            overdo.create_organisation(organisation)
            overdo.registererd_organisations.append(organisation)

        self['organisation'] = [{
            '$ref':
            OrganisationRecord.get_ref_link('organisations', organisation)
        }]

        if subdivision_name:
            # Store subdivision
            hash_key = hashlib.md5(
                (subdivision_name + organisation).encode()).hexdigest()

            subdivision_pid = SubdivisionRecord.get_pid_by_hash_key(hash_key)

            # No subdivision found
            if not subdivision_pid:
                subdivision = SubdivisionRecord.create({
                    'name': [{
                        'language': 'eng',
                        'value': subdivision_name
                    }],
                    'organisation': {
                        '$ref':
                        OrganisationRecord.get_ref_link(
                            'organisations', organisation)
                    },
                    'hashKey':
                    hash_key
                })
                subdivision.commit()
                subdivision.reindex()
                db.session.commit()
                subdivision_pid = subdivision['pid']

            self['subdivisions'] = [{
                '$ref':
                SubdivisionRecord.get_ref_link('subdivisions', subdivision_pid)
            }]

    # get doc type by mapping
    key = value.get('a', '') + '|' + value.get('f', '')
    if key not in TYPE_MAPPINGS:
        current_app.logger.warning(
            'Document type not found in mapping for type "{type}"'.format(
                type=key))
        return None

    # Store types to records
    self['documentType'] = TYPE_MAPPINGS[key]

    return None