Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Some things only need Redis, not a whole VumiApi.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Jun 18, 2015
1 parent 6690d6d commit 2169341
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 7 additions & 2 deletions go/api/go_api/session.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
# -*- test-case-name: go.api.go_api.tests.test_session -*-

from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase, CreateError
from vumi.persist.redis_manager import RedisManager

from go.base.utils import vumi_api
from go.api.go_api.session_manager import SessionManager


class SessionStore(SessionBase):
Expand All @@ -12,7 +14,10 @@ class SessionStore(SessionBase):
"""
def __init__(self, session_key=None):
super(SessionStore, self).__init__(session_key)
self.session_manager = vumi_api().session_manager
redis_config = settings.VUMI_API_CONFIG.get('redis_manager', {})
redis = RedisManager.from_config(redis_config)
self.session_manager = SessionManager(
redis.sub_manager('session_manager'))

def encode(self, data):
"""Replace Django's pickle serialization with a null-op.
Expand Down
9 changes: 9 additions & 0 deletions go/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django import forms
from django.http import Http404, HttpResponse
from django.conf import settings
from vumi.persist.redis_manager import RedisManager

from go.errors import UnknownConversationType, UnknownRouterType
from go.config import (
Expand Down Expand Up @@ -47,6 +48,14 @@ def sendfile(url, buffering=True, filename=None):
return response


def get_redis_manager():
"""
Build a Django-configured Redis manager.
"""
redis_config = settings.VUMI_API_CONFIG.get('redis_manager', {})
return RedisManager.from_config(redis_config)


def vumi_api():
"""Return a Vumi API instance."""
return VumiApi.from_config_sync(settings.VUMI_API_CONFIG, connection)
Expand Down
9 changes: 4 additions & 5 deletions go/token/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

from vumi.utils import load_class_by_string

from go.base.utils import vumi_api
from go.base.utils import get_redis_manager
from go.vumitools.token_manager import TokenManager


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)
tm = TokenManager(get_redis_manager().sub_manager('token_manager'))
token_data = tm.get(token)
if not token_data:
raise Http404

Expand Down

0 comments on commit 2169341

Please sign in to comment.