def download_rack_cables(request, handle_id, file_format=None): nh = get_object_or_404(NodeHandle, pk=handle_id) node = nh.get_node() location = '_'.join([loc['name'] for loc in node.get_location_path()['location_path']]) header = [ 'EquipmentA', 'PortA', 'Cable', 'CableType', 'EquipmentB', 'PortB', ] q = """ MATCH (r:Rack {handle_id: $handle_id})<-[:Located_in]-(n:Node)-[:Has]->(p:Port)<-[:Connected_to]-(c:Cable)-[:Connected_to]->(p2:Port)<-[:Has]-(n2:Node) WHERE n <> n2 and p <> p2 RETURN n.name AS EquipmentA, p.name AS PortA, c.name AS Cable, c.cable_type AS CableType, n2.name AS EquipmentB, p2.name AS PortB """ cables = nc.query_to_list(nc.graphdb.manager, q, handle_id=nh.handle_id) file_name = 'rack-cables_{}_{}_{}.{}'.format(location, nh.node_name, nh.handle_id, file_format) if cables and file_format == 'xls': sheet_name = '{} - {}'.format(location, nh.node_name) return helpers.dicts_to_xls_response(cables, header, file_name, sheet_name=sheet_name) elif cables and file_format == 'csv': return helpers.dicts_to_csv_response(cables, header, file_name) else: raise Http404
def download_unique_ids(request, file_format=None): header = ["ID", "Reserved", "Reserve message", "Site", "Reserver", "Created"] table = None id_list = get_id_list(request.GET or None) def get_site(uid): return uid.site.node_name if uid.site else "" def create_dict(uid): return { 'ID': uid.unique_id, 'Reserve message': uid.reserve_message, 'Reserved': uid.reserved, 'Site': get_site(uid), 'Reserver': str(uid.reserver), 'Created': uid.created } table = [create_dict(uid) for uid in id_list] # using values is faster, a lot, but no nice header :( and no username # table = id_list.values() if table and file_format == 'xls': return helpers.dicts_to_xls_response(table, header) elif table and file_format == 'csv': return helpers.dicts_to_csv_response(table, header) else: raise Http404
def search(request, value='', form=None): """ Search through nodes either from a POSTed search query or through an URL like /slug/key/value/ or /slug/value/. """ result = [] posted = False if request.POST: value = request.POST.get('q', '') posted = True if value: query = u'(?i).*{}.*'.format(re_escape(value)) # nodes = nc.search_nodes_by_value(nc.graphdb.manager, query) # TODO: when search uses the above go back to that q = """ match (n:Node) where any(prop in keys(n) where n[prop] =~ {search}) return n """ nodes = nc.query_to_list(nc.graphdb.manager, q, search=query) if form == 'csv': return helpers.dicts_to_csv_response([n['n'] for n in nodes]) elif form == 'xls': return helpers.dicts_to_xls_response([n['n'] for n in nodes]) for node in nodes: nh = get_object_or_404(NodeHandle, pk=node['n']['handle_id']) item = {'node': node['n'], 'nh': nh} result.append(item) if len(result) == 1: return redirect(result[0]['nh'].get_absolute_url()) return render(request, 'noclook/search_result.html', {'value': value, 'result': result, 'posted': posted})
def find_all(request, slug=None, key=None, value=None, form=None): """ Search through nodes either from a POSTed search query or through an URL like /slug/key/value/, /slug/value/ /key/value/, /value/ or /key/. """ label = None node_type = None if request.POST: value = request.POST.get('q', '') # search for '' if blank if slug: try: node_type = get_object_or_404(NodeType, slug=slug) label = node_type.get_label() except Http404: return render(request, 'noclook/search_result.html', {'node_type': slug, 'key': key, 'value': value, 'result': None, 'node_meta_type': None}) if value: nodes = nc.search_nodes_by_value(nc.graphdb.manager, value, key, label) else: nodes = nc.get_nodes_by_type(nc.graphdb.manager, label) if form == 'csv': return helpers.dicts_to_csv_response(list(nodes)) elif form == 'xls': return helpers.dicts_to_xls_response(list(nodes)) result = [] for node in nodes: nh = get_object_or_404(NodeHandle, pk=node['handle_id']) item = {'node': node, 'nh': nh} result.append(item) return render(request, 'noclook/search_result.html', {'node_type': node_type, 'key': key, 'value': value, 'result': result})
def json_table_to_file(request): if request.POST: file_format = request.POST.get('format', None) data = request.POST.get('data', None) header = request.POST.get('header', None) table = json.loads(data) header = json.loads(header) if table and file_format == 'csv': return helpers.dicts_to_csv_response(table, header) elif table and file_format == 'xls': return helpers.dicts_to_xls_response(table, header) raise Http404
def search(request, value='', form=None, permission_filter=False): """ Search through nodes either from a POSTed search query or through an URL like /slug/key/value/ or /slug/value/. """ result = [] posted = False if request.POST: value = request.POST.get('q', '') posted = True if value: query = u'(?i).*{}.*'.format(re_escape(value)) permission_clause = '' if permission_filter: readable_ids = sriutils.get_ids_user_canread(request.user) readable_ids = [str(x) for x in readable_ids] # string conversion ids = ', '.join(readable_ids) permission_clause = 'AND n.handle_id in [{ids}]'.format(ids=ids) # nodes = nc.search_nodes_by_value(nc.graphdb.manager, query) # TODO: when search uses the above go back to that q = """ MATCH (n:Node) WHERE any(prop in keys(n) WHERE n[prop] =~ {{search}}) {} WITH n, [prop IN keys(n) WHERE n[prop] =~ {{search}}] AS match_props RETURN n, reduce(s = "", prop IN match_props | s + n[prop] + ' [..] ') AS match_txt """.format(permission_clause) nodes = nc.query_to_list(nc.graphdb.manager, q, search=query) if form == 'csv': return helpers.dicts_to_csv_response([n['n'] for n in nodes]) elif form == 'xls': return helpers.dicts_to_xls_response([n['n'] for n in nodes]) elif form == 'json': return helpers.dicts_to_json_response([(n['n'], n['match_txt']) for n in nodes]) for node in nodes: nh = get_object_or_404(NodeHandle, pk=node['n']['handle_id']) item = {'node': node['n'], 'nh': nh} result.append(item) if len(result) == 1: return redirect(result[0]['nh'].get_absolute_url()) return render(request, 'noclook/search_result.html', { 'value': value, 'result': result, 'posted': posted })
def test_dicts_to_xls_response(self): resp = helpers.dicts_to_xls_response(self.dicts) self.assertEqual(resp['content-type'], 'application/excel') self.assertEqual(resp['Content-Disposition'], 'attachment; filename=result.xls;')