def test_getProfileByUsername(self): request = HttpRequest() request.user = User.objects.create_user('testuser', '*****@*****.**', 'testuserpassword') user_util.getProfile(request, False) self.assertEqual(str(user_util.getProfileByUsername('testuser')), 'Profile for testuser') self.assertEqual(user_util.getProfileByUsername('nonexistentuser'), None)
def updateProfile(request): profile = getProfile(request,allowDefault=False) if profile: profile.advancedUI = request.POST.get('advancedUI','off') == 'on' profile.save() nextPage = request.POST.get('nextPage', reverse('browser')) return HttpResponseRedirect(nextPage)
def updateProfile(request): profile = getProfile(request, allowDefault=False) if profile: profile.advancedUI = request.POST.get('advancedUI', 'off') == 'on' profile.save() nextPage = request.POST.get('nextPage', reverse('browser')) return HttpResponseRedirect(nextPage)
def header(request): "View for the header frame of the browser UI" context = {} context['user'] = request.user context['profile'] = getProfile(request) context['documentation_url'] = settings.DOCUMENTATION_URL context['login_url'] = settings.LOGIN_URL return render_to_response("browserHeader.html", context)
def composer(request): profile = getProfile(request) context = { 'queryString' : request.GET.urlencode().replace('+','%20'), 'showTarget' : request.GET.get('showTarget',''), 'user' : request.user, 'profile' : profile, 'showMyGraphs' : int( profile.user.username != 'default' ), 'searchEnabled' : int( os.access(settings.INDEX_FILE, os.R_OK) ), 'refreshInterval': settings.AUTO_REFRESH_INTERVAL, 'debug' : settings.DEBUG, 'jsdebug' : settings.DEBUG, } return render_to_response("composer.html",context)
def composer(request): profile = getProfile(request) context = { 'queryString': request.GET.urlencode().replace('+', '%20'), 'showTarget': request.GET.get('showTarget', ''), 'user': request.user, 'profile': profile, 'showMyGraphs': int(profile.user.username != 'default'), 'searchEnabled': int(os.access(settings.INDEX_FILE, os.R_OK)), 'refreshInterval': settings.AUTO_REFRESH_INTERVAL, 'debug': settings.DEBUG, 'jsdebug': settings.DEBUG, } return render_to_response("composer.html", context)
def mygraph(request): profile = getProfile(request, allowDefault=False) if not profile: return HttpResponse("You are not logged in!") action = request.GET['action'] graphName = request.GET['graphName'] if not graphName: return HttpResponse("You must type in a graph name.") if action == 'save': url = request.GET['url'] try: existingGraph = profile.mygraph_set.get(name=graphName) existingGraph.url = url existingGraph.save() except ObjectDoesNotExist: try: newGraph = MyGraph(profile=profile, name=graphName, url=url) newGraph.save() except: log.exception( "Failed to create new MyGraph in /composer/mygraph/, graphName=%s" % graphName) return HttpResponse("Failed to save graph %s" % graphName) return HttpResponse("SAVED") elif action == 'delete': try: existingGraph = profile.mygraph_set.get(name=graphName) existingGraph.delete() except ObjectDoesNotExist: return HttpResponse("No such graph '%s'" % graphName) return HttpResponse("DELETED") else: return HttpResponse("Invalid operation '%s'" % action)
def mygraph(request): profile = getProfile(request, allowDefault=False) if not profile: return HttpResponse( "You are not logged in!" ) action = request.GET['action'] graphName = request.GET['graphName'] if not graphName: return HttpResponse("You must type in a graph name.") if action == 'save': url = request.GET['url'] try: existingGraph = profile.mygraph_set.get(name=graphName) existingGraph.url = url existingGraph.save() except ObjectDoesNotExist: try: newGraph = MyGraph(profile=profile,name=graphName,url=url) newGraph.save() except: log.exception("Failed to create new MyGraph in /composer/mygraph/, graphName=%s" % graphName) return HttpResponse("Failed to save graph %s" % graphName) return HttpResponse("SAVED") elif action == 'delete': try: existingGraph = profile.mygraph_set.get(name=graphName) existingGraph.delete() except ObjectDoesNotExist: return HttpResponse("No such graph '%s'" % graphName) return HttpResponse("DELETED") else: return HttpResponse("Invalid operation '%s'" % action)
def test_getProfileByUsername(self): request = HttpRequest() request.user = User.objects.create_user('testuser', '*****@*****.**', 'testuserpassword') user_util.getProfile(request, False) self.assertEqual( str(user_util.getProfileByUsername('testuser')), 'Profile for testuser' ) self.assertEqual( user_util.getProfileByUsername('nonexistentuser'), None )
def myGraphLookup(request): "View for My Graphs navigation" profile = getProfile(request, allowDefault=False) assert profile nodes = [] leafNode = { 'allowChildren': 0, 'expandable': 0, 'leaf': 1, } branchNode = { 'allowChildren': 1, 'expandable': 1, 'leaf': 0, } try: path = request.GET.get('path', u'') if path: if path.endswith('.'): userpath_prefix = path else: userpath_prefix = path + '.' else: userpath_prefix = u"" matches = [ graph for graph in profile.mygraph_set.all().order_by('name') if graph.name.startswith(userpath_prefix) ] log.info( "myGraphLookup: username=%s, path=%s, userpath_prefix=%s, %ld graph to process" % (profile.user.username, path, userpath_prefix, len(matches))) branch_inserted = set() leaf_inserted = set() for graph in matches: #Now let's add the matching graph isBranch = False dotPos = graph.name.find('.', len(userpath_prefix)) if dotPos >= 0: isBranch = True name = graph.name[len(userpath_prefix):dotPos] if name in branch_inserted: continue branch_inserted.add(name) else: name = graph.name[len(userpath_prefix):] if name in leaf_inserted: continue leaf_inserted.add(name) node = {'text': escape(name)} if isBranch: node.update({'id': userpath_prefix + name + '.'}) node.update(branchNode) else: m = md5() m.update(name.encode('utf-8')) # Sanitize target urlEscaped = str(graph.url) graphUrl = urlparse(urlEscaped) graphUrlParams = {} graphUrlParams['target'] = [] for param in parse_qsl(graphUrl.query): if param[0] != 'target': graphUrlParams[param[0]] = param[1] else: graphUrlParams[param[0]].append(escape(param[1])) urlEscaped = graphUrl._replace( query=urlencode(graphUrlParams, True)).geturl() node.update({ 'id': str(userpath_prefix + m.hexdigest()), 'graphUrl': urlEscaped }) node.update(leafNode) nodes.append(node) except Exception: log.exception( "browser.views.myGraphLookup(): could not complete request.") if not nodes: no_graphs = {'text': "No saved graphs", 'id': 'no-click'} no_graphs.update(leafNode) nodes.append(no_graphs) return json_response(nodes, request)
def editProfile(request): if not isAuthenticated(request.user): return HttpResponseRedirect(reverse('browser')) context = {'profile': getProfile(request)} return render_to_response("editProfile.html", context)
def find_view(request): "View for finding metrics matching a given pattern" queryParams = request.GET.copy() queryParams.update(request.POST) format = queryParams.get('format', 'treejson') leaves_only = queryParamAsInt(queryParams, 'leavesOnly', 0) local_only = queryParamAsInt(queryParams, 'local', 0) wildcards = queryParamAsInt(queryParams, 'wildcards', 0) tzinfo = pytz.timezone(settings.TIME_ZONE) if 'tz' in queryParams: try: value = queryParams['tz'] tzinfo = pytz.timezone(value) except pytz.UnknownTimeZoneError: pass except Exception as e: raise InputParameterError( 'Invalid value {value} for param tz: {err}'.format( value=repr(value), err=str(e))) if 'now' in queryParams: try: value = queryParams['now'] now = parseATTime(value, tzinfo) except Exception as e: raise InputParameterError( 'Invalid value {value} for param now: {err}'.format( value=repr(value), err=str(e))) else: now = datetime.now(tzinfo) if 'from' in queryParams and str(queryParams['from']) != '-1': try: value = queryParams['from'] fromTime = int(epoch(parseATTime(value, tzinfo, now))) except Exception as e: raise InputParameterError( 'Invalid value {value} for param from: {err}'.format( value=repr(value), err=str(e))) else: fromTime = -1 if 'until' in queryParams and str(queryParams['until']) != '-1': try: value = queryParams['until'] untilTime = int(epoch(parseATTime(value, tzinfo, now))) except Exception as e: raise InputParameterError( 'Invalid value {value} for param until: {err}'.format( value=repr(value), err=str(e))) else: untilTime = -1 nodePosition = queryParamAsInt(queryParams, 'position', -1) jsonp = queryParams.get('jsonp', False) forward_headers = extractForwardHeaders(request) if fromTime == -1: fromTime = None if untilTime == -1: untilTime = None automatic_variants = queryParamAsInt(queryParams, 'automatic_variants', 0) try: query = str(queryParams['query']) except KeyError: raise InputParameterError('Missing required parameter \'query\'') if query == '': raise InputParameterError('Required parameter \'query\' is empty') if '.' in query: base_path = query.rsplit('.', 1)[0] + '.' else: base_path = '' if format == 'completer': query = query.replace('..', '*.') if not query.endswith('*'): query += '*' if automatic_variants: query_parts = query.split('.') for i, part in enumerate(query_parts): if ',' in part and '{' not in part: query_parts[i] = '{%s}' % part query = '.'.join(query_parts) try: matches = list( STORE.find( query, fromTime, untilTime, local=local_only, headers=forward_headers, leaves_only=leaves_only, )) except Exception: log.exception() raise log.info('find_view query=%s local_only=%s matches=%d' % (query, local_only, len(matches))) matches.sort(key=lambda node: node.name) log.info( "received remote find request: pattern=%s from=%s until=%s local_only=%s format=%s matches=%d" % (query, fromTime, untilTime, local_only, format, len(matches))) if format == 'treejson': profile = getProfile(request) content = tree_json(matches, base_path, wildcards=profile.advancedUI or wildcards) response = json_response_for(request, content, jsonp=jsonp) elif format == 'nodelist': content = nodes_by_position(matches, nodePosition) response = json_response_for(request, content, jsonp=jsonp) elif format == 'pickle': content = pickle_nodes(matches) response = HttpResponse(content, content_type='application/pickle') elif format == 'msgpack': content = msgpack_nodes(matches) response = HttpResponse(content, content_type='application/x-msgpack') elif format == 'json': content = json_nodes(matches) response = json_response_for(request, content, jsonp=jsonp) elif format == 'completer': results = [] for node in matches: node_info = dict(path=node.path, name=node.name, is_leaf=str(int(node.is_leaf))) if not node.is_leaf: node_info['path'] += '.' results.append(node_info) if len(results) > 1 and wildcards: wildcardNode = {'name': '*'} results.append(wildcardNode) response = json_response_for(request, {'metrics': results}, jsonp=jsonp) else: return HttpResponseBadRequest( content="Invalid value for 'format' parameter", content_type='text/plain') response['Pragma'] = 'no-cache' response['Cache-Control'] = 'no-cache' return response
def editProfile(request): if not isAuthenticated(request.user): return HttpResponseRedirect(reverse('browser')) context = { 'profile' : getProfile(request) } return render_to_response("editProfile.html",context)
def myGraphLookup(request): "View for My Graphs navigation" profile = getProfile(request,allowDefault=False) assert profile nodes = [] leafNode = { 'allowChildren' : 0, 'expandable' : 0, 'leaf' : 1, } branchNode = { 'allowChildren' : 1, 'expandable' : 1, 'leaf' : 0, } try: path = request.GET.get('path', u'') if path: if path.endswith('.'): userpath_prefix = path else: userpath_prefix = path + '.' else: userpath_prefix = u"" matches = [ graph for graph in profile.mygraph_set.all().order_by('name') if graph.name.startswith(userpath_prefix) ] log.info( "myGraphLookup: username=%s, path=%s, userpath_prefix=%s, %ld graph to process" % (profile.user.username, path, userpath_prefix, len(matches)) ) branch_inserted = set() leaf_inserted = set() for graph in matches: #Now let's add the matching graph isBranch = False dotPos = graph.name.find( '.', len(userpath_prefix) ) if dotPos >= 0: isBranch = True name = graph.name[ len(userpath_prefix) : dotPos ] if name in branch_inserted: continue branch_inserted.add(name) else: name = graph.name[ len(userpath_prefix): ] if name in leaf_inserted: continue leaf_inserted.add(name) node = {'text': escape(name)} if isBranch: node.update({'id': userpath_prefix + name + '.'}) node.update(branchNode) else: m = md5() m.update(name.encode('utf-8')) # Sanitize target urlEscaped = str(graph.url) graphUrl = urlparse(urlEscaped) graphUrlParams = {} graphUrlParams['target'] = [] for param in parse_qsl(graphUrl.query): if param[0] != 'target': graphUrlParams[param[0]] = param[1] else: graphUrlParams[param[0]].append(escape(param[1])) urlEscaped = graphUrl._replace(query=urlencode(graphUrlParams, True)).geturl() node.update( { 'id' : str(userpath_prefix + m.hexdigest()), 'graphUrl' : urlEscaped } ) node.update(leafNode) nodes.append(node) except: log.exception("browser.views.myGraphLookup(): could not complete request.") if not nodes: no_graphs = { 'text' : "No saved graphs", 'id' : 'no-click' } no_graphs.update(leafNode) nodes.append(no_graphs) return json_response(nodes, request)
def find_view(request): "View for finding metrics matching a given pattern" queryParams = request.GET.copy() queryParams.update(request.POST) format = queryParams.get('format', 'treejson') leaves_only = int( queryParams.get('leavesOnly', 0) ) local_only = int( queryParams.get('local', 0) ) wildcards = int( queryParams.get('wildcards', 0) ) tzinfo = pytz.timezone(settings.TIME_ZONE) if 'tz' in queryParams: try: tzinfo = pytz.timezone(queryParams['tz']) except pytz.UnknownTimeZoneError: pass if 'now' in queryParams: now = parseATTime(queryParams['now'], tzinfo) else: now = datetime.now(tzinfo) if 'from' in queryParams and str(queryParams['from']) != '-1': fromTime = int(epoch(parseATTime(queryParams['from'], tzinfo, now))) else: fromTime = -1 if 'until' in queryParams and str(queryParams['from']) != '-1': untilTime = int(epoch(parseATTime(queryParams['until'], tzinfo, now))) else: untilTime = -1 nodePosition = int( queryParams.get('position', -1) ) jsonp = queryParams.get('jsonp', False) forward_headers = extractForwardHeaders(request) if fromTime == -1: fromTime = None if untilTime == -1: untilTime = None automatic_variants = int( queryParams.get('automatic_variants', 0) ) try: query = str(queryParams['query']) except KeyError: return HttpResponseBadRequest(content="Missing required parameter 'query'", content_type='text/plain') if query == '': return HttpResponseBadRequest(content="Required parameter 'query' is empty", content_type='text/plain') if '.' in query: base_path = query.rsplit('.', 1)[0] + '.' else: base_path = '' if format == 'completer': query = query.replace('..', '*.') if not query.endswith('*'): query += '*' if automatic_variants: query_parts = query.split('.') for i,part in enumerate(query_parts): if ',' in part and '{' not in part: query_parts[i] = '{%s}' % part query = '.'.join(query_parts) try: matches = list(STORE.find( query, fromTime, untilTime, local=local_only, headers=forward_headers, leaves_only=leaves_only, )) except Exception: log.exception() raise log.info('find_view query=%s local_only=%s matches=%d' % (query, local_only, len(matches))) matches.sort(key=lambda node: node.name) log.info("received remote find request: pattern=%s from=%s until=%s local_only=%s format=%s matches=%d" % (query, fromTime, untilTime, local_only, format, len(matches))) if format == 'treejson': profile = getProfile(request) content = tree_json(matches, base_path, wildcards=profile.advancedUI or wildcards) response = json_response_for(request, content, jsonp=jsonp) elif format == 'nodelist': content = nodes_by_position(matches, nodePosition) response = json_response_for(request, content, jsonp=jsonp) elif format == 'pickle': content = pickle_nodes(matches) response = HttpResponse(content, content_type='application/pickle') elif format == 'msgpack': content = msgpack_nodes(matches) response = HttpResponse(content, content_type='application/x-msgpack') elif format == 'json': content = json_nodes(matches) response = json_response_for(request, content, jsonp=jsonp) elif format == 'completer': results = [] for node in matches: node_info = dict(path=node.path, name=node.name, is_leaf=str(int(node.is_leaf))) if not node.is_leaf: node_info['path'] += '.' results.append(node_info) if len(results) > 1 and wildcards: wildcardNode = {'name' : '*'} results.append(wildcardNode) response = json_response_for(request, { 'metrics' : results }, jsonp=jsonp) else: return HttpResponseBadRequest( content="Invalid value for 'format' parameter", content_type='text/plain') response['Pragma'] = 'no-cache' response['Cache-Control'] = 'no-cache' return response
def find_view(request): "View for finding metrics matching a given pattern" profile = getProfile(request) queryParams = request.GET.copy() queryParams.update(request.POST) format = queryParams.get('format', 'treejson') local_only = int(queryParams.get('local', 0)) wildcards = int(queryParams.get('wildcards', 0)) fromTime = int(queryParams.get('from', -1)) untilTime = int(queryParams.get('until', -1)) nodePosition = int(queryParams.get('position', -1)) jsonp = queryParams.get('jsonp', False) if fromTime == -1: fromTime = None if untilTime == -1: untilTime = None automatic_variants = int(queryParams.get('automatic_variants', 0)) try: query = str(queryParams['query']) except: return HttpResponseBadRequest( content="Missing required parameter 'query'", content_type='text/plain') if '.' in query: base_path = query.rsplit('.', 1)[0] + '.' else: base_path = '' if format == 'completer': query = query.replace('..', '*.') if not query.endswith('*'): query += '*' if automatic_variants: query_parts = query.split('.') for i, part in enumerate(query_parts): if ',' in part and '{' not in part: query_parts[i] = '{%s}' % part query = '.'.join(query_parts) try: matches = list(STORE.find(query, fromTime, untilTime, local=local_only)) except: log.exception() raise log.info('find_view query=%s local_only=%s matches=%d' % (query, local_only, len(matches))) matches.sort(key=lambda node: node.name) log.info( "received remote find request: pattern=%s from=%s until=%s local_only=%s format=%s matches=%d" % (query, fromTime, untilTime, local_only, format, len(matches))) if format == 'treejson': content = tree_json(matches, base_path, wildcards=profile.advancedUI or wildcards) response = json_response_for(request, content, jsonp=jsonp) elif format == 'nodelist': content = nodes_by_position(matches, nodePosition) response = json_response_for(request, content, jsonp=jsonp) elif format == 'pickle': content = pickle_nodes(matches) response = HttpResponse(content, content_type='application/pickle') elif format == 'completer': results = [] for node in matches: node_info = dict(path=node.path, name=node.name, is_leaf=str(int(node.is_leaf))) if not node.is_leaf: node_info['path'] += '.' results.append(node_info) if len(results) > 1 and wildcards: wildcardNode = {'name': '*'} results.append(wildcardNode) response = json_response_for(request, {'metrics': results}, jsonp=jsonp) else: return HttpResponseBadRequest( content="Invalid value for 'format' parameter", content_type='text/plain') response['Pragma'] = 'no-cache' response['Cache-Control'] = 'no-cache' return response