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 renderMyGraphView(request,username,graphName): profile = getProfileByUsername(username) if not profile: return errorPage("No such user '%s'" % username) try: graph = profile.mygraph_set.get(name=graphName) except ObjectDoesNotExist: return errorPage("User %s doesn't have a MyGraph named '%s'" % (username,graphName)) request_params = request.GET.copy() request_params.update(request.POST) if request_params: url_parts = urlsplit(graph.url) query_string = url_parts[3] if query_string: url_params = parse_qs(query_string) # Remove lists so that we can do an update() on the dict for param, value in url_params.items(): if isinstance(value, list) and param != 'target': url_params[param] = value[-1] url_params.update(request_params) # Handle 'target' being a list - we want duplicate &target params out of it url_param_pairs = [] for key,val in url_params.items(): if isinstance(val, list): for v in val: url_param_pairs.append( (key,v) ) else: url_param_pairs.append( (key,val) ) query_string = urlencode(url_param_pairs) url = urlunsplit(url_parts[:3] + (query_string,) + url_parts[4:]) else: url = graph.url return HttpResponseRedirect(url)
def userGraphLookup(request): "View for User Graphs navigation" user = request.GET.get('user') path = request.GET['path'] if user: username = user graphPath = path[len(username)+1:] elif '.' in path: username, graphPath = path.split('.', 1) else: username, graphPath = path, None nodes = [] branchNode = { 'allowChildren' : 1, 'expandable' : 1, 'leaf' : 0, } leafNode = { 'allowChildren' : 0, 'expandable' : 0, 'leaf' : 1, } try: if not username: profiles = Profile.objects.exclude(user__username='******').order_by('user__username') for profile in profiles: if profile.mygraph_set.count(): node = { 'text' : profile.user.username, 'id' : profile.user.username, } node.update(branchNode) nodes.append(node) else: profile = getProfileByUsername(username) assert profile, "No profile for username '%s'" % username if graphPath: prefix = graphPath.rstrip('.') + '.' else: prefix = '' matches = [ graph for graph in profile.mygraph_set.order_by('name') if graph.name.startswith(prefix) ] inserted = set() for graph in matches: relativePath = graph.name[ len(prefix): ] nodeName = relativePath.split('.')[0] if nodeName in inserted: continue inserted.add(nodeName) if '.' in relativePath: # branch node = { 'text' : escape(nodeName), 'id' : username + '.' + prefix + nodeName + '.', } node.update(branchNode) else: # leaf m = md5() m.update(nodeName.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 = { 'text' : escape(nodeName), 'id' : username + '.' + prefix + m.hexdigest(), 'graphUrl' : urlEscaped, } node.update(leafNode) nodes.append(node) except: log.exception("browser.views.userLookup(): could not complete request for %s" % username) if not nodes: no_graphs = { 'text' : "No saved graphs", 'id' : 'no-click' } no_graphs.update(leafNode) nodes.append(no_graphs) nodes.sort(key=lambda node: node['allowChildren'], reverse = True) return json_response(nodes, request)
def userGraphLookup(request): "View for User Graphs navigation" user = request.GET.get('user') path = request.GET['path'] if user: username = user graphPath = path[len(username) + 1:] elif '.' in path: username, graphPath = path.split('.', 1) else: username, graphPath = path, None nodes = [] branchNode = { 'allowChildren': 1, 'expandable': 1, 'leaf': 0, } leafNode = { 'allowChildren': 0, 'expandable': 0, 'leaf': 1, } try: if not username: profiles = Profile.objects.exclude( user__username='******').order_by('user__username') for profile in profiles: if profile.mygraph_set.count(): node = { 'text': profile.user.username, 'id': profile.user.username, } node.update(branchNode) nodes.append(node) else: profile = getProfileByUsername(username) assert profile, "No profile for username '%s'" % username if graphPath: prefix = graphPath.rstrip('.') + '.' else: prefix = '' matches = [ graph for graph in profile.mygraph_set.order_by('name') if graph.name.startswith(prefix) ] inserted = set() for graph in matches: relativePath = graph.name[len(prefix):] nodeName = relativePath.split('.')[0] if nodeName in inserted: continue inserted.add(nodeName) if '.' in relativePath: # branch node = { 'text': escape(nodeName), 'id': username + '.' + prefix + nodeName + '.', } node.update(branchNode) else: # leaf m = md5() m.update(nodeName.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 = { 'text': escape(nodeName), 'id': username + '.' + prefix + m.hexdigest(), 'graphUrl': urlEscaped, } node.update(leafNode) nodes.append(node) except Exception: log.exception( "browser.views.userLookup(): could not complete request for %s" % username) if not nodes: no_graphs = {'text': "No saved graphs", 'id': 'no-click'} no_graphs.update(leafNode) nodes.append(no_graphs) nodes.sort(key=lambda node: node['allowChildren'], reverse=True) return json_response(nodes, request)
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 )