def get(self, cstruct): if cstruct is None: raise BadRequest() project = self.decode(None, cstruct) if project is None: raise BadRequest() return project
def login(provider): if provider not in PROVIDERS: raise BadRequest('Unknown provider: %s' % provider) callback = url_for('sessions_api.%s_authorized' % provider) session.clear() if not request.args.get('next_url'): raise BadRequest("No 'next_url' is specified.") session['next_url'] = request.args.get('next_url') return PROVIDERS[provider].authorize(callback=callback)
def merge(): validator = entities.MergeValidator() data = validator.deserialize(request_data()) authz.require(authz.project_edit(data['orig'].project)) if data['orig'].id == data['dest'].id: raise BadRequest('Origin and destination are identical.') if data['orig'].project_id != data['dest'].project_id: raise BadRequest('Entities belong to different projects.') dest = entities.merge(data['orig'], data['dest']) db.session.commit() return jsonify(dest)
def suggest(): if not 'q' in request.args or not len(request.args.get('q').strip()): raise BadRequest("Missing the query ('q' parameter).") q = db.session.query(EntityProperty) q = q.filter(EntityProperty.name=='name') q = q.filter(EntityProperty.active==True) q = q.filter(EntityProperty.entity_id!=None) q = q.filter(EntityProperty.value.ilike(request.args.get('q') + '%')) if 'project' in request.args: q = q.join(Entity) q = q.join(Project) q = q.filter(Project.slug==request.args.get('project')) pager = Pager(q) data = [] def convert(props): for prop in props: data.append({ 'name': prop.value, 'api_url': url_for('entities_api.view', id=prop.entity_id) }) return data validate_cache(keys='#'.join([d['name'] for d in data])) return jsonify(pager.to_dict(results_converter=convert))
def request_data(overlay={}): """ Decode a JSON-formatted POST body. """ data = request.json if data is None: raise BadRequest() data.update(overlay) return data
def parse_relation_facets(relation_obj, full_facet, facet, q): """ Parse a facet related to an entity and return a modified query. """ if facet == 'project': facet_obj = aliased(Project) q = q.join(facet_obj, relation_obj.project) return apply_facet_obj(q, facet_obj) elif facet == 'schema': facet_obj = aliased(Schema) if not arg_bool('facet_%s_hidden' % full_facet, default=False): q = q.filter(facet_obj.hidden == False) q = q.join(facet_obj, relation_obj.schema) return apply_facet_obj(q, facet_obj) elif facet.startswith('properties.'): return apply_property_facet(q, facet, Property, relation_obj) elif facet.startswith('source.'): _, subfacet = facet.split('.', 1) ent_obj = aliased(Entity) q = q.join(ent_obj, relation_obj.source) return parse_entity_facets(ent_obj, full_facet, subfacet, q) elif facet.startswith('target.'): _, subfacet = facet.split('.', 1) ent_obj = aliased(Entity) q = q.join(ent_obj, relation_obj.target) return parse_entity_facets(ent_obj, full_facet, subfacet, q) else: raise BadRequest("Unknown facet: %s" % facet)
def parse_entity_facets(entity_obj, full_facet, facet, q): """ Parse a facet related to a relation object and return a modified query. """ # TODO: Status facet. # TODO: Author facet. if facet == 'project': facet_obj = aliased(Project) q = q.join(facet_obj, entity_obj.project) return apply_facet_obj(q, facet_obj) elif facet == 'schema': facet_obj = aliased(Schema) if not arg_bool('facet_%s_hidden' % full_facet, default=False): q = q.filter(facet_obj.hidden == False) q = q.join(facet_obj, entity_obj.schemata) return apply_facet_obj(q, facet_obj) elif facet.startswith('properties.'): return apply_property_facet(q, facet, Property, entity_obj) elif facet.startswith('inbound.'): _, subfacet = facet.split('.', 1) rel_obj = aliased(Relation) q = q.join(rel_obj, entity_obj.inbound) return parse_relation_facets(rel_obj, full_facet, subfacet, q) elif facet.startswith('outbound.'): _, subfacet = facet.split('.', 1) rel_obj = aliased(Relation) q = q.join(rel_obj, entity_obj.outbound) return parse_relation_facets(rel_obj, full_facet, subfacet, q) else: raise BadRequest("Unknown facet: %s" % facet)
def single_arg(name, default=None): vals = [v for v in request.args.getlist(name) if v.strip()] if len(vals) == 0: return default elif len(vals) > 1: raise BadRequest('Too many values given for: %s' % name) else: return vals[0]
def twitter_login(): if not app.config.get('TWITTER_API_KEY'): return provider_not_enabled('twitter') callback = url_for('sessions_api.twitter_authorized') session.clear() if not request.args.get('next_url'): raise BadRequest("No 'next_url' is specified.") session['next_url'] = request.args.get('next_url') return twitter.authorize(callback=callback)
def github_login(): if not app.config.get('GITHUB_CLIENT_ID'): return provider_not_enabled('github') callback = url_for('sessions_api.github_authorized') session.clear() if not request.args.get('next_url'): raise BadRequest("No 'next_url' is specified.") session['next_url'] = request.args.get('next_url') return github.authorize(callback=callback)
def facebook_login(): if not app.config.get('FACEBOOK_APP_ID'): return provider_not_enabled('facebook') callback = url_for('sessions_api.facebook_authorized') session.clear() if not request.args.get('next_url'): raise BadRequest("No 'next_url' is specified.") session['next_url'] = request.args.get('next_url') return facebook.authorize(callback=callback)
def reconcile(slug): """ Reconciliation API, emulates Google Refine API. See: http://code.google.com/p/google-refine/wiki/ReconciliationServiceApi """ project = object_or_404(Project.by_slug(slug)) authz.require(authz.project_read(project)) # TODO: Add proper support for types and namespacing. data = request.args.copy() data.update(request.form.copy()) if 'query' in data: # single q = data.get('query') if q.startswith('{'): try: q = json.loads(q) except ValueError: raise BadRequest() else: q = data return jsonify(reconcile_op(project, q)) elif 'queries' in data: # multiple requests in one query qs = data.get('queries') try: qs = json.loads(qs) except ValueError: raise BadRequest() queries = {} for k, q in qs.items(): queries[k] = reconcile_op(project, q) return jsonify(queries) else: return reconcile_index(project)
def apply_property_facet(q, facet, cls, parent_obj): """ Property facets are complicated because we don't want to facet over the whole property object, but merely it's value - which can be in one of any number of fields. """ if '.' not in facet: raise BadRequest("Invalud facet: %s" % facet) _, name = facet.split('.', 1) facet_obj = aliased(cls) q = q.join(facet_obj, parent_obj.properties) q = q.filter(facet_obj.active == True) q = q.filter(facet_obj.name == name) columns = (facet_obj.value_string, facet_obj.value_integer, facet_obj.value_float, facet_obj.value_datetime, facet_obj.value_boolean) q = q.add_columns(*columns) return q.group_by(*columns)
def query(slug): project = object_or_404(Project.by_slug(slug)) authz.require(authz.project_read(project)) if request.method == 'POST': query = request.json else: try: query = json.loads(request.args.get('query', 'null')) assert query is not None except (TypeError, ValueError, AssertionError): raise BadRequest('Invalid data submitted') eq = run_query(project, query) res = { 'status': 'ok', 'query': eq.node, 'results': eq.run(), 'total': eq.count() } return jsonify(res)
def save(data, file_data, file=None): """ Save or update a file. """ if file_data is None: raise BadRequest("No file given!") data.update({ 'file_name': file_data.filename, 'mime_type': file_data.mimetype }) sane = validate(data, file) if file is None: file = File() file.project = sane.get('project') file.author = sane.get('author') db.session.add(file) file.file_name = sane.get('file_name') file.mime_type = sane.get('mime_type') file.data = file_data.read() db.session.flush() return file
def suggest(): if 'q' not in request.args or not len(request.args.get('q').strip()): raise BadRequest("Missing the query ('q' parameter).") q = db.session.query(Property) q = q.join(Entity) q = q.filter(Entity.project_id.in_(authz.permissions().get('reader'))) q = q.filter(Property.name == 'name') q = q.filter(Property.active == True) # noqa q = q.filter(Property.entity_id != None) # noqa q = q.filter(Property.value_string.ilike(request.args.get('q') + '%')) if 'project' in request.args: q = q.join(Project) q = q.filter(Project.slug == request.args.get('project')) if 'exclude' in request.args: ents = request.args.getlist('exclude') q = q.filter(not_(Property.entity_id.in_(ents))) q = q.distinct() pager = Pager(q) data = [] def convert(props): for prop in props: data.append({ 'properties': { 'name': prop.to_dict_index(), }, 'id': prop.entity_id, 'api_url': url_for('entities_api.view', id=prop.entity_id) }) return data validate_cache(keys='#'.join([d['name'] for d in data])) return jsonify(pager.to_dict(results_converter=convert))
def format(self): fmt = request.args.get('format', 'json').lower().strip() if fmt not in ['json', 'gexf']: raise BadRequest('Invalid format name: %s' % fmt) return fmt