def test_end_record_insert(self):
     # verify string specified with %ZEOL gets inserted after each record
     # no EOL string
     custom_format = CustomFormat(custom_format=r'%ZEOL:"" %R')
     custom_format.set_json_from_solr(solrdata.data_6)
     assert (custom_format.get().get('export', ''), "2020AAS...23528705A2019EPSC...13.1911A2015scop.confE...3A2019AAS...23338108A2019AAS...23320704A2018EPJWC.18608001A2018AAS...23221409A2017ASPC..512...45A2018AAS...23136217A2018AAS...23130709A")
     # do not specify EOL string, and default linefeed is added in
     custom_format = CustomFormat(custom_format=r'%R')
     custom_format.set_json_from_solr(solrdata.data_6)
     assert (custom_format.get().get('export', ''), "2020AAS...23528705A\n2019EPSC...13.1911A\n2015scop.confE...3A\n2019AAS...23338108A\n2019AAS...23320704A\n2018EPJWC.18608001A\n2018AAS...23221409A\n2017ASPC..512...45A\n2018AAS...23136217A\n2018AAS...23130709A\n")
     # specify EOL string
     custom_format = CustomFormat(custom_format=r'%ZEOL:"--END" %R')
     custom_format.set_json_from_solr(solrdata.data_6)
     assert (custom_format.get().get('export', ''), "2020AAS...23528705A--END2019EPSC...13.1911A--END2015scop.confE...3A--END2019AAS...23338108A--END2019AAS...23320704A--END2018EPJWC.18608001A--END2018AAS...23221409A--END2017ASPC..512...45A--END2018AAS...23136217A--END2018AAS...23130709A--END")
Esempio n. 2
0
def custom_format_export():
    try:
        payload = request.get_json(force=True)  # post data in json
    except:
        payload = dict(request.form)  # post data in form encoding

    if not payload:
        return return_response({'error': 'no information received'}, 400)
    if 'bibcode' not in payload:
        return return_response(
            {
                'error':
                'no bibcode found in payload (parameter name is `bibcode`)'
            }, 400)
    if 'format' not in payload:
        return return_response(
            {
                'error':
                'no custom format found in payload (parameter name is `format`)'
            }, 400)
    if 'sort' in payload:
        if type(payload['sort']) is list:
            sort = payload['sort'][0]
        else:
            sort = payload['sort']
    else:
        sort = 'date desc, bibcode desc'

    bibcodes = payload['bibcode']
    try:
        if type(payload['format']) is list:
            custom_format_str = payload['format'][0]
        else:
            custom_format_str = payload['format']
    except Exception as e:
        return return_response({'error': 'unable to read custom format'}, 400)

    current_app.logger.info(
        'received request with bibcodes={bibcodes} to export in a custom format: {custom_format_str}  style using sort order={sort}'
        .format(bibcodes=','.join(bibcodes),
                custom_format_str=custom_format_str.encode('utf8'),
                sort=sort))

    if (len(bibcodes) == 0) or (len(custom_format_str) == 0):
        return return_response(
            {'error': 'not all the needed information received'}, 400)

    # pass the user defined format to CustomFormat to parse and we would be able to get which fields
    # in Solr we need to query on
    custom_export = CustomFormat(custom_format=custom_format_str)
    fields = custom_export.get_solr_fields()

    # now get the required data from Solr and send it to customFormat for formatting
    solr_data = get_solr_data(bibcodes=bibcodes, fields=fields, sort=sort)
    if (solr_data is not None):
        if ('error' in solr_data):
            return return_response({'error': 'unable to query solr'}, 400)
        custom_export.set_json_from_solr(solr_data)
        return return_response(custom_export.get(), 200, 'POST')
    return return_response({'error': 'no result from solr'}, 404)
 def test_page_count(self):
     # verify %pc outputs page_count properly
     formatted = [u'Yang, Huihui and Chen, Hongshan. (2017). 71, 191, 9 pp.\n',
                  u'Knapp, Wilfried and Thuemen, Chris. (2017). 13, 25, 6 pp.\n']
     custom_format = CustomFormat(custom_format=r'%A. (%Y). %q, %V, %p, %pc pp.')
     custom_format.set_json_from_solr(solrdata.data_8)
     assert (custom_format.get().get('export', '') == ''.join(formatted))
Esempio n. 4
0
 def test_custom(self):
     # format the stubdata using the code
     custom_format = CustomFormat(custom_format=r'%ZEncoding:latex\\bibitem[%2.1m\\(%Y)]{%2H%Y}\ %5.3l\ %Y\,%j\,%V\,%p ')
     custom_format.set_json_from_solr(solrdata.data)
     # now compare it with an already formatted data that we know is correct
     assert (custom_format.get() == customTest.data)
     # verify correct solr fields are fetched
     assert (custom_format.get_solr_fields() == 'author,year,pub,volume,page,page_range,bibcode,bibstem')
Esempio n. 5
0
def custom_format_export():
    try:
        payload = request.get_json(force=True)  # post data in json
    except:
        payload = dict(request.form)  # post data in form encoding

    if not payload:
        return return_response({'error': 'no information received'}, 400)
    if 'bibcode' not in payload:
        return return_response(
            {
                'error':
                'no bibcode found in payload (parameter name is `bibcode`)'
            }, 400)
    if 'format' not in payload:
        return return_response(
            {
                'error':
                'no custom format found in payload (parameter name is `format`)'
            }, 400)

    bibcodes = payload['bibcode']
    custom_format_str = payload['format']

    current_app.logger.debug(
        'received request with bibcodes={bibcodes} to export in a custom format: {custom_format_str}'
        .format(bibcodes=''.join(bibcodes),
                custom_format_str=custom_format_str))

    if (len(bibcodes) == 0) or (len(custom_format_str) == 0):
        return return_response(
            {'error': 'not all the needed information received'}, 400)

    # pass the user defined format to CustomFormat to parse and we would be able to get which fields
    # in Solr we need to query on
    custom_export = CustomFormat(custom_format=custom_format_str)
    fields = custom_export.get_solr_fields()
    # now get the required data from Solr and send it to customFormat for formatting
    solr_data = get_solr_data(bibcodes=bibcodes, fields=fields)
    if (solr_data is not None):
        if ('error' in solr_data):
            return return_response({'error': 'unable to query solr'}, 400)
        custom_export.set_json_from_solr(solr_data)
        return return_response(custom_export.get(), 200)
    return return_response({'error': 'no result from solr'}, 404)