def test_01_imports(self): """import all of the model objects successfully?""" from portality.models import Account from portality.models import Article, ArticleBibJSON, ArticleQuery, ArticleVolumesQuery, DuplicateArticleQuery from portality.models import AtomRecord from portality.models import GenericBibJSON from portality.models import Cache from portality.models import EditorGroupQuery, EditorGroup, EditorGroupMemberQuery from portality.models import ArticleHistory, ArticleHistoryQuery, JournalHistory, JournalHistoryQuery from portality.models import IssnQuery, Journal, JournalBibJSON, JournalQuery, PublisherQuery, TitleQuery from portality.models import LCC from portality.models import Lock from portality.models import OAIPMHArticle, OAIPMHJournal, OAIPMHRecord from portality.models import JournalArticle, JournalArticleQuery from portality.models import Suggestion, SuggestionQuery from portality.models import JournalIssueToC, JournalVolumeToC, ToCQuery, VolumesToCQuery from portality.models import ExistsFileQuery, FileUpload, OwnerFileQuery, ValidFileQuery from portality.models import ObjectDict from portality.models import BulkUpload, BulkReApplication, OwnerBulkQuery j = models.lookup_model("journal") ja = models.lookup_model("journal_article") assert j.__type__ == "journal" assert ja.__type__ == "journal,article"
def test_01_imports(self): """import all of the model objects successfully?""" j = models.lookup_model("journal") ja = models.lookup_model("journal_article") assert j.__type__ == "journal" assert ja.__type__ == "journal,article"
def autocomplete(doc_type, field_name): prefix = request.args.get('q', '') if not prefix: return jsonify( {'suggestions': [{ "id": "", "text": "No results found" }]} ) # select2 does not understand 400, which is the correct code here... m = models.lookup_model(doc_type) if not m: return jsonify( {'suggestions': [{ "id": "", "text": "No results found" }]} ) # select2 does not understand 404, which is the correct code here... size = request.args.get('size', 5) filter_field = app.config.get("AUTOCOMPLETE_ADVANCED_FIELD_MAPS", {}).get(field_name) suggs = [] if filter_field is None: suggs = m.autocomplete(field_name, prefix, size=size) else: suggs = m.advanced_autocomplete(filter_field, field_name, prefix, size=size, prefix_only=False) return jsonify({'suggestions': suggs})
def autocomplete(doc_type, field_name): prefix = request.args.get('q','').lower() if not prefix: return jsonify({'suggestions':[{"id":"", "text": "No results found"}]}) # select2 does not understand 400, which is the correct code here... m = models.lookup_model(doc_type) if not m: return jsonify({'suggestions':[{"id":"", "text": "No results found"}]}) # select2 does not understand 404, which is the correct code here... size = request.args.get('size', 5) return jsonify({'suggestions': m.autocomplete(field_name, prefix, size=size)})
def autocomplete(doc_type, field_name): prefix = request.args.get('q', '').lower() if not prefix: return jsonify({'suggestions': [{"id": "", "text": "No results found"}]}) # select2 does not understand 400, which is the correct code here... # Because AdBock blocks anything with the word advert in it, the doc_type the autocomplete submits had to be renamed. if doc_type == 'adsubmit': doc_type = 'advert' m = models.lookup_model(doc_type) if not m: return jsonify({'suggestions': [{"id": "", "text": "No results found"}]}) # select2 does not understand 404, which is the correct code here... size = request.args.get('size', 5) return jsonify({'suggestions': m.autocomplete(field_name, prefix, size=size)})
def autocomplete(doc_type, field_name): prefix = request.args.get('q', '') if not prefix: return jsonify({'suggestions': [{"id": "", "text": "No results found"}]}) # select2 does not understand 400, which is the correct code here... m = models.lookup_model(doc_type) if not m: return jsonify({'suggestions': [{"id": "", "text": "No results found"}]}) # select2 does not understand 404, which is the correct code here... size = request.args.get('size', 5) filter_field = app.config.get("AUTOCOMPLETE_ADVANCED_FIELD_MAPS", {}).get(field_name) suggs = [] if filter_field is None: suggs = m.autocomplete(field_name, prefix, size=size) else: suggs = m.advanced_autocomplete(filter_field, field_name, prefix, size=size, prefix_only=False) return jsonify({'suggestions': suggs})
def query(path='Pages'): pathparts = path.strip('/').split('/') subpath = pathparts[0] if ',' in subpath: subpaths = subpath.split(',') subpaths = [clean_item for clean_item in [item.strip() for item in subpaths] if clean_item] # strip whitespace else: subpaths = [subpath] if not subpaths: abort(400) # first, decide if we are to 401 the request for subpath in subpaths: if subpath.lower() in app.config.get('NO_QUERY',[]): abort(401) if subpath.lower() in app.config.get("SU_ONLY", []): if current_user.is_anonymous() or not current_user.is_super: abort(401) # now look at the config of this route and determine if the url path # requires us to limit access or not to a user role # owner_filter = False # don't apply the owner filter unless expressly requested default_filter = True # always apply the default filter unless asked otherwise editor_filter = False # don't apply the editor group filter unless expressly requested role = None qr = app.config.get("QUERY_ROUTE", {}) frag = request.path for qroute in qr: if frag.startswith("/" + qroute): default_filter = qr[qroute].get("default_filter", True) role = qr[qroute].get("role") owner_filter = qr[qroute].get("owner_filter") editor_filter = qr[qroute].get("editor_filter", False) associate_filter = qr[qroute].get("associate_filter", False) break # if there is a role, then check that the user is not anonymous and # that the user has the required role if role is not None: if current_user.is_anonymous() or not current_user.has_role(role): abort(401) # if we get to here, then we are good to respond, though check further down # to see if we apply the default filter modelname = '' for s in subpaths: modelname += s.capitalize() klass = models.lookup_model(modelname, capitalize=False) if not klass: abort(404) if len(pathparts) > 1 and pathparts[1] == '_mapping': resp = make_response( json.dumps(klass().query(endpoint='_mapping')) ) elif len(pathparts) == 2 and pathparts[1] not in ['_mapping','_search']: if request.method == 'POST': abort(401) else: rec = klass().pull(pathparts[1]) if rec: if not current_user.is_anonymous() or (app.config.get('PUBLIC_ACCESSIBLE_JSON',True) and rec.data.get('visible',True) and rec.data.get('accessible',True)): resp = make_response( rec.json ) else: abort(401) else: abort(404) else: if request.method == "POST": if request.json: qs = request.json else: qs = dict(request.form).keys()[-1] elif 'q' in request.values: qs = {'query': {'query_string': { 'query': request.values['q'] }}} if 'default_operator' in request.values: qs['query']['query_string']['default_operator'] = request.values['default_operator'] elif 'source' in request.values: unquoted_source = urllib2.unquote(request.values['source']) try: qs = json.loads(unquoted_source) except ValueError: app.logger.exception("Problematic query:\n {0}\n".format(unquoted_source)) else: qs = {'query': {'match_all': {}}} for item in request.values: if item not in ['q','source','callback','_', 'default_operator'] and isinstance(qs,dict): qs[item] = request.values[item] if 'sort' not in qs and app.config.get('DEFAULT_SORT',False): if path.lower() in app.config['DEFAULT_SORT'].keys(): qs['sort'] = app.config['DEFAULT_SORT'][path.lower()] if current_user.is_anonymous() and app.config.get('ANONYMOUS_SEARCH_TERMS',False): if path.lower() in app.config['ANONYMOUS_SEARCH_TERMS'].keys(): if 'bool' not in qs['query']: pq = qs['query'] qs['query'] = { 'bool':{ 'must': [ pq ] } } if 'must' not in qs['query']['bool']: qs['query']['bool']['must'] = [] qs['query']['bool']['must'] = qs['query']['bool']['must'] + app.config['ANONYMOUS_SEARCH_TERMS'][path.lower()] terms = {} shoulds = {} if default_filter: terms.update(_default_filter(subpaths)) if owner_filter: terms.update(_owner_filter()) if editor_filter: shoulds.update(_editor_filter()) if associate_filter: terms.update(_associate_filter()) print terms resp = make_response( json.dumps(klass().query(q=qs, terms=terms, should_terms=shoulds)) ) resp.mimetype = "application/json" return resp
def query(path='Pages'): pathparts = path.strip('/').split('/') subpath = pathparts[0] if ',' in subpath: subpaths = subpath.split(',') subpaths = [ clean_item for clean_item in [item.strip() for item in subpaths] if clean_item ] # strip whitespace else: subpaths = [subpath] if not subpaths: abort(400) # first, decide if we are to 401 the request for subpath in subpaths: if subpath.lower() in app.config.get('NO_QUERY', []): abort(401) if subpath.lower() in app.config.get("SU_ONLY", []): if current_user.is_anonymous() or not current_user.is_super: abort(401) # now look at the config of this route and determine if the url path # requires us to limit access or not to a user role # owner_filter = False # don't apply the owner filter unless expressly requested default_filter = True # always apply the default filter unless asked otherwise editor_filter = False # don't apply the editor group filter unless expressly requested role = None qr = app.config.get("QUERY_ROUTE", {}) frag = request.path for qroute in qr: if frag.startswith("/" + qroute): default_filter = qr[qroute].get("default_filter", True) role = qr[qroute].get("role") owner_filter = qr[qroute].get("owner_filter") editor_filter = qr[qroute].get("editor_filter", False) associate_filter = qr[qroute].get("associate_filter", False) break # if there is a role, then check that the user is not anonymous and # that the user has the required role if role is not None: if current_user.is_anonymous() or not current_user.has_role(role): abort(401) # if we get to here, then we are good to respond, though check further down # to see if we apply the default filter modelname = '' for s in subpaths: modelname += s.capitalize() klass = models.lookup_model(modelname, capitalize=False) if not klass: abort(404) if len(pathparts) > 1 and pathparts[1] == '_mapping': resp = make_response(json.dumps(klass().query(endpoint='_mapping'))) elif len(pathparts) == 2 and pathparts[1] not in ['_mapping', '_search']: if request.method == 'POST': abort(401) else: rec = klass().pull(pathparts[1]) if rec: if not current_user.is_anonymous() or ( app.config.get('PUBLIC_ACCESSIBLE_JSON', True) and rec.data.get('visible', True) and rec.data.get('accessible', True)): resp = make_response(rec.json) else: abort(401) else: abort(404) else: if request.method == "POST": if request.json: qs = request.json else: qs = dict(request.form).keys()[-1] elif 'q' in request.values: qs = {'query': {'query_string': {'query': request.values['q']}}} if 'default_operator' in request.values: qs['query']['query_string'][ 'default_operator'] = request.values['default_operator'] elif 'source' in request.values: unquoted_source = urllib2.unquote(request.values['source']) try: qs = json.loads(unquoted_source) except ValueError: app.logger.exception( "Problematic query:\n {0}\n".format(unquoted_source)) else: qs = {'query': {'match_all': {}}} for item in request.values: if item not in [ 'q', 'source', 'callback', '_', 'default_operator' ] and isinstance(qs, dict): qs[item] = request.values[item] if 'sort' not in qs and app.config.get('DEFAULT_SORT', False): if path.lower() in app.config['DEFAULT_SORT'].keys(): qs['sort'] = app.config['DEFAULT_SORT'][path.lower()] if current_user.is_anonymous() and app.config.get( 'ANONYMOUS_SEARCH_TERMS', False): if path.lower() in app.config['ANONYMOUS_SEARCH_TERMS'].keys(): if 'bool' not in qs['query']: pq = qs['query'] qs['query'] = {'bool': {'must': [pq]}} if 'must' not in qs['query']['bool']: qs['query']['bool']['must'] = [] qs['query']['bool']['must'] = qs['query']['bool'][ 'must'] + app.config['ANONYMOUS_SEARCH_TERMS'][ path.lower()] terms = {} shoulds = {} if default_filter: terms.update(_default_filter(subpaths)) if owner_filter: terms.update(_owner_filter()) if editor_filter: shoulds.update(_editor_filter()) if associate_filter: terms.update(_associate_filter()) print terms resp = make_response( json.dumps(klass().query(q=qs, terms=terms, should_terms=shoulds))) resp.mimetype = "application/json" return resp