def _operation(operation, identifier): api = API() doc = api.abstract(identifier) if 'bibcode' in doc: if _register_click(): api.link_gateway(doc['bibcode'], operation) if operation in ("trending", "similar"): sort = "score desc" elif operation == "references": sort = "first_author asc" else: sort = "date desc" target_url = _url_for('search', q=f'{operation}(bibcode:{doc["bibcode"]})', sort=sort) if request.cookies.get('core', 'never') == 'always': return redirect(target_url) else: key = "/".join( (app.config['REDIS_RENDER_KEY_PREFIX'], identifier, operation)) return _cached_render_template( key, 'abstract-empty.html', environment=current_app.config['ENVIRONMENT'], base_url=app.config['SERVER_BASE_URL'], doc=doc) else: abort(404)
def paper_form(): """ Paper form x if no search parameters are sent, otherwise process the parameters and redirect to the search results of a built query based on the parameters """ if request.args.get('reference'): # GET # Middle form with reference query query = None form = PaperForm(request.args) api = API() results = api.resolve_reference(request.args.get('reference')) if results.get('resolved', {}).get('bibcode'): query = "bibcode:{}".format(results.get('resolved', {}).get('bibcode')) else: reference_error = "Error occurred resolving reference" else: reference_error = None if request.method == 'POST': # Bottom form with bibcode list form = PaperForm(request.form) else: # GET # Top form with fields form = PaperForm(request.args) query = form.build_query() if query: return redirect(_url_for('search', q=query)) else: return _render_template('paper-form.html', form=form, reference_error=reference_error)
def build_query(self): """ Build query to be send to the main search endpoint """ if self.bibcodes.data: bibcode_list = self.bibcodes.data.split() if len(bibcode_list) > 0: # Store query and get QID api = API() results = api.store_query( self.bibcodes.data.split()) # Split will get rid of \r\n query = "docs({})".format(results['qid']) return query else: return "" else: query = [] if self.bibstem.data: query.append("bibstem:({})".format(self.bibstem.data)) if self.year.data: query.append("year:{}".format(self.year.data)) if self.volume.data: query.append("volume:{}".format(self.volume.data)) if self.page.data: query.append("page:{}".format(self.page.data)) return " ".join(query)
def _objects(self): # TODO: form.object_logic.data is not used (not even in BBB) objects = self.object_names.data.splitlines() api = API() results = api.objects_query(objects) transformed_objects_query = results.get('query') if transformed_objects_query: return transformed_objects_query else: return ""
def _abstract(identifier, section=None): api = API() doc = api.abstract(identifier) if 'bibcode' in doc: if doc['bibcode'] != identifier: target_url = _url_for('abs', identifier=doc['bibcode'], section='abstract') return redirect(target_url, code=301) if _register_click(): api.link_gateway(doc['bibcode'], "abstract") key = "/".join((app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'abstract')) return _cached_render_template(key, 'abstract.html', doc=doc) else: abort(404)
def _metrics(identifier): """ Metrics for a given identifier """ api = API() doc = api.abstract(identifier) if 'bibcode' in doc and doc['bibcode'] != identifier: target_url = _url_for('abs', identifier=doc['bibcode'], section='metrics') return redirect(target_url, code=301) if int(doc.get('metrics', {}).get('citation stats', {}).get('total number of citations', 0)) > 0 or int(doc.get('metrics', {}).get('basic stats', {}).get('total number of reads', 0)) > 0: if 'bibcode' in doc and _register_click(): api.link_gateway(doc['bibcode'], "metrics") key = "/".join((app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'metrics')) return _cached_render_template(key, 'abstract-metrics.html', doc=doc) else: abort(404)
def _graphics(identifier): """ Graphics for a given identifier """ api = API() doc = api.abstract(identifier) if 'bibcode' in doc and doc['bibcode'] != identifier: target_url = _url_for('abs', identifier=doc['bibcode'], section='graphics') return redirect(target_url, code=301) if len(doc.get('graphics', {}).get('figures', [])) > 0: if 'bibcode' in doc and _register_click(): api.link_gateway(doc['bibcode'], "graphics") key = "/".join((app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'graphics')) return _cached_render_template(key, 'abstract-graphics.html', doc=doc) else: abort(404)
def _export(identifier): """ Export bibtex given an identifier """ api = API() doc = api.abstract(identifier) if 'bibcode' in doc and doc['bibcode'] != identifier: target_url = _url_for('abs', identifier=doc['bibcode'], section='exportcitation') return redirect(target_url, code=301) if doc.get('export'): if 'bibcode' in doc and _register_click(): api.link_gateway(doc['bibcode'], "exportcitation") key = "/".join((app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'export')) return _cached_render_template(key, 'abstract-export.html', doc=doc) else: abort(404)
def _toc(identifier): api = API() doc = api.abstract(identifier) if 'bibcode' in doc: if doc['bibcode'] != identifier: target_url = _url_for('abs', identifier=doc['bibcode'], section='toc') return redirect(target_url, code=301) if _register_click(): api.link_gateway(doc['bibcode'], "toc") target_url = _url_for('search', q=f'bibcode:{doc["bibcode"][:13]}*') if request.cookies.get('core', 'never') == 'always': return redirect(target_url) else: key = "/".join((app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'toc')) return _cached_render_template(key, 'abstract-empty.html', doc=doc) else: abort(404)
def _graphics(identifier): """ Graphics for a given identifier """ api = API() doc = api.abstract(identifier) if len(doc.get('graphics', {}).get('figures', [])) > 0: if 'bibcode' in doc and _register_click(): api.link_gateway(doc['bibcode'], "graphics") key = "/".join( (app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'graphics')) return _cached_render_template( key, 'abstract-graphics.html', environment=current_app.config['ENVIRONMENT'], base_url=app.config['SERVER_BASE_URL'], doc=doc) else: abort(404)
def _export(identifier): """ Export bibtex given an identifier """ api = API() doc = api.abstract(identifier) if doc.get('export'): if 'bibcode' in doc and _register_click(): api.link_gateway(doc['bibcode'], "exportcitation") key = "/".join( (app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'export')) return _cached_render_template( key, 'abstract-export.html', environment=current_app.config['ENVIRONMENT'], base_url=app.config['SERVER_BASE_URL'], doc=doc) else: abort(404)
def _toc(identifier): api = API() doc = api.abstract(identifier) if 'bibcode' in doc: if _register_click(): api.link_gateway(doc['bibcode'], "toc") target_url = _url_for('search', q=f'bibcode:{doc["bibcode"][:13]}*') if request.cookies.get('core', 'never') == 'always': return redirect(target_url) else: key = "/".join( (app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'toc')) return _cached_render_template( key, 'abstract-empty.html', environment=current_app.config['ENVIRONMENT'], base_url=app.config['SERVER_BASE_URL'], doc=doc) else: abort(404)
def search(params=None): """ Modern form if no search parameters are sent, otherwise show search results """ form = ModernForm.parse(params or request.args) if form.p_.data > 0: # Redirect to correct the start parameter to match the requested page computed_start = (form.p_.data - 1) * form.rows.data if form.start.data != computed_start: return redirect( _url_for('search', q=form.q.data, sort=form.sort.data, rows=form.rows.data, start=computed_start)) elif form.q.data and len(form.q.data) > 0: if not form.sort.raw_data: # There was not previous sorting specified if "similar(" in form.q.data or "trending(" in form.q.data: form.sort.data = "score desc" elif "references(" in form.q.data: form.sort.data = "first_author asc" api = API() results = api.search(form.q.data, rows=form.rows.data, start=form.start.data, sort=form.sort.data) qtime = "{:.3f}s".format( float(results.get('responseHeader', {}).get('QTime', 0)) / 1000) return render_template('search-results.html', environment=current_app.config['ENVIRONMENT'], base_url=app.config['SERVER_BASE_URL'], form=form, results=results.get('response'), stats=results.get('stats'), error=results.get('error'), qtime=qtime, sort_options=current_app.config['SORT_OPTIONS']) else: return redirect(_url_for('index'))
def _metrics(identifier): """ Metrics for a given identifier """ api = API() doc = api.abstract(identifier) if int( doc.get('metrics', {}).get('citation stats', {}).get( 'total number of citations', 0)) > 0 or int( doc.get('metrics', {}).get('basic stats', {}).get( 'total number of reads', 0)) > 0: if 'bibcode' in doc and _register_click(): api.link_gateway(doc['bibcode'], "metrics") key = "/".join( (app.config['REDIS_RENDER_KEY_PREFIX'], identifier, 'metrics')) return _cached_render_template( key, 'abstract-metrics.html', environment=current_app.config['ENVIRONMENT'], base_url=app.config['SERVER_BASE_URL'], doc=doc) else: abort(404)