def category(label): ''' list of entries for a label :param label: the type of data to list out :return: rendered list page ''' label = helpers.sanitize(label) if not graph.validate_label(label): labels = graph.get_labels() return render_template(request.url, '404.html', labels=labels) filtered = None try: item1 = helpers.sanitize(request.values['i']) except KeyError: data = graph.get_all(label) else: try: item2 = helpers.sanitize(request.values['j']) operator = helpers.sanitize(request.values['op']) if operator not in ['and', 'not']: raise BadRequestKeyError except (KeyError, BadRequestKeyError): data = graph.get_filtered(label, item1) filtered = { 'item1': graph.get_node(item1)['nodes'][0], } else: data = graph.get_filtered(label, item1, item2, operator) filtered = { 'operator': operator, 'item1': graph.get_node(item1)['nodes'][0], 'item2': graph.get_node(item2)['nodes'][0] } items = data['nodes'] template = 'list.html' title = 'List of %s' % helpers.capitalize_filter(helpers.pluralize(label)) grimoires = [] if label in entities: template = 'entity-list.html' grimoires = graph.get_all('grimoire', with_connection_label=label)['nodes'] if len(grimoires) < 2: grimoires = None return render_template(request.url, template, items=items, title=title, label=label, grimoires=grimoires, filtered=filtered)
def run_comparison(): ''' redirect a comparison request :return: redirect to comparison page ''' try: item1 = helpers.sanitize(request.values['grim1']) item2 = helpers.sanitize(request.values['grim2']) except KeyError: return redirect('/compare') return redirect('/compare/%s/%s' % (item1, item2))
def spell(): ''' custom page for spells :return: rendered spell page template ''' sort = 'outcome' try: sort = helpers.sanitize(request.values['sort'], allow_spaces=True) except BadRequestKeyError: pass if sort == 'outcome': data = graph.get_spells_by_outcome() elif sort == 'grimoire': data = graph.get_spells_by_grimoire() else: return redirect('/spell') spells = { k['props']['identifier']: v for k, v in zip(data['nodes'], data['lists']) } return render_template(request.url, 'spells.html', spells=spells, sort=sort, title='List of Spells')
def api_connected_nodes(uid_raw, label): ''' get a list of items for a given label for a given node''' uid = helpers.sanitize(uid_raw) if not graph.validate_label(label): error = 'invalid label "%s"' % label logging.warn(error) return error, 404 parser = reqparse.RequestParser() parser.add_argument('limit', type=int, default=25) parser.add_argument('offset', type=int, default=0) parser.add_argument('random', case_sensitive=False, default=False) args = parser.parse_args() if args.limit < 1 or args.offset < 0: return 'Invalid limit or offset', 403 if args.limit > 100: return 'Max limit is 100', 403 if args.random: args.random = args.random not in falsey data = api_graph.get_connected_nodes(uid, label, **args) try: node = data['nodes'] except IndexError: return 'Invalid uid %s' % uid_raw, 404 return api_response(node)
def search(): ''' look up a term ''' try: term = helpers.sanitize(request.values['term'], allow_spaces=True) except BadRequestKeyError: return redirect('/') if not term: return redirect('/') data = graph.search(term) # if there's only one result, redirect if len(data['nodes']) == 1: item = data['nodes'][0] return redirect('/%s/%s' % (item['label'], item['props']['uid'])) template_data = {'results': data['nodes'], 'term': term} return render_template(request.url, 'search.html', **template_data)
def api_label(label): ''' get a list of items for a given label ''' if not graph.validate_label(label): error = 'invalid label "%s"' % label logging.warn(error) return error, 404 parser = reqparse.RequestParser() parser.add_argument('limit', type=int, default=25) parser.add_argument('offset', type=int, default=0) parser.add_argument('sort', type=str, case_sensitive=False) parser.add_argument('sort_direction', choices=['asc', 'desc'], case_sensitive=False, default='asc') parser.add_argument('uids_only', case_sensitive=False, default=False) parser.add_argument('random', case_sensitive=False, default=False) args = parser.parse_args() if args.uids_only: args.uids_only = args.uids_only not in falsey if args.random: args.random = args.random not in falsey if args.sort: args.sort = helpers.sanitize(args.sort) if args.limit < 1 or args.offset < 0: return 'Invalid limit or offset', 403 if args.limit > 100: return 'Max limit is 100', 403 # --- load data data = api_graph.get_label(label, **args) if args.uids_only: data = data['lists'] else: data = data['nodes'] return api_response(data)
def api_node(uid_raw): ''' get a particular node by uid ''' uid = helpers.sanitize(uid_raw) parser = reqparse.RequestParser() parser.add_argument('relationships', case_sensitive=False, default=False) args = parser.parse_args() if args.relationships: args.relationships = args.relationships not in falsey data = api_graph.get_node(uid, args.relationships) try: node = data['nodes'][0] except IndexError: return 'Invalid uid %s' % uid_raw, 404 if args.relationships: node['relationships'] = data['rels'] return api_response(node)
def item(label, uid): ''' generic page for an item :param label: the desired neo4j label :param uid: the human-readable uid of the node :return: customized data for this label rendered item page template ''' # load and validate url data label = helpers.sanitize(label) if not graph.validate_label(label): logging.error('Invalid label %s', label) return render_template(request.url, '404.html', labels=graph.get_labels()) try: data = helpers.get_node(uid) except NameError: items = graph.get_all(label) return render_template(request.url, '404.html', items=items['nodes'], search=graph.search(uid)['nodes'], label=label) node = data['node'] rels = data['rels'] # ----- page header/metadata title = node['props']['identifier'] # ----- formatted data switch = { 'parent:book': grimoire_item, 'parent:entity': entity_item, 'art': art_item, 'language': language_item, 'edition': edition_item, 'publisher': publisher_item, 'editor': editor_item, 'default': generic_item, 'spell': spell_item, 'talisman': spell_item, 'parent:ingredient': ingredient_item, 'outcome': outcome_item } key = node['parent_label'] if node['parent_label'] in switch else \ (label if label in switch else 'default') item_data = switch[key](node, rels) item_data['rels'] = helpers.combine_rels(item_data['rels']) # ----- sidebar sidebar = [] related = graph.related(uid, label) if related['nodes']: sidebar = [{'title': 'Similar %s' % \ helpers.capitalize_filter(helpers.pluralize(label)), 'data': related['nodes']}] sidebar += get_others(data['rels'], node) if not item_data['content'] and not item_data['excerpts']: item_data['content'] = 'The %s "%s."' % \ (helpers.format_filter(label), helpers.unthe(title)) max_main_length = max([len(i['data']) for i in item_data['main']] + [0]) default_collapse = len(item_data['main']) > 2 or max_main_length > 30 return render_template(request.url, 'item.html', data=item_data, title=title, label=label, sidebar=sidebar, default_collapse=default_collapse)