def token(request, token): # We only need the redis manager here, but it's saner to get a whole # vumi_api and not worry about all the setup magic. api = vumi_api() token_data = api.token_manager.get(token) if not token_data: raise Http404 user_id = int(token_data['user_id']) redirect_to = token_data['redirect_to'] system_token = token_data['system_token'] # If we're authorized and we're the same user_id then redirect to # where we need to be if not user_id or request.user.id == user_id: path, _, qs = redirect_to.partition('?') params = urlparse.parse_qs(qs) # since the token can be custom we prepend the size of the user_token # to the token being forwarded so the view handling the `redirect_to` # can lookup the token and verify the system token. params.update({'token': '%s-%s%s' % (len(token), token, system_token)}) return redirect('%s?%s' % (path, urlencode(params))) # If we got here then we need authentication and the user's either not # logged in or is logged in with a wrong account. if request.user.is_authenticated(): logout(request) messages.info(request, 'Wrong account for this token.') return redirect( '%s?%s' % (reverse('auth_login'), urlencode({ 'next': reverse('token', kwargs={'token': token}), })))
def handle(self, worker_name, command, *parameters, **config): handler = getattr(self, 'handle_%s' % (command,), None) if handler is None: raise CommandError('Unknown command %s' % (command,)) args = [] kwargs = {} for parameter in parameters: parameter = parameter.strip() if '=' in parameter: kwargs.update(dict((parameter.split('=', 1),))) else: args.append(parameter) cmd = handler(worker_name, command, *args, **kwargs) vumi_api().mapi.send_command(cmd)
def token(request, token): # We only need the redis manager here, but it's saner to get a whole # vumi_api and not worry about all the setup magic. api = vumi_api() token_data = api.token_manager.get(token) if not token_data: raise Http404 user_id = int(token_data['user_id']) redirect_to = token_data['redirect_to'] system_token = token_data['system_token'] # If we're authorized and we're the same user_id then redirect to # where we need to be if not user_id or request.user.id == user_id: path, _, qs = redirect_to.partition('?') params = urlparse.parse_qs(qs) # since the token can be custom we prepend the size of the user_token # to the token being forwarded so the view handling the `redirect_to` # can lookup the token and verify the system token. params.update({'token': '%s-%s%s' % (len(token), token, system_token)}) return redirect('%s?%s' % (path, urlencode(params))) # If we got here then we need authentication and the user's either not # logged in or is logged in with a wrong account. if request.user.is_authenticated(): logout(request) messages.info(request, 'Wrong account for this token.') return redirect('%s?%s' % (reverse('auth_login'), urlencode({ 'next': reverse('token', kwargs={'token': token}), })))
def handle(self, worker_name, command, *parameters, **config): handler = getattr(self, 'handle_%s' % (command, ), None) if handler is None: raise CommandError('Unknown command %s' % (command, )) args = [] kwargs = {} for parameter in parameters: parameter = parameter.strip() if '=' in parameter: kwargs.update(dict((parameter.split('=', 1), ))) else: args.append(parameter) cmd = handler(worker_name, command, *args, **kwargs) vumi_api().mapi.send_command(cmd)
def process_response(self, request, response): try: start_time = request.start_time except AttributeError: # Ignoring AttributeError as that just means this request wasn't # seen by process_request (this is normal when other middleware # returns a response). return response try: metric = self.metric_from_request(request) except Resolver404: # Ignoring the Resolver404 as that just means we've not found a # page and any response metric on that will not be of interest # to us. return response response_time = start_time - time.time() response['X-Response-Time'] = response_time # TODO: Better way to fire these metrics. metrics = vumi_api().get_metric_manager(get_django_metric_prefix()) metrics.oneshot(metric, response_time) metrics.publish_metrics() return response
def test_vumi_api(self): vumi_api = utils.vumi_api() self.assertTrue(isinstance(vumi_api, VumiApi))
def mk_vumi_api(self): return vumi_api()
def __init__(self, session_key=None): super(SessionStore, self).__init__(session_key) self.session_manager = vumi_api().session_manager